From 6a2abdc12516cd3801bceef4ccfba399c962a074 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 24 Aug 2012 22:48:55 -0700 Subject: apply: compute patch->def_name correctly under -p0 Back when "git apply" was written, we made sure that the user can skip more than the default number of path components (i.e. 1) by giving "-p", but the logic for doing so was built around the notion of "we skip N slashes and stop". This obviously does not work well when running under -p0 where we do not want to skip any, but still want to skip SP/HT that separates the pathnames of preimage and postimage and want to reject absolute pathnames. Stop using "stop_at_slash()", and instead introduce a new helper "skip_tree_prefix()" with similar logic but works correctly even for the -p0 case. This is an ancient bug, but has been masked for a long time because most of the patches are text and have other clues to tell us the name of the preimage and the postimage. Noticed by Colin McCabe. Signed-off-by: Junio C Hamano --- t/t4103-apply-binary.sh | 54 ++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 't/t4103-apply-binary.sh') diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh index dbbf56cba9..1b420e3b5f 100755 --- a/t/t4103-apply-binary.sh +++ b/t/t4103-apply-binary.sh @@ -8,30 +8,28 @@ test_description='git apply handling binary patches ' . ./test-lib.sh -# setup - -cat >file1 <file2 -cat file1 >file4 - -test_expect_success 'setup' " +test_expect_success 'setup' ' + cat >file1 <<-\EOF && + A quick brown fox jumps over the lazy dog. + A tiny little penguin runs around in circles. + There is a flag with Linux written on it. + A slow black-and-white panda just sits there, + munching on his bamboo. + EOF + cat file1 >file2 && + cat file1 >file4 && + git update-index --add --remove file1 file2 file4 && - git commit -m 'Initial Version' 2>/dev/null && + git commit -m "Initial Version" 2>/dev/null && git checkout -b binary && - perl -pe 'y/x/\000/' file3 && + perl -pe "y/x/\000/" file3 && cat file3 >file4 && git add file2 && - perl -pe 'y/\000/v/' file1 && + perl -pe "y/\000/v/" file1 && rm -f file2 && git update-index --add --remove file1 file2 file3 file4 && - git commit -m 'Second Version' && + git commit -m "Second Version" && git diff-tree -p master binary >B.diff && git diff-tree -p -C master binary >C.diff && @@ -42,17 +40,25 @@ test_expect_success 'setup' " git diff-tree -p --full-index master binary >B-index.diff && git diff-tree -p -C --full-index master binary >C-index.diff && + git diff-tree -p --binary --no-prefix master binary -- file3 >B0.diff && + git init other-repo && - (cd other-repo && - git fetch .. master && - git reset --hard FETCH_HEAD + ( + cd other-repo && + git fetch .. master && + git reset --hard FETCH_HEAD ) -" +' test_expect_success 'stat binary diff -- should not fail.' \ 'git checkout master && git apply --stat --summary B.diff' +test_expect_success 'stat binary -p0 diff -- should not fail.' ' + git checkout master && + git apply --stat -p0 B0.diff +' + test_expect_success 'stat binary diff (copy) -- should not fail.' \ 'git checkout master && git apply --stat --summary C.diff' @@ -143,4 +149,10 @@ test_expect_success 'apply binary diff (copy).' \ git apply --allow-binary-replacement --index CF.diff && test -z "$(git diff --name-status binary)"' +test_expect_success 'apply binary -p0 diff' ' + do_reset && + git apply -p0 --index B0.diff && + test -z "$(git diff --name-status binary -- file3)" +' + test_done -- cgit v1.2.3