Skip to content

Commit eab9a40

Browse files
dschogitster
authored andcommitted
Teach diff machinery to display other prefixes than "a/" and "b/"
With the new options "--src-prefix=<prefix>", "--dst-prefix=<prefix>" and "--no-prefix", you can now control the path prefixes of the diff machinery. These used to by hardwired to "a/" for the source prefix and "b/" for the destination prefix. Initial patch by Pascal Obry. Sane option names suggested by Linus. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 74f6b03 commit eab9a40

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

Documentation/diff-options.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,14 @@ endif::git-format-patch[]
211211
--no-ext-diff::
212212
Disallow external diff drivers.
213213

214+
--src-prefix=<prefix>::
215+
Show the given source prefix instead of "a/".
216+
217+
--dst-prefix=<prefix>::
218+
Show the given destination prefix instead of "b/".
219+
220+
--no-prefix::
221+
Do not show any source or destination prefix.
222+
214223
For more detailed explanation on these common options, see also
215224
link:diffcore.html[diffcore documentation].

diff.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ static void emit_rewrite_diff(const char *name_a,
290290
const char *name_b,
291291
struct diff_filespec *one,
292292
struct diff_filespec *two,
293-
int color_diff)
293+
struct diff_options *o)
294294
{
295295
int lc_a, lc_b;
296+
int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
296297
const char *name_a_tab, *name_b_tab;
297298
const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
298299
const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
@@ -309,9 +310,9 @@ static void emit_rewrite_diff(const char *name_a,
309310
diff_populate_filespec(two, 0);
310311
lc_a = count_lines(one->data, one->size);
311312
lc_b = count_lines(two->data, two->size);
312-
printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
313-
metainfo, name_a, name_a_tab, reset,
314-
metainfo, name_b, name_b_tab, reset, fraginfo);
313+
printf("%s--- %s%s%s%s\n%s+++ %s%s%s%s\n%s@@ -",
314+
metainfo, o->a_prefix, name_a, name_a_tab, reset,
315+
metainfo, o->b_prefix, name_b, name_b_tab, reset, fraginfo);
315316
print_line_count(lc_a);
316317
printf(" +");
317318
print_line_count(lc_b);
@@ -1212,8 +1213,8 @@ static void builtin_diff(const char *name_a,
12121213
const char *set = diff_get_color_opt(o, DIFF_METAINFO);
12131214
const char *reset = diff_get_color_opt(o, DIFF_RESET);
12141215

1215-
a_one = quote_two("a/", name_a + (*name_a == '/'));
1216-
b_two = quote_two("b/", name_b + (*name_b == '/'));
1216+
a_one = quote_two(o->a_prefix, name_a + (*name_a == '/'));
1217+
b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
12171218
lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
12181219
lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
12191220
printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
@@ -1242,8 +1243,7 @@ static void builtin_diff(const char *name_a,
12421243
if ((one->mode ^ two->mode) & S_IFMT)
12431244
goto free_ab_and_return;
12441245
if (complete_rewrite) {
1245-
emit_rewrite_diff(name_a, name_b, one, two,
1246-
DIFF_OPT_TST(o, COLOR_DIFF));
1246+
emit_rewrite_diff(name_a, name_b, one, two, o);
12471247
o->found_changes = 1;
12481248
goto free_ab_and_return;
12491249
}
@@ -2020,6 +2020,9 @@ void diff_setup(struct diff_options *options)
20202020
else
20212021
DIFF_OPT_CLR(options, COLOR_DIFF);
20222022
options->detect_rename = diff_detect_rename_default;
2023+
2024+
options->a_prefix = "a/";
2025+
options->b_prefix = "b/";
20232026
}
20242027

20252028
int diff_setup_done(struct diff_options *options)
@@ -2291,6 +2294,12 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
22912294
else if (40 < options->abbrev)
22922295
options->abbrev = 40;
22932296
}
2297+
else if (!prefixcmp(arg, "--src-prefix="))
2298+
options->a_prefix = arg + 13;
2299+
else if (!prefixcmp(arg, "--dst-prefix="))
2300+
options->b_prefix = arg + 13;
2301+
else if (!strcmp(arg, "--no-prefix"))
2302+
options->a_prefix = options->b_prefix = "";
22942303
else
22952304
return 0;
22962305
return 1;

diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct diff_options {
6969
const char *orderfile;
7070
const char *pickaxe;
7171
const char *single_follow;
72+
const char *a_prefix, *b_prefix;
7273
unsigned flags;
7374
int context;
7475
int break_opt;

0 commit comments

Comments
 (0)