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>
This commit is contained in:
Peter Hutterer 2024-03-15 11:03:10 +10:00 committed by Junio C Hamano
parent 4f9b731bde
commit 7fdc265633
4 changed files with 56 additions and 4 deletions

View File

@ -111,6 +111,12 @@ diff.mnemonicPrefix::
diff.noprefix:: diff.noprefix::
If set, 'git diff' does not show any source or destination prefix. If set, 'git diff' does not show any source or destination prefix.
diff.srcPrefix::
If set, 'git diff' uses this source prefix. Defaults to "a/".
diff.dstPrefix::
If set, 'git diff' uses this destination prefix. Defaults to "b/".
diff.relative:: diff.relative::
If set to 'true', 'git diff' does not show changes outside of the directory If set to 'true', 'git diff' does not show changes outside of the directory
and show pathnames relative to the current directory. and show pathnames relative to the current directory.

View File

@ -865,8 +865,9 @@ endif::git-format-patch[]
--default-prefix:: --default-prefix::
Use the default source and destination prefixes ("a/" and "b/"). Use the default source and destination prefixes ("a/" and "b/").
This is usually the default already, but may be used to override This overrides configuration variables such as `diff.noprefix`,
config such as `diff.noprefix`. `diff.srcPrefix`, `diff.dstPrefix`, and `diff.mnemonicPrefix`
(see `git-config`(1)).
--line-prefix=<prefix>:: --line-prefix=<prefix>::
Prepend an additional prefix to every line of output. Prepend an additional prefix to every line of output.

14
diff.c
View File

@ -62,6 +62,8 @@ static const char *diff_order_file_cfg;
int diff_auto_refresh_index = 1; int diff_auto_refresh_index = 1;
static int diff_mnemonic_prefix; static int diff_mnemonic_prefix;
static int diff_no_prefix; static int diff_no_prefix;
static const char *diff_src_prefix = "a/";
static const char *diff_dst_prefix = "b/";
static int diff_relative; static int diff_relative;
static int diff_stat_name_width; static int diff_stat_name_width;
static int diff_stat_graph_width; static int diff_stat_graph_width;
@ -408,6 +410,12 @@ int git_diff_ui_config(const char *var, const char *value,
diff_no_prefix = git_config_bool(var, value); diff_no_prefix = git_config_bool(var, value);
return 0; return 0;
} }
if (!strcmp(var, "diff.srcprefix")) {
return git_config_string(&diff_src_prefix, var, value);
}
if (!strcmp(var, "diff.dstprefix")) {
return git_config_string(&diff_dst_prefix, var, value);
}
if (!strcmp(var, "diff.relative")) { if (!strcmp(var, "diff.relative")) {
diff_relative = git_config_bool(var, value); diff_relative = git_config_bool(var, value);
return 0; return 0;
@ -3425,8 +3433,8 @@ void diff_set_noprefix(struct diff_options *options)
void diff_set_default_prefix(struct diff_options *options) void diff_set_default_prefix(struct diff_options *options)
{ {
options->a_prefix = "a/"; options->a_prefix = diff_src_prefix;
options->b_prefix = "b/"; options->b_prefix = diff_dst_prefix;
} }
struct userdiff_driver *get_textconv(struct repository *r, struct userdiff_driver *get_textconv(struct repository *r,
@ -5362,6 +5370,8 @@ static int diff_opt_default_prefix(const struct option *opt,
BUG_ON_OPT_NEG(unset); BUG_ON_OPT_NEG(unset);
BUG_ON_OPT_ARG(optarg); BUG_ON_OPT_ARG(optarg);
diff_src_prefix = "a/";
diff_dst_prefix = "b/";
diff_set_default_prefix(options); diff_set_default_prefix(options);
return 0; return 0;
} }

View File

@ -663,6 +663,41 @@ test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
check_prefix actual a/file0 b/file0 check_prefix actual a/file0 b/file0
' '
test_expect_success 'diff respects diff.srcprefix' '
git -c diff.srcprefix=x/ diff >actual &&
check_prefix actual x/file0 b/file0
'
test_expect_success 'diff respects diff.dstprefix' '
git -c diff.dstprefix=y/ diff >actual &&
check_prefix actual a/file0 y/file0
'
test_expect_success 'diff --src-prefix overrides diff.srcprefix' '
git -c diff.srcprefix=y/ diff --src-prefix=z/ >actual &&
check_prefix actual z/file0 b/file0
'
test_expect_success 'diff --dst-prefix overrides diff.dstprefix' '
git -c diff.dstprefix=y/ diff --dst-prefix=z/ >actual &&
check_prefix actual a/file0 z/file0
'
test_expect_success 'diff.{src,dst}prefix ignored with diff.noprefix' '
git -c diff.dstprefix=y/ -c diff.srcprefix=x/ -c diff.noprefix diff >actual &&
check_prefix actual file0 file0
'
test_expect_success 'diff.{src,dst}prefix ignored with diff.mnemonicprefix' '
git -c diff.dstprefix=x/ -c diff.srcprefix=y/ -c diff.mnemonicprefix diff >actual &&
check_prefix actual i/file0 w/file0
'
test_expect_success 'diff.{src,dst}prefix ignored with --default-prefix' '
git -c diff.dstprefix=x/ -c diff.srcprefix=y/ diff --default-prefix >actual &&
check_prefix actual a/file0 b/file0
'
test_expect_success 'diff --no-renames cannot be abbreviated' ' test_expect_success 'diff --no-renames cannot be abbreviated' '
test_expect_code 129 git diff --no-rename >actual 2>error && test_expect_code 129 git diff --no-rename >actual 2>error &&
test_must_be_empty actual && test_must_be_empty actual &&