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>
This commit is contained in:
Stefan Beller 2017-06-29 17:06:59 -07:00 коммит произвёл Junio C Hamano
Родитель 4acaaa7af6
Коммит 5af6ea957c
1 изменённых файлов: 21 добавлений и 14 удалений

35
diff.c
Просмотреть файл

@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
}
enum diff_symbol {
DIFF_SYMBOL_REWRITE_DIFF,
DIFF_SYMBOL_BINARY_FILES,
DIFF_SYMBOL_HEADER,
DIFF_SYMBOL_FILEPAIR_PLUS,
@ -615,7 +616,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
const char *line, int len, unsigned flags)
{
static const char *nneof = " No newline at end of file\n";
const char *context, *reset, *set, *meta;
const char *context, *reset, *set, *meta, *fraginfo;
switch (s) {
case DIFF_SYMBOL_NO_LF_EOF:
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,
case DIFF_SYMBOL_HEADER:
fprintf(o->file, "%s", line);
break;
case DIFF_SYMBOL_REWRITE_DIFF:
fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
reset = diff_get_color_opt(o, DIFF_RESET);
emit_line(o, fraginfo, reset, line, len);
break;
default:
die("BUG: unknown diff symbol");
}
@ -817,17 +823,17 @@ static void remove_tempfile(void)
}
}
static void print_line_count(FILE *file, int count)
static void add_line_count(struct strbuf *out, int count)
{
switch (count) {
case 0:
fprintf(file, "0,0");
strbuf_addstr(out, "0,0");
break;
case 1:
fprintf(file, "1");
strbuf_addstr(out, "1");
break;
default:
fprintf(file, "1,%d", count);
strbuf_addf(out, "1,%d", count);
break;
}
}
@ -866,14 +872,12 @@ static void emit_rewrite_diff(const char *name_a,
struct diff_options *o)
{
int lc_a, lc_b;
const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
const char *reset = diff_get_color(o->use_color, DIFF_RESET);
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
const char *a_prefix, *b_prefix;
char *data_one, *data_two;
size_t size_one, size_two;
struct emit_callback ecbdata;
const char *line_prefix = diff_line_prefix(o);
struct strbuf out = STRBUF_INIT;
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
a_prefix = o->b_prefix;
@ -917,14 +921,17 @@ static void emit_rewrite_diff(const char *name_a,
emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
b_name.buf, b_name.len, 0);
fprintf(o->file, "%s%s@@ -", line_prefix, fraginfo);
strbuf_addstr(&out, "@@ -");
if (!o->irreversible_delete)
print_line_count(o->file, lc_a);
add_line_count(&out, lc_a);
else
fprintf(o->file, "?,?");
fprintf(o->file, " +");
print_line_count(o->file, lc_b);
fprintf(o->file, " @@%s\n", reset);
strbuf_addstr(&out, "?,?");
strbuf_addstr(&out, " +");
add_line_count(&out, lc_b);
strbuf_addstr(&out, " @@\n");
emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
strbuf_release(&out);
if (lc_a && !o->irreversible_delete)
emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
if (lc_b)