Skip to content

Commit 5af6ea9

Browse files
stefanbellergitster
authored andcommitted
diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFF
Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4acaaa7 commit 5af6ea9

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

diff.c

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

563563
enum diff_symbol {
564+
DIFF_SYMBOL_REWRITE_DIFF,
564565
DIFF_SYMBOL_BINARY_FILES,
565566
DIFF_SYMBOL_HEADER,
566567
DIFF_SYMBOL_FILEPAIR_PLUS,
@@ -615,7 +616,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
615616
const char *line, int len, unsigned flags)
616617
{
617618
static const char *nneof = " No newline at end of file\n";
618-
const char *context, *reset, *set, *meta;
619+
const char *context, *reset, *set, *meta, *fraginfo;
619620
switch (s) {
620621
case DIFF_SYMBOL_NO_LF_EOF:
621622
context = diff_get_color_opt(o, DIFF_CONTEXT);
@@ -695,6 +696,11 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
695696
case DIFF_SYMBOL_HEADER:
696697
fprintf(o->file, "%s", line);
697698
break;
699+
case DIFF_SYMBOL_REWRITE_DIFF:
700+
fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
701+
reset = diff_get_color_opt(o, DIFF_RESET);
702+
emit_line(o, fraginfo, reset, line, len);
703+
break;
698704
default:
699705
die("BUG: unknown diff symbol");
700706
}
@@ -817,17 +823,17 @@ static void remove_tempfile(void)
817823
}
818824
}
819825

820-
static void print_line_count(FILE *file, int count)
826+
static void add_line_count(struct strbuf *out, int count)
821827
{
822828
switch (count) {
823829
case 0:
824-
fprintf(file, "0,0");
830+
strbuf_addstr(out, "0,0");
825831
break;
826832
case 1:
827-
fprintf(file, "1");
833+
strbuf_addstr(out, "1");
828834
break;
829835
default:
830-
fprintf(file, "1,%d", count);
836+
strbuf_addf(out, "1,%d", count);
831837
break;
832838
}
833839
}
@@ -866,14 +872,12 @@ static void emit_rewrite_diff(const char *name_a,
866872
struct diff_options *o)
867873
{
868874
int lc_a, lc_b;
869-
const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
870-
const char *reset = diff_get_color(o->use_color, DIFF_RESET);
871875
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
872876
const char *a_prefix, *b_prefix;
873877
char *data_one, *data_two;
874878
size_t size_one, size_two;
875879
struct emit_callback ecbdata;
876-
const char *line_prefix = diff_line_prefix(o);
880+
struct strbuf out = STRBUF_INIT;
877881

878882
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
879883
a_prefix = o->b_prefix;
@@ -917,14 +921,17 @@ static void emit_rewrite_diff(const char *name_a,
917921
emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
918922
b_name.buf, b_name.len, 0);
919923

920-
fprintf(o->file, "%s%s@@ -", line_prefix, fraginfo);
924+
strbuf_addstr(&out, "@@ -");
921925
if (!o->irreversible_delete)
922-
print_line_count(o->file, lc_a);
926+
add_line_count(&out, lc_a);
923927
else
924-
fprintf(o->file, "?,?");
925-
fprintf(o->file, " +");
926-
print_line_count(o->file, lc_b);
927-
fprintf(o->file, " @@%s\n", reset);
928+
strbuf_addstr(&out, "?,?");
929+
strbuf_addstr(&out, " +");
930+
add_line_count(&out, lc_b);
931+
strbuf_addstr(&out, " @@\n");
932+
emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
933+
strbuf_release(&out);
934+
928935
if (lc_a && !o->irreversible_delete)
929936
emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
930937
if (lc_b)

0 commit comments

Comments
 (0)