diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFF
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
5af6ea957c60c2f828a84c1d3862d33a7bd5e58f
1 file changed
+21
-14
diff.c
+21
-14
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
561
}
562
563
enum diff_symbol {
564
+ DIFF_SYMBOL_REWRITE_DIFF,
565
DIFF_SYMBOL_BINARY_FILES,
566
DIFF_SYMBOL_HEADER,
567
DIFF_SYMBOL_FILEPAIR_PLUS,
@@ -615,7 +616,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
616
const char *line, int len, unsigned flags)
617
{
618
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;
620
switch (s) {
621
case DIFF_SYMBOL_NO_LF_EOF:
622
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,
696
case DIFF_SYMBOL_HEADER:
697
fprintf(o->file, "%s", line);
698
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;
704
default:
705
die("BUG: unknown diff symbol");
706
}
@@ -817,17 +823,17 @@ static void remove_tempfile(void)
823
}
824
}
825
820
-static void print_line_count(FILE *file, int count)
826
+static void add_line_count(struct strbuf *out, int count)
827
{
828
switch (count) {
829
case 0:
824
- fprintf(file, "0,0");
830
+ strbuf_addstr(out, "0,0");
831
break;
832
case 1:
827
- fprintf(file, "1");
833
+ strbuf_addstr(out, "1");
834
break;
835
default:
830
- fprintf(file, "1,%d", count);
836
+ strbuf_addf(out, "1,%d", count);
837
break;
838
}
839
}
@@ -866,14 +872,12 @@ static void emit_rewrite_diff(const char *name_a,
872
struct diff_options *o)
873
{
874
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);
875
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
876
const char *a_prefix, *b_prefix;
877
char *data_one, *data_two;
878
size_t size_one, size_two;
879
struct emit_callback ecbdata;
876
- const char *line_prefix = diff_line_prefix(o);
880
+ struct strbuf out = STRBUF_INIT;
881
882
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
883
a_prefix = o->b_prefix;
@@ -917,14 +921,17 @@ static void emit_rewrite_diff(const char *name_a,
921
emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
922
b_name.buf, b_name.len, 0);
923
920
- fprintf(o->file, "%s%s@@ -", line_prefix, fraginfo);
924
+ strbuf_addstr(&out, "@@ -");
925
if (!o->irreversible_delete)
922
- print_line_count(o->file, lc_a);
926
+ add_line_count(&out, lc_a);
927
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
+
935
if (lc_a && !o->irreversible_delete)
936
emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
937
if (lc_b)