git/t/t4126-apply-empty.sh
Jerry Zhang 324eb77ee7 git-apply: add --allow-empty flag
Some users or scripts will pipe "git diff"
output to "git apply" when replaying diffs
or commits. In these cases, they will rely
on the return value of "git apply" to know
whether the diff was applied successfully.

However, for empty commits, "git apply" will
fail. This complicates scripts since they
have to either buffer the diff and check
its length, or run diff again with "exit-code",
essentially doing the diff twice.

Add the "--allow-empty" flag to "git apply"
which allows it to handle both empty diffs
and empty commits created by "git format-patch
--always" by doing nothing and returning 0.

Add tests for both with and without --allow-empty.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-12-13 14:30:25 -08:00

71 lines
1.5 KiB
Bash
Executable file

#!/bin/sh
test_description='apply empty'
. ./test-lib.sh
test_expect_success setup '
>empty &&
git add empty &&
test_tick &&
git commit -m initial &&
git commit --allow-empty -m "empty commit" &&
git format-patch --always HEAD~ >empty.patch &&
for i in a b c d e
do
echo $i
done >empty &&
cat empty >expect &&
git diff |
sed -e "/^diff --git/d" \
-e "/^index /d" \
-e "s|a/empty|empty.orig|" \
-e "s|b/empty|empty|" >patch0 &&
sed -e "s|empty|missing|" patch0 >patch1 &&
>empty &&
git update-index --refresh
'
test_expect_success 'apply empty' '
rm -f missing &&
test_when_finished "git reset --hard" &&
git apply patch0 &&
test_cmp expect empty
'
test_expect_success 'apply empty patch fails' '
test_when_finished "git reset --hard" &&
test_must_fail git apply empty.patch &&
test_must_fail git apply - </dev/null
'
test_expect_success 'apply with --allow-empty succeeds' '
test_when_finished "git reset --hard" &&
git apply --allow-empty empty.patch &&
git apply --allow-empty - </dev/null
'
test_expect_success 'apply --index empty' '
rm -f missing &&
test_when_finished "git reset --hard" &&
git apply --index patch0 &&
test_cmp expect empty &&
git diff --exit-code
'
test_expect_success 'apply create' '
rm -f missing &&
test_when_finished "git reset --hard" &&
git apply patch1 &&
test_cmp expect missing
'
test_expect_success 'apply --index create' '
rm -f missing &&
test_when_finished "git reset --hard" &&
git apply --index patch1 &&
test_cmp expect missing &&
git diff --exit-code
'
test_done