MCPcopy Create free account
hub / github.com/git/git / emit_hunk_header

Function emit_hunk_header

diff.c:1733–1798  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1731}
1732
1733static void emit_hunk_header(struct emit_callback *ecbdata,
1734 const char *line, int len)
1735{
1736 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
1737 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1738 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1739 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1740 const char *reverse = want_color(ecbdata->color_diff) ? GIT_COLOR_REVERSE : "";
1741 static const char atat[2] = { '@', '@' };
1742 const char *cp, *ep;
1743 struct strbuf msgbuf = STRBUF_INIT;
1744 int org_len = len;
1745 int i = 1;
1746
1747 /*
1748 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1749 * it always is at least 10 bytes long.
1750 */
1751 if (len < 10 ||
1752 memcmp(line, atat, 2) ||
1753 !(ep = memmem(line + 2, len - 2, atat, 2))) {
1754 emit_diff_symbol(ecbdata->opt,
1755 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
1756 return;
1757 }
1758 ep += 2; /* skip over @@ */
1759
1760 /* The hunk header in fraginfo color */
1761 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1762 strbuf_addstr(&msgbuf, reverse);
1763 strbuf_addstr(&msgbuf, frag);
1764 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1765 strbuf_add(&msgbuf, atat, sizeof(atat));
1766 else
1767 strbuf_add(&msgbuf, line, ep - line);
1768 strbuf_addstr(&msgbuf, reset);
1769
1770 /*
1771 * trailing "\r\n"
1772 */
1773 for ( ; i < 3; i++)
1774 if (line[len - i] == '\r' || line[len - i] == '\n')
1775 len--;
1776
1777 /* blank before the func header */
1778 for (cp = ep; ep - line < len; ep++)
1779 if (*ep != ' ' && *ep != '\t')
1780 break;
1781 if (ep != cp) {
1782 strbuf_addstr(&msgbuf, context);
1783 strbuf_add(&msgbuf, cp, ep - cp);
1784 strbuf_addstr(&msgbuf, reset);
1785 }
1786
1787 if (ep < line + len) {
1788 strbuf_addstr(&msgbuf, func);
1789 strbuf_add(&msgbuf, ep, line + len - ep);
1790 strbuf_addstr(&msgbuf, reset);

Callers 1

fn_out_consumeFunction · 0.85

Calls 6

diff_get_colorFunction · 0.85
emit_diff_symbolFunction · 0.85
strbuf_addstrFunction · 0.85
strbuf_addFunction · 0.85
strbuf_complete_lineFunction · 0.85
strbuf_releaseFunction · 0.85

Tested by

no test coverage detected