@matheusbsbs / matheus-bagliano / commits / 89cb73a19a
Fork 0

Give the hunk comment its own color

Inspired by the coloring of quilt. Introduce a separate color and paint the hunk comment part, i.e. the name of the function, in a separate color "diff.func" (defaults to plain). Whitespace between hunk header and hunk comment is printed in plain color. Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bert Wesarg committed Nov 27, 2009 at 07:55 UTC 89cb73a19ac94d15babf77af490fa5db78908234
5 files changed +52 -9
Documentation/config.txt
+4 -4
@@ -635,10 +635,10 @@ color.diff.<slot>::
635 Use customized color for diff colorization. `<slot>` specifies
636 which part of the patch to use the specified color, and is one
637 of `plain` (context text), `meta` (metainformation), `frag`
638 - (hunk header), `old` (removed lines), `new` (added lines),
639 - `commit` (commit headers), or `whitespace` (highlighting
640 - whitespace errors). The values of these variables may be specified as
641 - in color.branch.<slot>.
638 + (hunk header), 'func' (function in hunk header), `old` (removed lines),
639 + `new` (added lines), `commit` (commit headers), or `whitespace`
640 + (highlighting whitespace errors). The values of these variables may be
641 + specified as in color.branch.<slot>.
642
643 color.grep::
644 When set to `always`, always highlight matches. When `false` (or
combine-diff.c
+4 -1
@@ -524,6 +524,7 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
524 int i;
525 unsigned long lno = 0;
526 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
527 + const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
528 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
529 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
530 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
@@ -588,7 +589,9 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
589 comment_end = i;
590 }
591 if (comment_end)
591 - putchar(' ');
592 + printf("%s%s %s%s", c_reset,
593 + c_plain, c_reset,
594 + c_func);
595 for (i = 0; i < comment_end; i++)
596 putchar(hunk_comment[i]);
597 }
diff.c
+40 -3
@@ -39,6 +39,7 @@ static char diff_colors[][COLOR_MAXLEN] = {
39 GIT_COLOR_GREEN, /* NEW */
40 GIT_COLOR_YELLOW, /* COMMIT */
41 GIT_COLOR_BG_RED, /* WHITESPACE */
42 + GIT_COLOR_NORMAL, /* FUNCINFO */
43 };
44
45 static void diff_filespec_load_driver(struct diff_filespec *one);
@@ -60,6 +61,8 @@ static int parse_diff_color_slot(const char *var, int ofs)
61 return DIFF_COMMIT;
62 if (!strcasecmp(var+ofs, "whitespace"))
63 return DIFF_WHITESPACE;
64 + if (!strcasecmp(var+ofs, "func"))
65 + return DIFF_FUNCINFO;
66 die("bad config variable '%s'", var);
67 }
68
@@ -345,6 +348,42 @@ static void emit_add_line(const char *reset,
348 }
349 }
350
351 +static void emit_hunk_header(struct emit_callback *ecbdata,
352 + const char *line, int len)
353 +{
354 + const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
355 + const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
356 + const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
357 + const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
358 + static const char atat[2] = { '@', '@' };
359 + const char *cp, *ep;
360 +
361 + /*
362 + * As a hunk header must begin with "@@ -<old>, +<new> @@",
363 + * it always is at least 10 bytes long.
364 + */
365 + if (len < 10 ||
366 + memcmp(line, atat, 2) ||
367 + !(ep = memmem(line + 2, len - 2, atat, 2))) {
368 + emit_line(ecbdata->file, plain, reset, line, len);
369 + return;
370 + }
371 + ep += 2; /* skip over @@ */
372 +
373 + /* The hunk header in fraginfo color */
374 + emit_line(ecbdata->file, frag, reset, line, ep - line);
375 +
376 + /* blank before the func header */
377 + for (cp = ep; ep - line < len; ep++)
378 + if (*ep != ' ' && *ep != '\t')
379 + break;
380 + if (ep != cp)
381 + emit_line(ecbdata->file, plain, reset, cp, ep - cp);
382 +
383 + if (ep < line + len)
384 + emit_line(ecbdata->file, func, reset, ep, line + len - ep);
385 +}
386 +
387 static struct diff_tempfile *claim_diff_tempfile(void) {
388 int i;
389 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
@@ -782,9 +821,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
821 diff_words_flush(ecbdata);
822 len = sane_truncate_line(ecbdata, line, len);
823 find_lno(line, ecbdata);
785 - emit_line(ecbdata->file,
786 - diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
787 - reset, line, len);
824 + emit_hunk_header(ecbdata, line, len);
825 if (line[len-1] != '\n')
826 putc('\n', ecbdata->file);
827 return;
diff.h
+1
@@ -130,6 +130,7 @@ enum color_diff {
130 DIFF_FILE_NEW = 5,
131 DIFF_COMMIT = 6,
132 DIFF_WHITESPACE = 7,
133 + DIFF_FUNCINFO = 8,
134 };
135 const char *diff_get_color(int diff_use_color, enum color_diff ix);
136 #define diff_get_color_opt(o, ix) \
t/t4034-diff-words.sh
+3 -1
@@ -8,6 +8,7 @@ test_expect_success setup '
8
9 git config diff.color.old red
10 git config diff.color.new green
11 + git config diff.color.func magenta
12
13 '
14
@@ -16,6 +17,7 @@ decrypt_color () {
17 -e 's/.\[1m/<WHITE>/g' \
18 -e 's/.\[31m/<RED>/g' \
19 -e 's/.\[32m/<GREEN>/g' \
20 + -e 's/.\[35m/<MAGENTA>/g' \
21 -e 's/.\[36m/<BROWN>/g' \
22 -e 's/.\[m/<RESET>/g'
23 }
@@ -70,7 +72,7 @@ cat > expect <<\EOF
72 <WHITE>+++ b/post<RESET>
73 <BROWN>@@ -1 +1 @@<RESET>
74 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
73 -<BROWN>@@ -3,0 +4,4 @@ a = b + c<RESET>
75 +<BROWN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
76
77 <GREEN>aa = a<RESET>
78