@matheusbsbs / matheus-bagliano / commits / 1a9eb3b9d5
Fork 0

git-diff/git-apply: make diff output a bit friendlier to GNU patch (part 2)

Somebody was wondering on #git channel why a git generated diff does not apply with GNU patch when the filename contains a SP. It is because GNU patch expects to find TAB (and trailing timestamp) on ---/+++ (old_name and new_name) lines after the filenames. The "diff --git" output format was carefully designed to be compatible with GNU patch where it can, but whitespace characters were always a pain. This adds an extra TAB (but not trailing timestamp) to old_name and new_name lines of git-diff output when the filename has a SP in it. An earlier patch updated git-apply to prepare for this. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 22, 2006 at 16:17 UTC 1a9eb3b9d50367bee8fe85022684d812816fe531
1 file changed +17 -3
diff.c
+17 -3
@@ -204,11 +204,18 @@ static void emit_rewrite_diff(const char *name_a,
204 struct diff_filespec *two)
205 {
206 int lc_a, lc_b;
207 + const char *name_a_tab, *name_b_tab;
208 +
209 + name_a_tab = strchr(name_a, ' ') ? "\t" : "";
210 + name_b_tab = strchr(name_b, ' ') ? "\t" : "";
211 +
212 diff_populate_filespec(one, 0);
213 diff_populate_filespec(two, 0);
214 lc_a = count_lines(one->data, one->size);
215 lc_b = count_lines(two->data, two->size);
211 - printf("--- a/%s\n+++ b/%s\n@@ -", name_a, name_b);
216 + printf("--- a/%s%s\n+++ b/%s%s\n@@ -",
217 + name_a, name_a_tab,
218 + name_b, name_b_tab);
219 print_line_count(lc_a);
220 printf(" +");
221 print_line_count(lc_b);
@@ -474,8 +481,15 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
481 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
482
483 if (ecbdata->label_path[0]) {
477 - printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
478 - printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
484 + const char *name_a_tab, *name_b_tab;
485 +
486 + name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
487 + name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
488 +
489 + printf("%s--- %s%s%s\n",
490 + set, ecbdata->label_path[0], reset, name_a_tab);
491 + printf("%s+++ %s%s%s\n",
492 + set, ecbdata->label_path[1], reset, name_b_tab);
493 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
494 }
495