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 <peter.hutterer@who-t.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Peter Hutterer committed Mar 15, 2024 at 11:03 UTC 7fdc2656331f051a673a61444051cde58044ceeb
4 files changed +56 -4
Documentation/config/diff.txt
+6
@@ -111,6 +111,12 @@ diff.mnemonicPrefix::
111 diff.noprefix::
112 If set, 'git diff' does not show any source or destination prefix.
113
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 +
120 diff.relative::
121 If set to 'true', 'git diff' does not show changes outside of the directory
122 and show pathnames relative to the current directory.
Documentation/diff-options.txt
+3 -2
@@ -865,8 +865,9 @@ endif::git-format-patch[]
865
866 --default-prefix::
867 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)).
871
872 --line-prefix=<prefix>::
873 Prepend an additional prefix to every line of output.
diff.c
+12 -2
@@ -62,6 +62,8 @@ static const char *diff_order_file_cfg;
62 int diff_auto_refresh_index = 1;
63 static int diff_mnemonic_prefix;
64 static int diff_no_prefix;
65 +static const char *diff_src_prefix = "a/";
66 +static const char *diff_dst_prefix = "b/";
67 static int diff_relative;
68 static int diff_stat_name_width;
69 static int diff_stat_graph_width;
@@ -408,6 +410,12 @@ int git_diff_ui_config(const char *var, const char *value,
410 diff_no_prefix = git_config_bool(var, value);
411 return 0;
412 }
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 + }
419 if (!strcmp(var, "diff.relative")) {
420 diff_relative = git_config_bool(var, value);
421 return 0;
@@ -3425,8 +3433,8 @@ void diff_set_noprefix(struct diff_options *options)
3433
3434 void diff_set_default_prefix(struct diff_options *options)
3435 {
3428 - options->a_prefix = "a/";
3429 - options->b_prefix = "b/";
3436 + options->a_prefix = diff_src_prefix;
3437 + options->b_prefix = diff_dst_prefix;
3438 }
3439
3440 struct userdiff_driver *get_textconv(struct repository *r,
@@ -5362,6 +5370,8 @@ static int diff_opt_default_prefix(const struct option *opt,
5370
5371 BUG_ON_OPT_NEG(unset);
5372 BUG_ON_OPT_ARG(optarg);
5373 + diff_src_prefix = "a/";
5374 + diff_dst_prefix = "b/";
5375 diff_set_default_prefix(options);
5376 return 0;
5377 }
t/t4013-diff-various.sh
+35
@@ -663,6 +663,41 @@ test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
663 check_prefix actual a/file0 b/file0
664 '
665
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 +
701 test_expect_success 'diff --no-renames cannot be abbreviated' '
702 test_expect_code 129 git diff --no-rename >actual 2>error &&
703 test_must_be_empty actual &&