Skip to content

Commit a5a818e

Browse files
committed
diff: vary default prefix depending on what are compared
With a new configuration "diff.mnemonicprefix", "git diff" shows the differences between various combinations of preimage and postimage trees with prefixes different from the standard "a/" and "b/". Hopefully this will make the distinction stand out for some people. "git diff" compares the (i)ndex and the (w)ork tree; "git diff HEAD" compares a (c)ommit and the (w)ork tree; "git diff --cached" compares a (c)ommit and the (i)ndex; "git-diff HEAD:file1 file2" compares an (o)bject and a (w)ork tree entity; "git diff --no-index a b" compares two non-git things (1) and (2). Because these mnemonics now have meanings, they are swapped when reverse diff is in effect and this feature is enabled. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 53d1589 commit a5a818e

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
@@ -581,6 +581,22 @@ diff.external::
581581
you want to use an external diff program only on a subset of
582582
your files, you might want to use linkgit:gitattributes[5] instead.
583583

584+
diff.mnemonicprefix::
585+
If set, 'git-diff' uses a prefix pair that is different from the
586+
standard "a/" and "b/" depending on what is being compared. When
587+
this configuration is in effect, reverse diff output also swaps
588+
the order of the prefixes:
589+
'git-diff';;
590+
compares the (i)ndex and the (w)ork tree;
591+
'git-diff HEAD';;
592+
compares a (c)ommit and the (w)ork tree;
593+
'git diff --cached';;
594+
compares a (c)ommit and the (i)ndex;
595+
'git-diff HEAD:file1 file2';;
596+
compares an (o)bject and a (w)ork tree entity;
597+
'git diff --no-index a b';;
598+
compares two non-git things (1) and (2).
599+
584600
diff.renameLimit::
585601
The number of files to consider when performing the copy/rename
586602
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
@@ -675,9 +675,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
675675
int i, show_hunks;
676676
int working_tree_file = is_null_sha1(elem->sha1);
677677
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
678+
const char *a_prefix, *b_prefix;
678679
mmfile_t result_file;
679680

680681
context = opt->context;
682+
a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
683+
b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
684+
681685
/* Read the result of merge first */
682686
if (!working_tree_file)
683687
result = grab_blob(elem->sha1, &result_size);
@@ -853,13 +857,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
853857
dump_quoted_path("--- ", "", "/dev/null",
854858
c_meta, c_reset);
855859
else
856-
dump_quoted_path("--- ", opt->a_prefix, elem->path,
860+
dump_quoted_path("--- ", a_prefix, elem->path,
857861
c_meta, c_reset);
858862
if (deleted)
859863
dump_quoted_path("+++ ", "", "/dev/null",
860864
c_meta, c_reset);
861865
else
862-
dump_quoted_path("+++ ", opt->b_prefix, elem->path,
866+
dump_quoted_path("+++ ", b_prefix, elem->path,
863867
c_meta, c_reset);
864868
dump_sline(sline, cnt, num_parent,
865869
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
@@ -23,6 +23,7 @@ static int diff_rename_limit_default = 200;
2323
int diff_use_color_default = -1;
2424
static const char *external_diff_cmd_cfg;
2525
int diff_auto_refresh_index = 1;
26+
static int diff_mnemonic_prefix;
2627

2728
static char diff_colors[][COLOR_MAXLEN] = {
2829
"\033[m", /* reset */
@@ -149,6 +150,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
149150
diff_auto_refresh_index = git_config_bool(var, value);
150151
return 0;
151152
}
153+
if (!strcmp(var, "diff.mnemonicprefix")) {
154+
diff_mnemonic_prefix = git_config_bool(var, value);
155+
return 0;
156+
}
152157
if (!strcmp(var, "diff.external"))
153158
return git_config_string(&external_diff_cmd_cfg, var, value);
154159
if (!prefixcmp(var, "diff.")) {
@@ -305,6 +310,15 @@ static void emit_rewrite_diff(const char *name_a,
305310
const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
306311
const char *reset = diff_get_color(color_diff, DIFF_RESET);
307312
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
313+
const char *a_prefix, *b_prefix;
314+
315+
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
316+
a_prefix = o->b_prefix;
317+
b_prefix = o->a_prefix;
318+
} else {
319+
a_prefix = o->a_prefix;
320+
b_prefix = o->b_prefix;
321+
}
308322

309323
name_a += (*name_a == '/');
310324
name_b += (*name_b == '/');
@@ -313,8 +327,8 @@ static void emit_rewrite_diff(const char *name_a,
313327

314328
strbuf_reset(&a_name);
315329
strbuf_reset(&b_name);
316-
quote_two_c_style(&a_name, o->a_prefix, name_a, 0);
317-
quote_two_c_style(&b_name, o->b_prefix, name_b, 0);
330+
quote_two_c_style(&a_name, a_prefix, name_a, 0);
331+
quote_two_c_style(&b_name, b_prefix, name_b, 0);
318332

319333
diff_populate_filespec(one, 0);
320334
diff_populate_filespec(two, 0);
@@ -1432,6 +1446,14 @@ static const char *diff_funcname_pattern(struct diff_filespec *one)
14321446
return NULL;
14331447
}
14341448

1449+
void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
1450+
{
1451+
if (!options->a_prefix)
1452+
options->a_prefix = a;
1453+
if (!options->b_prefix)
1454+
options->b_prefix = b;
1455+
}
1456+
14351457
static void builtin_diff(const char *name_a,
14361458
const char *name_b,
14371459
struct diff_filespec *one,
@@ -1445,9 +1467,19 @@ static void builtin_diff(const char *name_a,
14451467
char *a_one, *b_two;
14461468
const char *set = diff_get_color_opt(o, DIFF_METAINFO);
14471469
const char *reset = diff_get_color_opt(o, DIFF_RESET);
1470+
const char *a_prefix, *b_prefix;
1471+
1472+
diff_set_mnemonic_prefix(o, "a/", "b/");
1473+
if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
1474+
a_prefix = o->b_prefix;
1475+
b_prefix = o->a_prefix;
1476+
} else {
1477+
a_prefix = o->a_prefix;
1478+
b_prefix = o->b_prefix;
1479+
}
14481480

1449-
a_one = quote_two(o->a_prefix, name_a + (*name_a == '/'));
1450-
b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
1481+
a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
1482+
b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
14511483
lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
14521484
lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
14531485
fprintf(o->file, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
@@ -2308,8 +2340,10 @@ void diff_setup(struct diff_options *options)
23082340
DIFF_OPT_CLR(options, COLOR_DIFF);
23092341
options->detect_rename = diff_detect_rename_default;
23102342

2311-
options->a_prefix = "a/";
2312-
options->b_prefix = "b/";
2343+
if (!diff_mnemonic_prefix) {
2344+
options->a_prefix = "a/";
2345+
options->b_prefix = "b/";
2346+
}
23132347
}
23142348

23152349
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)