git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2] git apply: apply patches with -pN (N>1) properly for some cases.
Date: Thu, 25 Nov 2010 10:58:43 +0800	[thread overview]
Message-ID: <4CEDD0E3.6050905@gmail.com> (raw)

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

                 reply	other threads:[~2010-11-25  2:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4CEDD0E3.6050905@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).