diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS}

We have to use fprintf instead of emit_line, because we want to emit the tab after the color. This is important for ancient versions of gnu patch AFAICT, although we probably do not want to feed colored output to the patch utility, such that it would not matter if the trailing tab is colored. Keep the corner case as-is though. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Jun 29, 2017 at 17:06 UTC 3ee8b7bfe403f8c0c566a6963f7377a0ae98011e
1 file changed +30 -21
diff.c
+30 -21
@@ -561,6 +561,8 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
561 }
562
563 enum diff_symbol {
564 + DIFF_SYMBOL_FILEPAIR_PLUS,
565 + DIFF_SYMBOL_FILEPAIR_MINUS,
566 DIFF_SYMBOL_WORDS_PORCELAIN,
567 DIFF_SYMBOL_WORDS,
568 DIFF_SYMBOL_CONTEXT,
@@ -611,7 +613,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
613 const char *line, int len, unsigned flags)
614 {
615 static const char *nneof = " No newline at end of file\n";
614 - const char *context, *reset, *set;
616 + const char *context, *reset, *set, *meta;
617 switch (s) {
618 case DIFF_SYMBOL_NO_LF_EOF:
619 context = diff_get_color_opt(o, DIFF_CONTEXT);
@@ -673,6 +675,20 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
675 }
676 emit_line(o, context, reset, line, len);
677 break;
678 + case DIFF_SYMBOL_FILEPAIR_PLUS:
679 + meta = diff_get_color_opt(o, DIFF_METAINFO);
680 + reset = diff_get_color_opt(o, DIFF_RESET);
681 + fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
682 + line, reset,
683 + strchr(line, ' ') ? "\t" : "");
684 + break;
685 + case DIFF_SYMBOL_FILEPAIR_MINUS:
686 + meta = diff_get_color_opt(o, DIFF_METAINFO);
687 + reset = diff_get_color_opt(o, DIFF_RESET);
688 + fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
689 + line, reset,
690 + strchr(line, ' ') ? "\t" : "");
691 + break;
692 default:
693 die("BUG: unknown diff symbol");
694 }
@@ -844,8 +860,6 @@ static void emit_rewrite_diff(const char *name_a,
860 struct diff_options *o)
861 {
862 int lc_a, lc_b;
847 - const char *name_a_tab, *name_b_tab;
848 - const char *metainfo = diff_get_color(o->use_color, DIFF_METAINFO);
863 const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
864 const char *reset = diff_get_color(o->use_color, DIFF_RESET);
865 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
@@ -865,8 +879,6 @@ static void emit_rewrite_diff(const char *name_a,
879
880 name_a += (*name_a == '/');
881 name_b += (*name_b == '/');
868 - name_a_tab = strchr(name_a, ' ') ? "\t" : "";
869 - name_b_tab = strchr(name_b, ' ') ? "\t" : "";
882
883 strbuf_reset(&a_name);
884 strbuf_reset(&b_name);
@@ -893,11 +905,13 @@ static void emit_rewrite_diff(const char *name_a,
905
906 lc_a = count_lines(data_one, size_one);
907 lc_b = count_lines(data_two, size_two);
896 - fprintf(o->file,
897 - "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
898 - line_prefix, metainfo, a_name.buf, name_a_tab, reset,
899 - line_prefix, metainfo, b_name.buf, name_b_tab, reset,
900 - line_prefix, fraginfo);
908 +
909 + emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
910 + a_name.buf, a_name.len, 0);
911 + emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
912 + b_name.buf, b_name.len, 0);
913 +
914 + fprintf(o->file, "%s%s@@ -", line_prefix, fraginfo);
915 if (!o->irreversible_delete)
916 print_line_count(o->file, lc_a);
917 else
@@ -1365,10 +1379,8 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
1379 static void fn_out_consume(void *priv, char *line, unsigned long len)
1380 {
1381 struct emit_callback *ecbdata = priv;
1368 - const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
1382 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1383 struct diff_options *o = ecbdata->opt;
1371 - const char *line_prefix = diff_line_prefix(o);
1384
1385 o->found_changes = 1;
1386
@@ -1379,15 +1391,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
1391 }
1392
1393 if (ecbdata->label_path[0]) {
1382 - const char *name_a_tab, *name_b_tab;
1383 -
1384 - name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
1385 - name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
1386 -
1387 - fprintf(o->file, "%s%s--- %s%s%s\n",
1388 - line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
1389 - fprintf(o->file, "%s%s+++ %s%s%s\n",
1390 - line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
1394 + emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1395 + ecbdata->label_path[0],
1396 + strlen(ecbdata->label_path[0]), 0);
1397 + emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1398 + ecbdata->label_path[1],
1399 + strlen(ecbdata->label_path[1]), 0);
1400 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
1401 }
1402