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

Jeff King committed Mar 9, 2023 at 01:09 UTC b39a5697296659c344cd0a286b51303fd5375fab
3 files changed +29
Documentation/diff-options.txt
+5
@@ -852,6 +852,11 @@ endif::git-format-patch[]
852 --no-prefix::
853 Do not show any source or destination prefix.
854
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 +
860 --line-prefix=<prefix>::
861 Prepend an additional prefix to every line of output.
862
diff.c
+14
@@ -5275,6 +5275,17 @@ static int diff_opt_no_prefix(const struct option *opt,
5275 return 0;
5276 }
5277
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 +
5289 static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5290 const struct option *opt,
5291 const char *arg, int unset)
@@ -5564,6 +5575,9 @@ struct option *add_diff_options(const struct option *opts,
5575 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5576 N_("do not show any source or destination prefix"),
5577 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),
5581 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5582 N_("show context between diff hunks up to the specified number of lines"),
5583 PARSE_OPT_NONEG),
t/t4013-diff-various.sh
+10
@@ -643,9 +643,19 @@ test_expect_success 'diff respects diff.noprefix' '
643 check_prefix actual file0 file0
644 '
645
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 +
651 test_expect_success 'diff respects diff.mnemonicprefix' '
652 git -c diff.mnemonicprefix diff >actual &&
653 check_prefix actual i/file0 w/file0
654 '
655
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 +
661 test_done