Skip to content

Commit 3ee8b7b

Browse files
stefanbellergitster
authored andcommitted
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>
1 parent f2bb121 commit 3ee8b7b

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

diff.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
561561
}
562562

563563
enum diff_symbol {
564+
DIFF_SYMBOL_FILEPAIR_PLUS,
565+
DIFF_SYMBOL_FILEPAIR_MINUS,
564566
DIFF_SYMBOL_WORDS_PORCELAIN,
565567
DIFF_SYMBOL_WORDS,
566568
DIFF_SYMBOL_CONTEXT,
@@ -611,7 +613,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
611613
const char *line, int len, unsigned flags)
612614
{
613615
static const char *nneof = " No newline at end of file\n";
614-
const char *context, *reset, *set;
616+
const char *context, *reset, *set, *meta;
615617
switch (s) {
616618
case DIFF_SYMBOL_NO_LF_EOF:
617619
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,
673675
}
674676
emit_line(o, context, reset, line, len);
675677
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;
676692
default:
677693
die("BUG: unknown diff symbol");
678694
}
@@ -844,8 +860,6 @@ static void emit_rewrite_diff(const char *name_a,
844860
struct diff_options *o)
845861
{
846862
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);
849863
const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
850864
const char *reset = diff_get_color(o->use_color, DIFF_RESET);
851865
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
@@ -865,8 +879,6 @@ static void emit_rewrite_diff(const char *name_a,
865879

866880
name_a += (*name_a == '/');
867881
name_b += (*name_b == '/');
868-
name_a_tab = strchr(name_a, ' ') ? "\t" : "";
869-
name_b_tab = strchr(name_b, ' ') ? "\t" : "";
870882

871883
strbuf_reset(&a_name);
872884
strbuf_reset(&b_name);
@@ -893,11 +905,13 @@ static void emit_rewrite_diff(const char *name_a,
893905

894906
lc_a = count_lines(data_one, size_one);
895907
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);
901915
if (!o->irreversible_delete)
902916
print_line_count(o->file, lc_a);
903917
else
@@ -1365,10 +1379,8 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
13651379
static void fn_out_consume(void *priv, char *line, unsigned long len)
13661380
{
13671381
struct emit_callback *ecbdata = priv;
1368-
const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
13691382
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
13701383
struct diff_options *o = ecbdata->opt;
1371-
const char *line_prefix = diff_line_prefix(o);
13721384

13731385
o->found_changes = 1;
13741386

@@ -1379,15 +1391,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
13791391
}
13801392

13811393
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);
13911400
ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
13921401
}
13931402

0 commit comments

Comments
 (0)