提交 13e36ec5 编写于 作者: J Johannes Schindelin 提交者: Junio C Hamano

Teach diff -B about colours

Matthias Lederhofer noticed that `diff -B` did not pick up on diff
colournig.
Signed-off-by: NJohannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: NJunio C Hamano <junkio@cox.net>
上级 1918278e
...@@ -184,30 +184,40 @@ static void print_line_count(int count) ...@@ -184,30 +184,40 @@ static void print_line_count(int count)
} }
} }
static void copy_file(int prefix, const char *data, int size) static void copy_file(int prefix, const char *data, int size,
const char *set, const char *reset)
{ {
int ch, nl_just_seen = 1; int ch, nl_just_seen = 1;
while (0 < size--) { while (0 < size--) {
ch = *data++; ch = *data++;
if (nl_just_seen) if (nl_just_seen) {
fputs(set, stdout);
putchar(prefix); putchar(prefix);
putchar(ch); }
if (ch == '\n') if (ch == '\n') {
nl_just_seen = 1; nl_just_seen = 1;
else fputs(reset, stdout);
} else
nl_just_seen = 0; nl_just_seen = 0;
putchar(ch);
} }
if (!nl_just_seen) if (!nl_just_seen)
printf("\n\\ No newline at end of file\n"); printf("%s\n\\ No newline at end of file\n", reset);
} }
static void emit_rewrite_diff(const char *name_a, static void emit_rewrite_diff(const char *name_a,
const char *name_b, const char *name_b,
struct diff_filespec *one, struct diff_filespec *one,
struct diff_filespec *two) struct diff_filespec *two,
int color_diff)
{ {
int lc_a, lc_b; int lc_a, lc_b;
const char *name_a_tab, *name_b_tab; const char *name_a_tab, *name_b_tab;
const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
const char *reset = diff_get_color(color_diff, DIFF_RESET);
name_a_tab = strchr(name_a, ' ') ? "\t" : ""; name_a_tab = strchr(name_a, ' ') ? "\t" : "";
name_b_tab = strchr(name_b, ' ') ? "\t" : ""; name_b_tab = strchr(name_b, ' ') ? "\t" : "";
...@@ -216,17 +226,17 @@ static void emit_rewrite_diff(const char *name_a, ...@@ -216,17 +226,17 @@ static void emit_rewrite_diff(const char *name_a,
diff_populate_filespec(two, 0); diff_populate_filespec(two, 0);
lc_a = count_lines(one->data, one->size); lc_a = count_lines(one->data, one->size);
lc_b = count_lines(two->data, two->size); lc_b = count_lines(two->data, two->size);
printf("--- a/%s%s\n+++ b/%s%s\n@@ -", printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
name_a, name_a_tab, metainfo, name_a, name_a_tab, reset,
name_b, name_b_tab); metainfo, name_b, name_b_tab, reset, fraginfo);
print_line_count(lc_a); print_line_count(lc_a);
printf(" +"); printf(" +");
print_line_count(lc_b); print_line_count(lc_b);
printf(" @@\n"); printf(" @@%s\n", reset);
if (lc_a) if (lc_a)
copy_file('-', one->data, one->size); copy_file('-', one->data, one->size, old, reset);
if (lc_b) if (lc_b)
copy_file('+', two->data, two->size); copy_file('+', two->data, two->size, new, reset);
} }
static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
...@@ -1084,7 +1094,8 @@ static void builtin_diff(const char *name_a, ...@@ -1084,7 +1094,8 @@ static void builtin_diff(const char *name_a,
if ((one->mode ^ two->mode) & S_IFMT) if ((one->mode ^ two->mode) & S_IFMT)
goto free_ab_and_return; goto free_ab_and_return;
if (complete_rewrite) { if (complete_rewrite) {
emit_rewrite_diff(name_a, name_b, one, two); emit_rewrite_diff(name_a, name_b, one, two,
o->color_diff);
goto free_ab_and_return; goto free_ab_and_return;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册