Skip to content

Commit 7fdc265

Browse files
whotgitster
authored andcommitted
diff: add diff.srcPrefix and diff.dstPrefix configuration variables
Allow the default prefixes "a/" and "b/" to be tweaked by the diff.srcPrefix and diff.dstPrefix configuration variables. Signed-off-by: Peter Hutterer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f9b731 commit 7fdc265

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Documentation/config/diff.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ diff.mnemonicPrefix::
111111
diff.noprefix::
112112
If set, 'git diff' does not show any source or destination prefix.
113113

114+
diff.srcPrefix::
115+
If set, 'git diff' uses this source prefix. Defaults to "a/".
116+
117+
diff.dstPrefix::
118+
If set, 'git diff' uses this destination prefix. Defaults to "b/".
119+
114120
diff.relative::
115121
If set to 'true', 'git diff' does not show changes outside of the directory
116122
and show pathnames relative to the current directory.

Documentation/diff-options.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,9 @@ endif::git-format-patch[]
865865

866866
--default-prefix::
867867
Use the default source and destination prefixes ("a/" and "b/").
868-
This is usually the default already, but may be used to override
869-
config such as `diff.noprefix`.
868+
This overrides configuration variables such as `diff.noprefix`,
869+
`diff.srcPrefix`, `diff.dstPrefix`, and `diff.mnemonicPrefix`
870+
(see `git-config`(1)).
870871

871872
--line-prefix=<prefix>::
872873
Prepend an additional prefix to every line of output.

diff.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static const char *diff_order_file_cfg;
6262
int diff_auto_refresh_index = 1;
6363
static int diff_mnemonic_prefix;
6464
static int diff_no_prefix;
65+
static const char *diff_src_prefix = "a/";
66+
static const char *diff_dst_prefix = "b/";
6567
static int diff_relative;
6668
static int diff_stat_name_width;
6769
static int diff_stat_graph_width;
@@ -408,6 +410,12 @@ int git_diff_ui_config(const char *var, const char *value,
408410
diff_no_prefix = git_config_bool(var, value);
409411
return 0;
410412
}
413+
if (!strcmp(var, "diff.srcprefix")) {
414+
return git_config_string(&diff_src_prefix, var, value);
415+
}
416+
if (!strcmp(var, "diff.dstprefix")) {
417+
return git_config_string(&diff_dst_prefix, var, value);
418+
}
411419
if (!strcmp(var, "diff.relative")) {
412420
diff_relative = git_config_bool(var, value);
413421
return 0;
@@ -3425,8 +3433,8 @@ void diff_set_noprefix(struct diff_options *options)
34253433

34263434
void diff_set_default_prefix(struct diff_options *options)
34273435
{
3428-
options->a_prefix = "a/";
3429-
options->b_prefix = "b/";
3436+
options->a_prefix = diff_src_prefix;
3437+
options->b_prefix = diff_dst_prefix;
34303438
}
34313439

34323440
struct userdiff_driver *get_textconv(struct repository *r,
@@ -5362,6 +5370,8 @@ static int diff_opt_default_prefix(const struct option *opt,
53625370

53635371
BUG_ON_OPT_NEG(unset);
53645372
BUG_ON_OPT_ARG(optarg);
5373+
diff_src_prefix = "a/";
5374+
diff_dst_prefix = "b/";
53655375
diff_set_default_prefix(options);
53665376
return 0;
53675377
}

t/t4013-diff-various.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,41 @@ test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
663663
check_prefix actual a/file0 b/file0
664664
'
665665

666+
test_expect_success 'diff respects diff.srcprefix' '
667+
git -c diff.srcprefix=x/ diff >actual &&
668+
check_prefix actual x/file0 b/file0
669+
'
670+
671+
test_expect_success 'diff respects diff.dstprefix' '
672+
git -c diff.dstprefix=y/ diff >actual &&
673+
check_prefix actual a/file0 y/file0
674+
'
675+
676+
test_expect_success 'diff --src-prefix overrides diff.srcprefix' '
677+
git -c diff.srcprefix=y/ diff --src-prefix=z/ >actual &&
678+
check_prefix actual z/file0 b/file0
679+
'
680+
681+
test_expect_success 'diff --dst-prefix overrides diff.dstprefix' '
682+
git -c diff.dstprefix=y/ diff --dst-prefix=z/ >actual &&
683+
check_prefix actual a/file0 z/file0
684+
'
685+
686+
test_expect_success 'diff.{src,dst}prefix ignored with diff.noprefix' '
687+
git -c diff.dstprefix=y/ -c diff.srcprefix=x/ -c diff.noprefix diff >actual &&
688+
check_prefix actual file0 file0
689+
'
690+
691+
test_expect_success 'diff.{src,dst}prefix ignored with diff.mnemonicprefix' '
692+
git -c diff.dstprefix=x/ -c diff.srcprefix=y/ -c diff.mnemonicprefix diff >actual &&
693+
check_prefix actual i/file0 w/file0
694+
'
695+
696+
test_expect_success 'diff.{src,dst}prefix ignored with --default-prefix' '
697+
git -c diff.dstprefix=x/ -c diff.srcprefix=y/ diff --default-prefix >actual &&
698+
check_prefix actual a/file0 b/file0
699+
'
700+
666701
test_expect_success 'diff --no-renames cannot be abbreviated' '
667702
test_expect_code 129 git diff --no-rename >actual 2>error &&
668703
test_must_be_empty actual &&

0 commit comments

Comments
 (0)