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

[PATCH] Add format-patch option --no-name-prefix.

From
Pascal Obry <pascal.obry@gmail.com>
Date
Dec 18, 2007, 15:42 UTC
Message-ID
<1197992574-3464-1-git-send-email-pascal@obry.net>

This option can be used to generate a patch file where file names are relative to the Git root directory. Such a patch can then be applied with the standard patch tool using option -p0.

Signed-off-by: Pascal Obry <pascal@obry.net>
---
 Documentation/git-format-patch.txt |    6 +++++-
 builtin-log.c                      |    7 ++++++-
 diff.c                             |   10 ++++++++--
 diff.h                             |    1 +
 4 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index 6fb9429..5a642ad 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -15,7 +15,7 @@ SYNOPSIS
                    [-n | --numbered | -N | --no-numbered]
                    [--start-number <n>] [--numbered-files]
                    [--in-reply-to=Message-Id] [--suffix=.<sfx>]
-                   [--ignore-if-in-upstream]
+                   [--ignore-if-in-upstream] [--no-name-prefix]
                    [--subject-prefix=Subject-Prefix]
 		   [ <since> | <revision range> ]
 
@@ -90,6 +90,10 @@ include::diff-options.txt[]
 	without the default first line of the commit appended.
 	Mutually exclusive with the --stdout option.
 
+--no-name-prefix::
+	Generate a patch file that can be applied with a patch(1) -p0
+	from the Git root directory.
+
 -k|--keep-subject::
 	Do not strip/add '[PATCH]' from the first line of the
 	commit log message.
diff --git a/builtin-log.c b/builtin-log.c
index cc3cc90..36582bd 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -599,6 +599,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	int subject_prefix = 0;
 	int ignore_if_in_upstream = 0;
 	int thread = 0;
+	int no_name_prefix = 0;
 	const char *in_reply_to = NULL;
 	struct patch_ids ids;
 	char *add_signoff = NULL;
@@ -636,6 +637,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		}
 		else if (!prefixcmp(argv[i], "--start-number="))
 			start_number = strtol(argv[i] + 15, NULL, 10);
+		else if (!prefixcmp(argv[i], "--no-name-prefix"))
+			no_name_prefix = 1;
 		else if (!strcmp(argv[i], "--numbered-files"))
 			numbered_files = 1;
 		else if (!strcmp(argv[i], "--start-number")) {
@@ -719,8 +722,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		die ("unrecognized argument: %s", argv[1]);
 
 	if (!rev.diffopt.output_format)
-		rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
+		rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH | DIFF_FORMAT_NAME_PREFIX;
 
+	if (no_name_prefix)
+		rev.diffopt.output_format &= ~DIFF_FORMAT_NAME_PREFIX;
 	if (!DIFF_OPT_TST(&rev.diffopt, TEXT))
 		DIFF_OPT_SET(&rev.diffopt, BINARY);
 
diff --git a/diff.c b/diff.c
index e26584c..f07d9c0 100644
--- a/diff.c
+++ b/diff.c
@@ -1212,8 +1212,14 @@ static void builtin_diff(const char *name_a,
 	const char *set = diff_get_color_opt(o, DIFF_METAINFO);
 	const char *reset = diff_get_color_opt(o, DIFF_RESET);
 
-	a_one = quote_two("a/", name_a + (*name_a == '/'));
-	b_two = quote_two("b/", name_b + (*name_b == '/'));
+	if (o->output_format & DIFF_FORMAT_NAME_PREFIX) {
+		a_one = quote_two("a/", name_a + (*name_a == '/'));
+		b_two = quote_two("b/", name_b + (*name_b == '/'));
+	}
+	else {
+		a_one = quote_two("", name_a + (*name_a == '/'));
+		b_two = quote_two("", name_b + (*name_b == '/'));
+	}
 	lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
 	lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
 	printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
diff --git a/diff.h b/diff.h
index 7e8000a..86458a3 100644
--- a/diff.h
+++ b/diff.h
@@ -30,6 +30,7 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
 #define DIFF_FORMAT_SUMMARY	0x0008
 #define DIFF_FORMAT_PATCH	0x0010
 #define DIFF_FORMAT_SHORTSTAT	0x0020
+#define DIFF_FORMAT_NAME_PREFIX 0x0040
 
 /* These override all above */
 #define DIFF_FORMAT_NAME	0x0100
-- 
1.5.4.rc0.56.g6fbe
Next: Pascal Obry
Message 1 of 22 in “Add format-patch option --no-name-prefix.”
  1. Add format-patch option --no-name-prefix.Pascal Obry, Dec 18, 2007
  2. Pascal ObryDec 18, 2007
  3. Johannes SixtDec 18, 2007
  4. Pascal ObryDec 18, 2007
  5. Linus TorvaldsDec 18, 2007
  6. Pascal ObryDec 18, 2007
  7. Johannes SchindelinDec 18, 2007
  8. Pascal ObryDec 18, 2007
  9. Teach diff machinery to display other prefixes than "a/" and "b/"Johannes Schindelin, Dec 18, 2007
  10. Pascal ObryDec 18, 2007
  11. Linus TorvaldsDec 18, 2007
  12. Teach diff machinery to display other prefixes than "a/" and "b/"Johannes Schindelin, Dec 18, 2007
  13. Linus TorvaldsDec 18, 2007
  14. Junio C HamanoDec 18, 2007
  15. Andreas EricssonDec 18, 2007
  16. Pascal ObryDec 18, 2007
  17. Andreas EricssonDec 19, 2007
  18. Johannes SixtDec 19, 2007
  19. Teach diff machinery to display other prefixes than "a/" and "b/"Johannes Schindelin, Dec 18, 2007
  20. Pascal ObryDec 18, 2007
  21. Johannes SchindelinDec 18, 2007
  22. Matthieu MoyDec 18, 2007

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.