Merge branch 'ph/diff-src-dst-prefix-config'

"git diff" and friends learned two extra configuration variables,
diff.srcPrefix and diff.dstPrefix.

* ph/diff-src-dst-prefix-config:
  diff.*Prefix: use camelCase in the doc and test titles
  diff: add diff.srcPrefix and diff.dstPrefix configuration variables
This commit is contained in:
Junio C Hamano 2024-03-28 14:13:50 -07:00
commit 396430b5a7
4 changed files with 67 additions and 15 deletions

View File

@ -108,9 +108,15 @@ diff.mnemonicPrefix::
`git diff --no-index a b`;; `git diff --no-index a b`;;
compares two non-git things (1) and (2). compares two non-git things (1) and (2).
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

@ -633,8 +633,8 @@ check_prefix () {
test_cmp expect actual.paths test_cmp expect actual.paths
} }
test_expect_success 'diff-files does not respect diff.noprefix' ' test_expect_success 'diff-files does not respect diff.noPrefix' '
git -c diff.noprefix diff-files -p >actual && git -c diff.noPrefix diff-files -p >actual &&
check_prefix actual a/file0 b/file0 check_prefix actual a/file0 b/file0
' '
@ -643,23 +643,58 @@ test_expect_success 'diff-files respects --no-prefix' '
check_prefix actual file0 file0 check_prefix actual file0 file0
' '
test_expect_success 'diff respects diff.noprefix' ' test_expect_success 'diff respects diff.noPrefix' '
git -c diff.noprefix diff >actual && git -c diff.noPrefix diff >actual &&
check_prefix actual file0 file0 check_prefix actual file0 file0
' '
test_expect_success 'diff --default-prefix overrides diff.noprefix' ' test_expect_success 'diff --default-prefix overrides diff.noPrefix' '
git -c diff.noprefix diff --default-prefix >actual && git -c diff.noPrefix diff --default-prefix >actual &&
check_prefix actual a/file0 b/file0 check_prefix actual a/file0 b/file0
' '
test_expect_success 'diff respects diff.mnemonicprefix' ' test_expect_success 'diff respects diff.mnemonicPrefix' '
git -c diff.mnemonicprefix diff >actual && git -c diff.mnemonicPrefix diff >actual &&
check_prefix actual i/file0 w/file0 check_prefix actual i/file0 w/file0
' '
test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' ' test_expect_success 'diff --default-prefix overrides diff.mnemonicPrefix' '
git -c diff.mnemonicprefix diff --default-prefix >actual && git -c diff.mnemonicPrefix diff --default-prefix >actual &&
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 check_prefix actual a/file0 b/file0
' '