@matheusbsbs / matheus-bagliano / commits / 690ed84363
Fork 0

diff --color: color blank-at-eof

Since the coloring logic processed the patch output one line at a time, we couldn't easily color code the new blank lines at the end of file. Reuse the adds_blank_at_eof() function to find where the runs of such blank lines start, keep track of the line number in the preimage while processing the patch output one line at a time, and paint the new blank lines that appear after that line to implement this. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 4, 2009 at 00:41 UTC 690ed8436326484fe7e3f4deac4cffd780c7d630
2 files changed +36 -10
diff.c
+27 -10
@@ -491,6 +491,8 @@ struct emit_callback {
491 struct xdiff_emit_state xm;
492 int color_diff;
493 unsigned ws_rule;
494 + int blank_at_eof;
495 + int lno_in_preimage;
496 sane_truncate_fn truncate;
497 const char **label_path;
498 struct diff_words_data *diff_words;
@@ -547,6 +549,12 @@ static void emit_add_line(const char *reset, struct emit_callback *ecbdata, cons
549
550 if (!*ws)
551 emit_line(ecbdata->file, set, reset, line, len);
552 + else if ((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
553 + ecbdata->blank_at_eof &&
554 + (ecbdata->blank_at_eof <= ecbdata->lno_in_preimage) &&
555 + ws_blank_line(line + 1, len - 1, ecbdata->ws_rule))
556 + /* Blank line at EOF */
557 + emit_line(ecbdata->file, ws, reset, line, len);
558 else {
559 /* Emit just the prefix, then the rest. */
560 emit_line(ecbdata->file, set, reset, line, 1);
@@ -573,9 +581,16 @@ static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, u
581 return allot - l;
582 }
583
584 +static int find_preimage_lno(const char *line)
585 +{
586 + char *p = strchr(line, '-');
587 + if (!p)
588 + return 0; /* should not happen */
589 + return strtol(p+1, NULL, 10);
590 +}
591 +
592 static void fn_out_consume(void *priv, char *line, unsigned long len)
593 {
578 - int color;
594 struct emit_callback *ecbdata = priv;
595 const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
596 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
@@ -598,6 +613,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
613
614 if (line[0] == '@') {
615 len = sane_truncate_line(ecbdata, line, len);
616 + ecbdata->lno_in_preimage = find_preimage_lno(line);
617 emit_line(ecbdata->file,
618 diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
619 reset, line, len);
@@ -611,7 +627,6 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
627 return;
628 }
629
614 - color = DIFF_PLAIN;
630 if (ecbdata->diff_words) {
631 if (line[0] == '-') {
632 diff_words_append(line, len,
@@ -630,14 +645,13 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
645 emit_line(ecbdata->file, plain, reset, line, len);
646 return;
647 }
633 - if (line[0] == '-')
634 - color = DIFF_FILE_OLD;
635 - else if (line[0] == '+')
636 - color = DIFF_FILE_NEW;
637 - if (color != DIFF_FILE_NEW) {
638 - emit_line(ecbdata->file,
639 - diff_get_color(ecbdata->color_diff, color),
640 - reset, line, len);
648 +
649 + if (line[0] != '+') {
650 + const char *color =
651 + diff_get_color(ecbdata->color_diff,
652 + line[0] == '-' ? DIFF_FILE_OLD : DIFF_PLAIN);
653 + ecbdata->lno_in_preimage++;
654 + emit_line(ecbdata->file, color, reset, line, len);
655 return;
656 }
657 emit_add_line(reset, ecbdata, line, len);
@@ -1557,6 +1571,9 @@ static void builtin_diff(const char *name_a,
1571 ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1572 ecbdata.found_changesp = &o->found_changes;
1573 ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
1574 + if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
1575 + ecbdata.blank_at_eof =
1576 + adds_blank_at_eof(&mf1, &mf2, ecbdata.ws_rule);
1577 ecbdata.file = o->file;
1578 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1579 xecfg.ctxlen = o->context;
t/t4019-diff-wserror.sh
+9
@@ -190,4 +190,13 @@ test_expect_success 'do not color trailing cr in context' '
190
191 '
192
193 +test_expect_success 'color new trailing blank lines' '
194 + { echo a; echo b; echo; echo; } >x &&
195 + git add x &&
196 + { echo a; echo; echo; echo; echo; } >x &&
197 + git diff --color x >output &&
198 + cnt=$(grep "${blue_grep}" output | wc -l) &&
199 + test $cnt = 2
200 +'
201 +
202 test_done