git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/5] diff: add --default-prefix option

From
Jeff King <peff@peff.net>
Date
Mar 9, 2023, 06:09 UTC
Message-ID
<ZAl4MkWVV8fr+3fO@coredump.intra.peff.net>
In-Reply-To
<ZAl3bHB9zxjLITgf@coredump.intra.peff.net>

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>
---
This isn't strictly necessary for the series, but it seemed like a gap.
You can always do:
  git -c diff.noprefix=false -c diff.mnemonicprefix=false ...
but that's rather a mouthful.

Note that there isn't a command-line equivalent for mnemonicprefix, either. I don't think it's worth adding unless somebody really wants it.

 Documentation/diff-options.txt |  5 +++++
 diff.c                         | 14 ++++++++++++++
 t/t4013-diff-various.sh        | 10 ++++++++++
 3 files changed, 29 insertions(+)
diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index 7d73e976d99..08ab86189a7 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -852,6 +852,11 @@ endif::git-format-patch[]
 --no-prefix::
 	Do not show any source or destination prefix.
 
+--default-prefix::
+	Use the default source and destination prefixes ("a/" and "b/").
+	This is usually the default already, but may be used to override
+	config such as `diff.noprefix`.
+
 --line-prefix=<prefix>::
 	Prepend an additional prefix to every line of output.
 
diff --git a/diff.c b/diff.c
index 750d1b1a6c3..b322e319ff3 100644
--- a/diff.c
+++ b/diff.c
@@ -5275,6 +5275,17 @@ static int diff_opt_no_prefix(const struct option *opt,
 	return 0;
 }
 
+static int diff_opt_default_prefix(const struct option *opt,
+				   const char *optarg, int unset)
+{
+	struct diff_options *options = opt->value;
+
+	BUG_ON_OPT_NEG(unset);
+	BUG_ON_OPT_ARG(optarg);
+	diff_set_default_prefix(options);
+	return 0;
+}
+
 static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
 					     const struct option *opt,
 					     const char *arg, int unset)
@@ -5564,6 +5575,9 @@ struct option *add_diff_options(const struct option *opts,
 		OPT_CALLBACK_F(0, "no-prefix", options, NULL,
 			       N_("do not show any source or destination prefix"),
 			       PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
+		OPT_CALLBACK_F(0, "default-prefix", options, NULL,
+			       N_("use default prefixes a/ and b/"),
+			       PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_default_prefix),
 		OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
 			      N_("show context between diff hunks up to the specified number of lines"),
 			      PARSE_OPT_NONEG),
diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh
index 0bc69579898..5de1d190759 100755
--- a/t/t4013-diff-various.sh
+++ b/t/t4013-diff-various.sh
@@ -643,9 +643,19 @@ test_expect_success 'diff respects diff.noprefix' '
 	check_prefix actual file0 file0
 '
 
+test_expect_success 'diff --default-prefix overrides diff.noprefix' '
+	git -c diff.noprefix diff --default-prefix >actual &&
+	check_prefix actual a/file0 b/file0
+'
+
 test_expect_success 'diff respects diff.mnemonicprefix' '
 	git -c diff.mnemonicprefix diff >actual &&
 	check_prefix actual i/file0 w/file0
 '
 
+test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
+	git -c diff.mnemonicprefix diff --default-prefix >actual &&
+	check_prefix actual a/file0 b/file0
+'
+
 test_done
-- 
2.40.0.rc2.537.g928a61c97db
Previous: Jeff KingNext: Alejandro Colomar
Message 7 of 30 in “Better suggestions when git-am(1) fails”
  1. Alejandro ColomarMar 8, 2023
  2. Jeff KingMar 9, 2023
  3. Jeff KingMar 9, 2023
  4. 1/5 diff: factor out src/dst prefix setupJeff King, Mar 9, 2023
  5. Alejandro ColomarMar 9, 2023
  6. 2/5 t4013: add tests for diff prefix optionsJeff King, Mar 9, 2023
  7. 3/5 diff: add --default-prefix optionJeff King, Mar 9, 2023
  8. Alejandro ColomarMar 9, 2023
  9. Junio C HamanoMar 9, 2023
  10. Jeff KingMar 10, 2023
  11. Junio C HamanoMar 10, 2023
  12. Jeff KingMar 13, 2023
  13. Junio C HamanoMar 13, 2023
  14. Junio C HamanoMar 13, 2023
  15. Jeff KingMar 13, 2023
  16. 4/5 format-patch: do not respect diff.noprefixJeff King, Mar 9, 2023
  17. Alejandro ColomarMar 9, 2023
  18. Junio C HamanoMar 9, 2023
  19. Jeff KingMar 10, 2023
  20. 5/5 format-patch: add format.noprefix optionJeff King, Mar 9, 2023
  21. Junio C HamanoMar 9, 2023
  22. Jeff KingMar 10, 2023
  23. Alejandro ColomarMar 9, 2023
  24. Junio C HamanoMar 9, 2023
  25. Jeff KingMar 10, 2023
  26. Junio C HamanoMar 9, 2023
  27. Jeff KingMar 10, 2023
  28. Junio C HamanoMar 10, 2023
  29. Jeff KingMar 13, 2023
  30. Junio C HamanoMar 13, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.