git/t/t4104-apply-boundary.sh
Björn Gustavsson 24ff4d56cf apply: Remove the quick rejection test
In the next commit, we will make it possible for blank context
lines to match beyond the end of the file. That means that a hunk
with a preimage that has more lines than present in the file may
be possible to successfully apply. Therefore, we must remove
the quick rejection test in find_pos().

find_pos() will already work correctly without the quick
rejection test, but that might not be obvious. Therefore,
comment the test for handling out-of-range line numbers in
find_pos() and cast the "line" variable to the same (unsigned)
type as img->nr.

What are performance implications of removing the quick
rejection test?

It can only help "git apply" to reject a patch faster. For example,
if I have a file with one million lines and a patch that removes
slightly more than 50 percent of the lines and try to apply that
patch twice, the second attempt will fail slightly faster
with the test than without (based on actual measurements).

However, there is the pathological case of a patch with many
more context lines than the default three, and applying that patch
using "git apply -C1". Without the rejection test, the running
time will be roughly proportional to the number of context lines
times the size of the file. That could be handled by writing
a more complicated rejection test (it would have to count the
number of blanks at the end of the preimage), but I don't find
that worth doing until there is a real-world use case that
would benfit from it.

It would be possible to keep the quick rejection test if
--whitespace=fix is not given, but I don't like that from
a testing point of view.

Signed-off-by: Björn Gustavsson <bgustavsson@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-06 23:53:00 -08:00

147 lines
2.9 KiB
Bash
Executable File

#!/bin/sh
#
# Copyright (c) 2005 Junio C Hamano
#
test_description='git apply boundary tests
'
. ./test-lib.sh
L="c d e f g h i j k l m n o p q r s t u v w x"
test_expect_success setup '
for i in b '"$L"' y
do
echo $i
done >victim &&
cat victim >original &&
git update-index --add victim &&
: add to the head
for i in a b '"$L"' y
do
echo $i
done >victim &&
cat victim >add-a-expect &&
git diff victim >add-a-patch.with &&
git diff --unified=0 >add-a-patch.without &&
: insert at line two
for i in b a '"$L"' y
do
echo $i
done >victim &&
cat victim >insert-a-expect &&
git diff victim >insert-a-patch.with &&
git diff --unified=0 >insert-a-patch.without &&
: modify at the head
for i in a '"$L"' y
do
echo $i
done >victim &&
cat victim >mod-a-expect &&
git diff victim >mod-a-patch.with &&
git diff --unified=0 >mod-a-patch.without &&
: remove from the head
for i in '"$L"' y
do
echo $i
done >victim &&
cat victim >del-a-expect &&
git diff victim >del-a-patch.with
git diff --unified=0 >del-a-patch.without &&
: add to the tail
for i in b '"$L"' y z
do
echo $i
done >victim &&
cat victim >add-z-expect &&
git diff victim >add-z-patch.with &&
git diff --unified=0 >add-z-patch.without &&
: modify at the tail
for i in b '"$L"' z
do
echo $i
done >victim &&
cat victim >mod-z-expect &&
git diff victim >mod-z-patch.with &&
git diff --unified=0 >mod-z-patch.without &&
: remove from the tail
for i in b '"$L"'
do
echo $i
done >victim &&
cat victim >del-z-expect &&
git diff victim >del-z-patch.with
git diff --unified=0 >del-z-patch.without &&
: done
'
for with in with without
do
case "$with" in
with) u= ;;
without) u='--unidiff-zero ' ;;
esac
for kind in add-a add-z insert-a mod-a mod-z del-a del-z
do
test_expect_success "apply $kind-patch $with context" '
cat original >victim &&
git update-index victim &&
git apply --index '"$u$kind-patch.$with"' || {
cat '"$kind-patch.$with"'
(exit 1)
} &&
test_cmp '"$kind"'-expect victim
'
done
done
for kind in add-a add-z insert-a mod-a mod-z del-a del-z
do
rm -f $kind-ng.without
sed -e "s/^diff --git /diff /" \
-e '/^index /d' \
<$kind-patch.without >$kind-ng.without
test_expect_success "apply non-git $kind-patch without context" '
cat original >victim &&
git update-index victim &&
git apply --unidiff-zero --index '"$kind-ng.without"' || {
cat '"$kind-ng.without"'
(exit 1)
} &&
test_cmp '"$kind"'-expect victim
'
done
test_expect_success 'two lines' '
>file &&
git add file &&
echo aaa >file &&
git diff >patch &&
git add file &&
echo bbb >file &&
git add file &&
test_must_fail git apply --check patch
'
test_expect_success 'apply patch with 3 context lines matching at end' '
{ echo a; echo b; echo c; echo d; } >file &&
git add file &&
echo e >>file &&
git diff >patch &&
>file &&
test_must_fail git apply patch
'
test_done