From 324eb77ee76277be99bdc54ef0b74ff30f5f567b Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Mon, 13 Dec 2021 14:03:27 -0800 Subject: 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 Signed-off-by: Junio C Hamano --- t/t4126-apply-empty.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 't') diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh index ceb6a79fe0..949e284d14 100755 --- a/t/t4126-apply-empty.sh +++ b/t/t4126-apply-empty.sh @@ -9,6 +9,8 @@ test_expect_success setup ' 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 @@ -25,30 +27,42 @@ test_expect_success setup ' ' test_expect_success 'apply empty' ' - git reset --hard && 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 -