Skip to content

Commit b39a569

Browse files
peffgitster
authored andcommitted
diff: add --default-prefix option
You can change the output of prefixes with diff.noprefix and diff.mnemonicprefix, but there's no easy way to override them from the command-line. We do have "--no-prefix", but there's no way to get back to the default prefix. So let's add an option to do that. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c03d0d commit b39a569

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Documentation/diff-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,11 @@ endif::git-format-patch[]
852852
--no-prefix::
853853
Do not show any source or destination prefix.
854854

855+
--default-prefix::
856+
Use the default source and destination prefixes ("a/" and "b/").
857+
This is usually the default already, but may be used to override
858+
config such as `diff.noprefix`.
859+
855860
--line-prefix=<prefix>::
856861
Prepend an additional prefix to every line of output.
857862

diff.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5275,6 +5275,17 @@ static int diff_opt_no_prefix(const struct option *opt,
52755275
return 0;
52765276
}
52775277

5278+
static int diff_opt_default_prefix(const struct option *opt,
5279+
const char *optarg, int unset)
5280+
{
5281+
struct diff_options *options = opt->value;
5282+
5283+
BUG_ON_OPT_NEG(unset);
5284+
BUG_ON_OPT_ARG(optarg);
5285+
diff_set_default_prefix(options);
5286+
return 0;
5287+
}
5288+
52785289
static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
52795290
const struct option *opt,
52805291
const char *arg, int unset)
@@ -5564,6 +5575,9 @@ struct option *add_diff_options(const struct option *opts,
55645575
OPT_CALLBACK_F(0, "no-prefix", options, NULL,
55655576
N_("do not show any source or destination prefix"),
55665577
PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
5578+
OPT_CALLBACK_F(0, "default-prefix", options, NULL,
5579+
N_("use default prefixes a/ and b/"),
5580+
PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_default_prefix),
55675581
OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
55685582
N_("show context between diff hunks up to the specified number of lines"),
55695583
PARSE_OPT_NONEG),

t/t4013-diff-various.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,19 @@ test_expect_success 'diff respects diff.noprefix' '
643643
check_prefix actual file0 file0
644644
'
645645

646+
test_expect_success 'diff --default-prefix overrides diff.noprefix' '
647+
git -c diff.noprefix diff --default-prefix >actual &&
648+
check_prefix actual a/file0 b/file0
649+
'
650+
646651
test_expect_success 'diff respects diff.mnemonicprefix' '
647652
git -c diff.mnemonicprefix diff >actual &&
648653
check_prefix actual i/file0 w/file0
649654
'
650655

656+
test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
657+
git -c diff.mnemonicprefix diff --default-prefix >actual &&
658+
check_prefix actual a/file0 b/file0
659+
'
660+
651661
test_done

0 commit comments

Comments
 (0)