git/t/t4104-apply-boundary.sh
Junio C Hamano 4be609625e apply --unidiff-zero: loosen sanity checks for --unidiff=0 patches
In "git-apply", we have a few sanity checks and heuristics that
expects that the patch fed to us is a unified diff with at least
one line of context.

 * When there is no leading context line in a hunk, the hunk
   must apply at the beginning of the preimage.  Similarly, no
   trailing context means that the hunk is anchored at the end.

 * We learn a patch deletes the file from a hunk that has no
   resulting line (i.e. all lines are prefixed with '-') if it
   has not otherwise been known if the patch deletes the file.
   Similarly, no old line means the file is being created.

And we declare an error condition when the file created by a
creation patch already exists, and/or when a deletion patch
still leaves content in the file.

These sanity checks are good safety measures, but breaks down
when people feed a diff generated with --unified=0.  This was
recently noticed first by Matthew Wilcox and Gerrit Pape.

This adds a new flag, --unified-zero, to allow bypassing these
checks.  If you are in control of the patch generation process,
you should not use --unified=0 patch and fix it up with this
flag; rather you should try work with a patch with context.  But
if all you have to work with is a patch without context, this
flag may come handy as the last resort.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-17 01:12:37 -07:00

116 lines
2.3 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 &&
: 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 a '"$L"' y
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 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)
} &&
diff -u '"$kind"'-expect victim
'
done
done
for kind in add-a add-z 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)
} &&
diff -u '"$kind"'-expect victim
'
done
test_done