git/t/t4127-apply-same-fn.sh

99 lines
2.2 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='apply same filename'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
modify () {
sed -e "$1" < "$2" > "$2".x &&
mv "$2".x "$2"
}
test_expect_success setup '
test_write_lines a b c d e f g h i j k l m >same_fn &&
cp same_fn other_fn &&
git add same_fn other_fn &&
git commit -m initial
'
test_expect_success 'apply same filename with independent changes' '
modify "s/^d/z/" same_fn &&
git diff > patch0 &&
git add same_fn &&
modify "s/^i/y/" same_fn &&
git diff >> patch0 &&
cp same_fn same_fn2 &&
git reset --hard &&
git apply patch0 &&
test_cmp same_fn same_fn2
'
test_expect_success 'apply same filename with overlapping changes' '
git reset --hard &&
apply: when -R, also reverse list of sections A patch changing a symlink into a file is written with 2 sections (in the code, represented as "struct patch"): firstly, the deletion of the symlink, and secondly, the creation of the file. When applying that patch with -R, the sections are reversed, so we get: (1) creation of a symlink, then (2) deletion of a file. This causes an issue when the "deletion of a file" section is checked, because Git observes that the so-called file is not a file but a symlink, resulting in a "wrong type" error message. What we want is: (1) deletion of a file, then (2) creation of a symlink. In the code, this is reflected in the behavior of previous_patch() when invoked from check_preimage() when the deletion is checked. Creation then deletion means that when the deletion is checked, previous_patch() returns the creation section, triggering a mode conflict resulting in the "wrong type" error message. But deletion then creation means that when the deletion is checked, previous_patch() returns NULL, so the deletion mode is checked against lstat, which is what we want. There are also other ways a patch can contain 2 sections referencing the same file, for example, in 7a07841c0b ("git-apply: handle a patch that touches the same path more than once better", 2008-06-27). "git apply -R" fails in the same way, and this commit makes this case succeed. Therefore, when building the list of sections, build them in reverse order (by adding to the front of the list instead of the back) when -R is passed. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-10-20 22:04:52 +00:00
# Store same_fn so that we can check apply -R in next test
cp same_fn same_fn1 &&
modify "s/^d/z/" same_fn &&
git diff > patch0 &&
git add same_fn &&
modify "s/^e/y/" same_fn &&
git diff >> patch0 &&
cp same_fn same_fn2 &&
git reset --hard &&
git apply patch0 &&
test_cmp same_fn same_fn2
'
apply: when -R, also reverse list of sections A patch changing a symlink into a file is written with 2 sections (in the code, represented as "struct patch"): firstly, the deletion of the symlink, and secondly, the creation of the file. When applying that patch with -R, the sections are reversed, so we get: (1) creation of a symlink, then (2) deletion of a file. This causes an issue when the "deletion of a file" section is checked, because Git observes that the so-called file is not a file but a symlink, resulting in a "wrong type" error message. What we want is: (1) deletion of a file, then (2) creation of a symlink. In the code, this is reflected in the behavior of previous_patch() when invoked from check_preimage() when the deletion is checked. Creation then deletion means that when the deletion is checked, previous_patch() returns the creation section, triggering a mode conflict resulting in the "wrong type" error message. But deletion then creation means that when the deletion is checked, previous_patch() returns NULL, so the deletion mode is checked against lstat, which is what we want. There are also other ways a patch can contain 2 sections referencing the same file, for example, in 7a07841c0b ("git-apply: handle a patch that touches the same path more than once better", 2008-06-27). "git apply -R" fails in the same way, and this commit makes this case succeed. Therefore, when building the list of sections, build them in reverse order (by adding to the front of the list instead of the back) when -R is passed. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-10-20 22:04:52 +00:00
test_expect_success 'apply same filename with overlapping changes, in reverse' '
git apply -R patch0 &&
test_cmp same_fn same_fn1
'
test_expect_success 'apply same new filename after rename' '
git reset --hard &&
git mv same_fn new_fn &&
modify "s/^d/z/" new_fn &&
git add new_fn &&
git diff -M --cached > patch1 &&
modify "s/^e/y/" new_fn &&
git diff >> patch1 &&
cp new_fn new_fn2 &&
git reset --hard &&
git apply --index patch1 &&
test_cmp new_fn new_fn2
'
test_expect_success 'apply same old filename after rename -- should fail.' '
git reset --hard &&
git mv same_fn new_fn &&
modify "s/^d/z/" new_fn &&
git add new_fn &&
git diff -M --cached > patch1 &&
git mv new_fn same_fn &&
modify "s/^e/y/" same_fn &&
git diff >> patch1 &&
git reset --hard &&
test_must_fail git apply patch1
'
test_expect_success 'apply A->B (rename), C->A (rename), A->A -- should pass.' '
git reset --hard &&
git mv same_fn new_fn &&
modify "s/^d/z/" new_fn &&
git add new_fn &&
git diff -M --cached > patch1 &&
git commit -m "a rename" &&
git mv other_fn same_fn &&
modify "s/^e/y/" same_fn &&
git add same_fn &&
git diff -M --cached >> patch1 &&
modify "s/^g/x/" same_fn &&
git diff >> patch1 &&
git reset --hard HEAD^ &&
git apply patch1
'
test_done