@matheusbsbs / matheus-bagliano / commits / a5a818ee48
Fork 0

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>

Junio C Hamano committed Aug 18, 2008 at 20:08 UTC a5a818ee4877e4458e8e6741a03ac3b19941d58a
7 files changed +70 -8
Documentation/config.txt
+16
@@ -581,6 +581,22 @@ diff.external::
581 you want to use an external diff program only on a subset of
582 your files, you might want to use linkgit:gitattributes[5] instead.
583
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 +
600 diff.renameLimit::
601 The number of files to consider when performing the copy/rename
602 detection; equivalent to the 'git-diff' option '-l'.
builtin-diff.c
+2
@@ -74,6 +74,8 @@ static int builtin_diff_b_f(struct rev_info *revs,
74 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
75 die("'%s': not a regular file or symlink", path);
76
77 + diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
78 +
79 if (blob[0].mode == S_IFINVALID)
80 blob[0].mode = canon_mode(st.st_mode);
81
combine-diff.c
+6 -2
@@ -675,9 +675,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
675 int i, show_hunks;
676 int working_tree_file = is_null_sha1(elem->sha1);
677 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
678 + const char *a_prefix, *b_prefix;
679 mmfile_t result_file;
680
681 context = opt->context;
682 + a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
683 + b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
684 +
685 /* Read the result of merge first */
686 if (!working_tree_file)
687 result = grab_blob(elem->sha1, &result_size);
@@ -853,13 +857,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
857 dump_quoted_path("--- ", "", "/dev/null",
858 c_meta, c_reset);
859 else
856 - dump_quoted_path("--- ", opt->a_prefix, elem->path,
860 + dump_quoted_path("--- ", a_prefix, elem->path,
861 c_meta, c_reset);
862 if (deleted)
863 dump_quoted_path("+++ ", "", "/dev/null",
864 c_meta, c_reset);
865 else
862 - dump_quoted_path("+++ ", opt->b_prefix, elem->path,
866 + dump_quoted_path("+++ ", b_prefix, elem->path,
867 c_meta, c_reset);
868 dump_sline(sline, cnt, num_parent,
869 DIFF_OPT_TST(opt, COLOR_DIFF));
diff-lib.c
+3
@@ -63,6 +63,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
63 ? CE_MATCH_RACY_IS_DIRTY : 0);
64 char symcache[PATH_MAX];
65
66 + diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
67 +
68 if (diff_unmerged_stage < 0)
69 diff_unmerged_stage = 2;
70 entries = active_nr;
@@ -469,6 +471,7 @@ int run_diff_index(struct rev_info *revs, int cached)
471 if (unpack_trees(1, &t, &opts))
472 exit(128);
473
474 + diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
475 diffcore_std(&revs->diffopt);
476 diff_flush(&revs->diffopt);
477 return 0;
diff-no-index.c
+1
@@ -252,6 +252,7 @@ void diff_no_index(struct rev_info *revs,
252 if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
253 revs->diffopt.paths[1]))
254 exit(1);
255 + diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
256 diffcore_std(&revs->diffopt);
257 diff_flush(&revs->diffopt);
258
diff.c
+40 -6
@@ -23,6 +23,7 @@ static int diff_rename_limit_default = 200;
23 int diff_use_color_default = -1;
24 static const char *external_diff_cmd_cfg;
25 int diff_auto_refresh_index = 1;
26 +static int diff_mnemonic_prefix;
27
28 static char diff_colors[][COLOR_MAXLEN] = {
29 "\033[m", /* reset */
@@ -149,6 +150,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
150 diff_auto_refresh_index = git_config_bool(var, value);
151 return 0;
152 }
153 + if (!strcmp(var, "diff.mnemonicprefix")) {
154 + diff_mnemonic_prefix = git_config_bool(var, value);
155 + return 0;
156 + }
157 if (!strcmp(var, "diff.external"))
158 return git_config_string(&external_diff_cmd_cfg, var, value);
159 if (!prefixcmp(var, "diff.")) {
@@ -305,6 +310,15 @@ static void emit_rewrite_diff(const char *name_a,
310 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
311 const char *reset = diff_get_color(color_diff, DIFF_RESET);
312 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 + }
322
323 name_a += (*name_a == '/');
324 name_b += (*name_b == '/');
@@ -313,8 +327,8 @@ static void emit_rewrite_diff(const char *name_a,
327
328 strbuf_reset(&a_name);
329 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);
332
333 diff_populate_filespec(one, 0);
334 diff_populate_filespec(two, 0);
@@ -1432,6 +1446,14 @@ static const char *diff_funcname_pattern(struct diff_filespec *one)
1446 return NULL;
1447 }
1448
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 +
1457 static void builtin_diff(const char *name_a,
1458 const char *name_b,
1459 struct diff_filespec *one,
@@ -1445,9 +1467,19 @@ static void builtin_diff(const char *name_a,
1467 char *a_one, *b_two;
1468 const char *set = diff_get_color_opt(o, DIFF_METAINFO);
1469 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 + }
1480
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 == '/'));
1483 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1484 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1485 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)
2340 DIFF_OPT_CLR(options, COLOR_DIFF);
2341 options->detect_rename = diff_detect_rename_default;
2342
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 + }
2347 }
2348
2349 int diff_setup_done(struct diff_options *options)
diff.h
+2
@@ -160,6 +160,8 @@ extern void diff_tree_combined(const unsigned char *sha1, const unsigned char pa
160
161 extern void diff_tree_combined_merge(const unsigned char *sha1, int, struct rev_info *);
162
163 +void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b);
164 +
165 extern void diff_addremove(struct diff_options *,
166 int addremove,
167 unsigned mode,