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>

Jerry Zhang committed Dec 13, 2021 at 14:03 UTC 324eb77ee76277be99bdc54ef0b74ff30f5f567b
4 files changed +30 -7
Documentation/git-apply.txt
+5 -1
@@ -16,7 +16,7 @@ SYNOPSIS
16 [--ignore-space-change | --ignore-whitespace]
17 [--whitespace=(nowarn|warn|fix|error|error-all)]
18 [--exclude=<path>] [--include=<path>] [--directory=<root>]
19 - [--verbose | --quiet] [--unsafe-paths] [<patch>...]
19 + [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
20
21 DESCRIPTION
22 -----------
@@ -256,6 +256,10 @@ When `git apply` is used as a "better GNU patch", the user can pass
256 the `--unsafe-paths` option to override this safety check. This option
257 has no effect when `--index` or `--cached` is in use.
258
259 +--allow-empty::
260 + Don't return error for patches containing no diff. This includes
261 + empty patches and patches with commit text only.
262 +
263 CONFIGURATION
264 -------------
265
apply.c
+6 -2
@@ -4752,8 +4752,10 @@ static int apply_patch(struct apply_state *state,
4752 }
4753
4754 if (!list && !skipped_patch) {
4755 - error(_("unrecognized input"));
4756 - res = -128;
4755 + if (!state->allow_empty) {
4756 + error(_("No valid patches in input (allow with \"--allow-empty\")"));
4757 + res = -128;
4758 + }
4759 goto end;
4760 }
4761
@@ -5081,6 +5083,8 @@ int apply_parse_options(int argc, const char **argv,
5083 OPT_CALLBACK(0, "directory", state, N_("root"),
5084 N_("prepend <root> to all filenames"),
5085 apply_option_parse_directory),
5086 + OPT_BOOL(0, "allow-empty", &state->allow_empty,
5087 + N_("don't return error for empty patches")),
5088 OPT_END()
5089 };
5090
apply.h
+1
@@ -66,6 +66,7 @@ struct apply_state {
66 int threeway;
67 int unidiff_zero;
68 int unsafe_paths;
69 + int allow_empty;
70
71 /* Other non boolean parameters */
72 struct repository *repo;
t/t4126-apply-empty.sh
+18 -4
@@ -9,6 +9,8 @@ test_expect_success setup '
9 git add empty &&
10 test_tick &&
11 git commit -m initial &&
12 + git commit --allow-empty -m "empty commit" &&
13 + git format-patch --always HEAD~ >empty.patch &&
14 for i in a b c d e
15 do
16 echo $i
@@ -25,30 +27,42 @@ test_expect_success setup '
27 '
28
29 test_expect_success 'apply empty' '
28 - git reset --hard &&
30 rm -f missing &&
31 + test_when_finished "git reset --hard" &&
32 git apply patch0 &&
33 test_cmp expect empty
34 '
35
36 +test_expect_success 'apply empty patch fails' '
37 + test_when_finished "git reset --hard" &&
38 + test_must_fail git apply empty.patch &&
39 + test_must_fail git apply - </dev/null
40 +'
41 +
42 +test_expect_success 'apply with --allow-empty succeeds' '
43 + test_when_finished "git reset --hard" &&
44 + git apply --allow-empty empty.patch &&
45 + git apply --allow-empty - </dev/null
46 +'
47 +
48 test_expect_success 'apply --index empty' '
35 - git reset --hard &&
49 rm -f missing &&
50 + test_when_finished "git reset --hard" &&
51 git apply --index patch0 &&
52 test_cmp expect empty &&
53 git diff --exit-code
54 '
55
56 test_expect_success 'apply create' '
43 - git reset --hard &&
57 rm -f missing &&
58 + test_when_finished "git reset --hard" &&
59 git apply patch1 &&
60 test_cmp expect missing
61 '
62
63 test_expect_success 'apply --index create' '
50 - git reset --hard &&
64 rm -f missing &&
65 + test_when_finished "git reset --hard" &&
66 git apply --index patch1 &&
67 test_cmp expect missing &&
68 git diff --exit-code