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

[PATCH v2] git apply: apply patches with -pN (N>1) properly for some cases.

From
Jiang Xin <worldhello.net@gmail.com>
Date
Nov 25, 2010, 02:58 UTC
Message-ID
<4CEDD0E3.6050905@gmail.com>

In some cases when apply patch, there maybe no '--- path/to/old' and '+++ path/to/new' lines in the patch file, and the only way to get filename is from the 'diff --git path/to/old path/to/new' line in the patch header. For example, differ of binary files, pure mode changes, removing or adding empty files.

The function 'git_header_name' is broken and casue patch apply failed with -pN (N>1) for these cases. This patch fixed it and provide a testcase. I also add a testcase for fancy filename.

Signed-off-by: Jiang Xin <jiangxin@ossxp.com>
---
 builtin/apply.c       |    3 ++-
 t/t4120-apply-popt.sh |   11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 23c18c5..d603e37 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1126,6 +1126,7 @@ static char *git_header_name(char *line, int llen)
 	 * form.
 	 */
 	for (len = 0 ; ; len++) {
+		int nslash = p_value;
 		switch (name[len]) {
 		default:
 			continue;
@@ -1137,7 +1138,7 @@ static char *git_header_name(char *line, int llen)
 				char c = *second++;
 				if (c == '\n')
 					return NULL;
-				if (c == '/')
+				if (c == '/' && --nslash <= 0)
 					break;
 			}
 			if (second[len] == '\n' && !memcmp(name, second, len)) {
diff --git a/t/t4120-apply-popt.sh b/t/t4120-apply-popt.sh
index 2b2d00b..8a1e80e 100755
--- a/t/t4120-apply-popt.sh
+++ b/t/t4120-apply-popt.sh
@@ -50,10 +50,21 @@ test_expect_success 'apply (-p2) traditional diff with funny filenames' '
 	test_cmp expected file1
 '
 
+test_expect_success 'apply git diff with -p2 and fancy filename' '
+	cp file1.saved file1 &&
+	git apply -p2 patch.escaped
+'
+
 test_expect_success 'apply with too large -p and fancy filename' '
 	cp file1.saved file1 &&
 	test_must_fail git apply --stat -p3 patch.escaped 2>err &&
 	grep "removing 3 leading" err
 '
 
+test_expect_success 'apply git diff with -p2 and use default name from header' '
+	sed -e "/^\(---\|+++\) / d" patch.file > patch.newheader &&
+	cp file1.saved file1 &&
+	git apply -p2 patch.newheader
+'
+
 test_done
-- 
1.7.3.2.245.g03276.dirty
Message 1 of 1 in “git apply: apply patches with -pN (N>1) properly for some cases.”
  1. git apply: apply patches with -pN (N>1) properly for some cases.Jiang Xin, Nov 25, 2010

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.