@matheusbsbs / matheus-bagliano / commits / 13e36ec51b
Fork 0

Teach diff -B about colours

Matthias Lederhofer noticed that `diff -B` did not pick up on diff colournig. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Feb 20, 2007 at 15:08 UTC 13e36ec51bee59c6433322b23323b75e9a635d35
1 file changed +25 -14
diff.c
+25 -14
@@ -184,30 +184,40 @@ static void print_line_count(int count)
184 }
185 }
186
187 -static void copy_file(int prefix, const char *data, int size)
187 +static void copy_file(int prefix, const char *data, int size,
188 + const char *set, const char *reset)
189 {
190 int ch, nl_just_seen = 1;
191 while (0 < size--) {
192 ch = *data++;
192 - if (nl_just_seen)
193 + if (nl_just_seen) {
194 + fputs(set, stdout);
195 putchar(prefix);
194 - putchar(ch);
195 - if (ch == '\n')
196 + }
197 + if (ch == '\n') {
198 nl_just_seen = 1;
197 - else
199 + fputs(reset, stdout);
200 + } else
201 nl_just_seen = 0;
202 + putchar(ch);
203 }
204 if (!nl_just_seen)
201 - printf("\n\\ No newline at end of file\n");
205 + printf("%s\n\\ No newline at end of file\n", reset);
206 }
207
208 static void emit_rewrite_diff(const char *name_a,
209 const char *name_b,
210 struct diff_filespec *one,
207 - struct diff_filespec *two)
211 + struct diff_filespec *two,
212 + int color_diff)
213 {
214 int lc_a, lc_b;
215 const char *name_a_tab, *name_b_tab;
216 + const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
217 + const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
218 + const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
219 + const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
220 + const char *reset = diff_get_color(color_diff, DIFF_RESET);
221
222 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
223 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
@@ -216,17 +226,17 @@ static void emit_rewrite_diff(const char *name_a,
226 diff_populate_filespec(two, 0);
227 lc_a = count_lines(one->data, one->size);
228 lc_b = count_lines(two->data, two->size);
219 - printf("--- a/%s%s\n+++ b/%s%s\n@@ -",
220 - name_a, name_a_tab,
221 - name_b, name_b_tab);
229 + printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
230 + metainfo, name_a, name_a_tab, reset,
231 + metainfo, name_b, name_b_tab, reset, fraginfo);
232 print_line_count(lc_a);
233 printf(" +");
234 print_line_count(lc_b);
225 - printf(" @@\n");
235 + printf(" @@%s\n", reset);
236 if (lc_a)
227 - copy_file('-', one->data, one->size);
237 + copy_file('-', one->data, one->size, old, reset);
238 if (lc_b)
229 - copy_file('+', two->data, two->size);
239 + copy_file('+', two->data, two->size, new, reset);
240 }
241
242 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
@@ -1084,7 +1094,8 @@ static void builtin_diff(const char *name_a,
1094 if ((one->mode ^ two->mode) & S_IFMT)
1095 goto free_ab_and_return;
1096 if (complete_rewrite) {
1087 - emit_rewrite_diff(name_a, name_b, one, two);
1097 + emit_rewrite_diff(name_a, name_b, one, two,
1098 + o->color_diff);
1099 goto free_ab_and_return;
1100 }
1101 }