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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]