Skip to content

Commit 324eb77

Browse files
jerry-skydiogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c21b8ae commit 324eb77

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

Documentation/git-apply.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SYNOPSIS
1616
[--ignore-space-change | --ignore-whitespace]
1717
[--whitespace=(nowarn|warn|fix|error|error-all)]
1818
[--exclude=<path>] [--include=<path>] [--directory=<root>]
19-
[--verbose | --quiet] [--unsafe-paths] [<patch>...]
19+
[--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
2020

2121
DESCRIPTION
2222
-----------
@@ -256,6 +256,10 @@ When `git apply` is used as a "better GNU patch", the user can pass
256256
the `--unsafe-paths` option to override this safety check. This option
257257
has no effect when `--index` or `--cached` is in use.
258258

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+
259263
CONFIGURATION
260264
-------------
261265

apply.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4752,8 +4752,10 @@ static int apply_patch(struct apply_state *state,
47524752
}
47534753

47544754
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+
}
47574759
goto end;
47584760
}
47594761

@@ -5081,6 +5083,8 @@ int apply_parse_options(int argc, const char **argv,
50815083
OPT_CALLBACK(0, "directory", state, N_("root"),
50825084
N_("prepend <root> to all filenames"),
50835085
apply_option_parse_directory),
5086+
OPT_BOOL(0, "allow-empty", &state->allow_empty,
5087+
N_("don't return error for empty patches")),
50845088
OPT_END()
50855089
};
50865090

apply.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct apply_state {
6666
int threeway;
6767
int unidiff_zero;
6868
int unsafe_paths;
69+
int allow_empty;
6970

7071
/* Other non boolean parameters */
7172
struct repository *repo;

t/t4126-apply-empty.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ test_expect_success setup '
99
git add empty &&
1010
test_tick &&
1111
git commit -m initial &&
12+
git commit --allow-empty -m "empty commit" &&
13+
git format-patch --always HEAD~ >empty.patch &&
1214
for i in a b c d e
1315
do
1416
echo $i
@@ -25,30 +27,42 @@ test_expect_success setup '
2527
'
2628

2729
test_expect_success 'apply empty' '
28-
git reset --hard &&
2930
rm -f missing &&
31+
test_when_finished "git reset --hard" &&
3032
git apply patch0 &&
3133
test_cmp expect empty
3234
'
3335

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+
3448
test_expect_success 'apply --index empty' '
35-
git reset --hard &&
3649
rm -f missing &&
50+
test_when_finished "git reset --hard" &&
3751
git apply --index patch0 &&
3852
test_cmp expect empty &&
3953
git diff --exit-code
4054
'
4155

4256
test_expect_success 'apply create' '
43-
git reset --hard &&
4457
rm -f missing &&
58+
test_when_finished "git reset --hard" &&
4559
git apply patch1 &&
4660
test_cmp expect missing
4761
'
4862

4963
test_expect_success 'apply --index create' '
50-
git reset --hard &&
5164
rm -f missing &&
65+
test_when_finished "git reset --hard" &&
5266
git apply --index patch1 &&
5367
test_cmp expect missing &&
5468
git diff --exit-code

0 commit comments

Comments
 (0)