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 committedDec 13, 2021 at 14:03 UTC324eb77ee76277be99bdc54ef0b74ff30f5f567b
4 files changed+30-7
Documentation/git-apply.txt
+5-1
index a32ad64718..b6d77f4206 100644--- a/Documentation/git-apply.txt+++ b/Documentation/git-apply.txt@@ -16,7 +16,7 @@ SYNOPSIS [--ignore-space-change | --ignore-whitespace] [--whitespace=(nowarn|warn|fix|error|error-all)] [--exclude=<path>] [--include=<path>] [--directory=<root>]- [--verbose | --quiet] [--unsafe-paths] [<patch>...]+ [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...] DESCRIPTION -----------@@ -256,6 +256,10 @@ When `git apply` is used as a "better GNU patch", the user can pass the `--unsafe-paths` option to override this safety check. This option has no effect when `--index` or `--cached` is in use.+--allow-empty::+ Don't return error for patches containing no diff. This includes+ empty patches and patches with commit text only.+ CONFIGURATION -------------
apply.c
+6-2
index 0392f42066..fed195250b 100644--- a/apply.c+++ b/apply.c@@ -4752,8 +4752,10 @@ static int apply_patch(struct apply_state *state, } if (!list && !skipped_patch) {- error(_("unrecognized input"));- res = -128;+ if (!state->allow_empty) {+ error(_("No valid patches in input (allow with \"--allow-empty\")"));+ res = -128;+ } goto end; }@@ -5081,6 +5083,8 @@ int apply_parse_options(int argc, const char **argv, OPT_CALLBACK(0, "directory", state, N_("root"), N_("prepend <root> to all filenames"), apply_option_parse_directory),+ OPT_BOOL(0, "allow-empty", &state->allow_empty,+ N_("don't return error for empty patches")), OPT_END() };
apply.h
+1
index da3d95fa50..16202da160 100644--- a/apply.h+++ b/apply.h@@ -66,6 +66,7 @@ struct apply_state { int threeway; int unidiff_zero; int unsafe_paths;+ int allow_empty; /* Other non boolean parameters */ struct repository *repo;