]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-apply: add --allow-empty flag
authorJerry Zhang <jerry@skydio.com>
Mon, 13 Dec 2021 22:03:27 +0000 (14:03 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 Dec 2021 22:30:25 +0000 (14:30 -0800)
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>
Documentation/git-apply.txt
apply.c
apply.h
t/t4126-apply-empty.sh

index a32ad647182e1ed4a393984aad0b3c4d50be42b0..b6d77f420682618be63cb27a34e803cfcac8f802 100644 (file)
@@ -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
 -------------
 
diff --git a/apply.c b/apply.c
index 0392f420664cec4733373d1075fd972d393344a5..fed195250b64eea60e34849940c43016c2c3b5fa 100644 (file)
--- 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()
        };
 
diff --git a/apply.h b/apply.h
index da3d95fa50989bdd18289eec3a79b955c0d59443..16202da16026f8ef127e406418229d1f47802b53 100644 (file)
--- 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;
index ceb6a79fe0c8ca5b26a9e148215556f2aa344eb9..949e284d149a922652508d4e8f867b288b75aa9a 100755 (executable)
@@ -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 - </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' '
-       git reset --hard &&
        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' '
-       git reset --hard &&
        rm -f missing &&
+       test_when_finished "git reset --hard" &&
        git apply patch1 &&
        test_cmp expect missing
 '
 
 test_expect_success 'apply --index create' '
-       git reset --hard &&
        rm -f missing &&
+       test_when_finished "git reset --hard" &&
        git apply --index patch1 &&
        test_cmp expect missing &&
        git diff --exit-code