Skip to content

Commit e69a6f4

Browse files
committed
Merge branch 'jc/diff-prefix'
* jc/diff-prefix: diff: vary default prefix depending on what are compared
2 parents c439828 + a5a818e commit e69a6f4

File tree

7 files changed

+70
-8
lines changed

7 files changed

+70
-8
lines changed

Documentation/config.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@ diff.external::
585585
you want to use an external diff program only on a subset of
586586
your files, you might want to use linkgit:gitattributes[5] instead.
587587

588+
diff.mnemonicprefix::
589+
If set, 'git-diff' uses a prefix pair that is different from the
590+
standard "a/" and "b/" depending on what is being compared. When
591+
this configuration is in effect, reverse diff output also swaps
592+
the order of the prefixes:
593+
'git-diff';;
594+
compares the (i)ndex and the (w)ork tree;
595+
'git-diff HEAD';;
596+
compares a (c)ommit and the (w)ork tree;
597+
'git diff --cached';;
598+
compares a (c)ommit and the (i)ndex;
599+
'git-diff HEAD:file1 file2';;
600+
compares an (o)bject and a (w)ork tree entity;
601+
'git diff --no-index a b';;
602+
compares two non-git things (1) and (2).
603+
588604
diff.renameLimit::
589605
The number of files to consider when performing the copy/rename
590606
detection; equivalent to the 'git-diff' option '-l'.

builtin-diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static int builtin_diff_b_f(struct rev_info *revs,
7474
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
7575
die("'%s': not a regular file or symlink", path);
7676

77+
diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
78+
7779
if (blob[0].mode == S_IFINVALID)
7880
blob[0].mode = canon_mode(st.st_mode);
7981

combine-diff.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
683683
int i, show_hunks;
684684
int working_tree_file = is_null_sha1(elem->sha1);
685685
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
686+
const char *a_prefix, *b_prefix;
686687
mmfile_t result_file;
687688

688689
context = opt->context;
690+
a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
691+
b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
692+
689693
/* Read the result of merge first */
690694
if (!working_tree_file)
691695
result = grab_blob(elem->sha1, &result_size);
@@ -861,13 +865,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
861865
dump_quoted_path("--- ", "", "/dev/null",
862866
c_meta, c_reset);
863867
else
864-
dump_quoted_path("--- ", opt->a_prefix, elem->path,
868+
dump_quoted_path("--- ", a_prefix, elem->path,
865869
c_meta, c_reset);
866870
if (deleted)
867871
dump_quoted_path("+++ ", "", "/dev/null",
868872
c_meta, c_reset);
869873
else
870-
dump_quoted_path("+++ ", opt->b_prefix, elem->path,
874+
dump_quoted_path("+++ ", b_prefix, elem->path,
871875
c_meta, c_reset);
872876
dump_sline(sline, cnt, num_parent,
873877
DIFF_OPT_TST(opt, COLOR_DIFF));

diff-lib.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
6363
? CE_MATCH_RACY_IS_DIRTY : 0);
6464
char symcache[PATH_MAX];
6565

66+
diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
67+
6668
if (diff_unmerged_stage < 0)
6769
diff_unmerged_stage = 2;
6870
entries = active_nr;
@@ -469,6 +471,7 @@ int run_diff_index(struct rev_info *revs, int cached)
469471
if (unpack_trees(1, &t, &opts))
470472
exit(128);
471473

474+
diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
472475
diffcore_std(&revs->diffopt);
473476
diff_flush(&revs->diffopt);
474477
return 0;

diff-no-index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ void diff_no_index(struct rev_info *revs,
252252
if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
253253
revs->diffopt.paths[1]))
254254
exit(1);
255+
diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
255256
diffcore_std(&revs->diffopt);
256257
diff_flush(&revs->diffopt);
257258

diff.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static int diff_suppress_blank_empty;
2424
int diff_use_color_default = -1;
2525
static const char *external_diff_cmd_cfg;
2626
int diff_auto_refresh_index = 1;
27+
static int diff_mnemonic_prefix;
2728

2829
static char diff_colors[][COLOR_MAXLEN] = {
2930
"\033[m", /* reset */
@@ -150,6 +151,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
150151
diff_auto_refresh_index = git_config_bool(var, value);
151152
return 0;
152153
}
154+
if (!strcmp(var, "diff.mnemonicprefix")) {
155+
diff_mnemonic_prefix = git_config_bool(var, value);
156+
return 0;
157+
}
153158
if (!strcmp(var, "diff.external"))
154159
return git_config_string(&external_diff_cmd_cfg, var, value);
155160
if (!prefixcmp(var, "diff.")) {
@@ -312,6 +317,15 @@ static void emit_rewrite_diff(const char *name_a,
312317
const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
313318
const char *reset = diff_get_color(color_diff, DIFF_RESET);
314319
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
320+
const char *a_prefix, *b_prefix;
321+
322+
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
323+
a_prefix = o->b_prefix;
324+
b_prefix = o->a_prefix;
325+
} else {
326+
a_prefix = o->a_prefix;
327+
b_prefix = o->b_prefix;
328+
}
315329

316330
name_a += (*name_a == '/');
317331
name_b += (*name_b == '/');
@@ -320,8 +334,8 @@ static void emit_rewrite_diff(const char *name_a,
320334

321335
strbuf_reset(&a_name);
322336
strbuf_reset(&b_name);
323-
quote_two_c_style(&a_name, o->a_prefix, name_a, 0);
324-
quote_two_c_style(&b_name, o->b_prefix, name_b, 0);
337+
quote_two_c_style(&a_name, a_prefix, name_a, 0);
338+
quote_two_c_style(&b_name, b_prefix, name_b, 0);
325339

326340
diff_populate_filespec(one, 0);
327341
diff_populate_filespec(two, 0);
@@ -1447,6 +1461,14 @@ static const char *diff_funcname_pattern(struct diff_filespec *one)
14471461
return NULL;
14481462
}
14491463

1464+
void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
1465+
{
1466+
if (!options->a_prefix)
1467+
options->a_prefix = a;
1468+
if (!options->b_prefix)
1469+
options->b_prefix = b;
1470+
}
1471+
14501472
static void builtin_diff(const char *name_a,
14511473
const char *name_b,
14521474
struct diff_filespec *one,
@@ -1460,9 +1482,19 @@ static void builtin_diff(const char *name_a,
14601482
char *a_one, *b_two;
14611483
const char *set = diff_get_color_opt(o, DIFF_METAINFO);
14621484
const char *reset = diff_get_color_opt(o, DIFF_RESET);
1485+
const char *a_prefix, *b_prefix;
1486+
1487+
diff_set_mnemonic_prefix(o, "a/", "b/");
1488+
if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
1489+
a_prefix = o->b_prefix;
1490+
b_prefix = o->a_prefix;
1491+
} else {
1492+
a_prefix = o->a_prefix;
1493+
b_prefix = o->b_prefix;
1494+
}
14631495

1464-
a_one = quote_two(o->a_prefix, name_a + (*name_a == '/'));
1465-
b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
1496+
a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
1497+
b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
14661498
lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
14671499
lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
14681500
fprintf(o->file, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
@@ -2319,8 +2351,10 @@ void diff_setup(struct diff_options *options)
23192351
DIFF_OPT_CLR(options, COLOR_DIFF);
23202352
options->detect_rename = diff_detect_rename_default;
23212353

2322-
options->a_prefix = "a/";
2323-
options->b_prefix = "b/";
2354+
if (!diff_mnemonic_prefix) {
2355+
options->a_prefix = "a/";
2356+
options->b_prefix = "b/";
2357+
}
23242358
}
23252359

23262360
int diff_setup_done(struct diff_options *options)

diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ extern void diff_tree_combined(const unsigned char *sha1, const unsigned char pa
160160

161161
extern void diff_tree_combined_merge(const unsigned char *sha1, int, struct rev_info *);
162162

163+
void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b);
164+
163165
extern void diff_addremove(struct diff_options *,
164166
int addremove,
165167
unsigned mode,

0 commit comments

Comments
 (0)