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

[PATCH 4/5] format-patch: do not respect diff.noprefix

From
Jeff King <peff@peff.net>
Date
Mar 9, 2023, 06:11 UTC
Message-ID
<ZAl4pZV08a6Bgoip@coredump.intra.peff.net>
In-Reply-To
<ZAl3bHB9zxjLITgf@coredump.intra.peff.net>

The output of format-patch respects diff.noprefix, but this usually ends up being a hassle for people receiving the patch, as they have to manually specify "-p0" in order to apply it.

I don't think there was any specific intention for it to behave this way. The noprefix option is handled by git_diff_ui_config(), and format-patch exists in a gray area between plumbing and porcelain. People do look at the output, and we'd expect it to colorize things, respect their choice of algorithm, and so on. But this particular option creates problems for the receiver (in theory so does diff.mnemonicprefix, but since we are always formatting commits, the mnemonic prefixes will always be "a/" and "b/").

So let's disable it. The slight downsides are:
  - people who have set diff.noprefix presumably like to see their
    patches without prefixes. If they use format-patch to review their
    series, they'll see prefixes. On the other hand, it is probably a
    good idea for them to look at what will actually get sent out.
    We could try to play games here with "is stdout a tty", as we do for
    color. But that's not a completely reliable signal, and it's
    probably not worth the trouble. If you want to see the patch with
    the usual bells and whistles, then you are better off using "git
    log" or "git show".
  - if a project really does have a workflow that likes prefix-less
    patches, and the receiver is prepared to use "-p0", then the sender
    now has to manually say "--no-prefix" for each format-patch
    invocation. That doesn't seem _too_ terrible given that the receiver
    has to manually say "-p0" for each git-am invocation.
Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/log.c           | 9 +++++++++
 t/t4014-format-patch.sh | 5 +++++
 2 files changed, 14 insertions(+)
diff --git a/builtin/log.c b/builtin/log.c
index a70fba198f9..eaf511aab86 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1085,6 +1085,15 @@ static int git_format_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
+	/*
+	 * ignore some porcelain config which would otherwise be parsed by
+	 * git_diff_ui_config(), via git_log_config(); we can't just avoid
+	 * diff_ui_config completely, because we do care about some ui options
+	 * like color.
+	 */
+	if (!strcmp(var, "diff.noprefix"))
+		return 0;
+
 	return git_log_config(var, value, cb);
 }
 
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index f3313b8c58f..f5a41fd47ed 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -2386,4 +2386,9 @@ test_expect_success 'interdiff: solo-patch' '
 	test_cmp expect actual
 '
 
+test_expect_success 'format-patch does not respect diff.noprefix' '
+	git -c diff.noprefix format-patch -1 --stdout >actual &&
+	grep "^--- a/blorp" actual
+'
+
 test_done
-- 
2.40.0.rc2.537.g928a61c97db
Previous: Jeff KingNext: Alejandro Colomar
Message 16 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.