Merge branch 'jz/apply-quiet-and-allow-empty'

"git apply" has been taught to ignore a message without a patch
with the "--allow-empty" option.  It also learned to honor the
"--quiet" option given from the command line.

* jz/apply-quiet-and-allow-empty:
  git-apply: add --allow-empty flag
  git-apply: add --quiet flag
This commit is contained in:
Junio C Hamano
2021-12-22 22:48:11 -08:00
4 changed files with 36 additions and 8 deletions

View File

@ -16,7 +16,7 @@ SYNOPSIS
[--ignore-space-change | --ignore-whitespace] [--ignore-space-change | --ignore-whitespace]
[--whitespace=(nowarn|warn|fix|error|error-all)] [--whitespace=(nowarn|warn|fix|error|error-all)]
[--exclude=<path>] [--include=<path>] [--directory=<root>] [--exclude=<path>] [--include=<path>] [--directory=<root>]
[--verbose] [--unsafe-paths] [<patch>...] [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
DESCRIPTION DESCRIPTION
----------- -----------
@ -228,6 +228,11 @@ behavior:
current patch being applied will be printed. This option will cause current patch being applied will be printed. This option will cause
additional information to be reported. additional information to be reported.
-q::
--quiet::
Suppress stderr output. Messages about patch status and progress
will not be printed.
--recount:: --recount::
Do not trust the line counts in the hunk headers, but infer them Do not trust the line counts in the hunk headers, but infer them
by inspecting the patch (e.g. after editing the patch without by inspecting the patch (e.g. after editing the patch without
@ -251,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 the `--unsafe-paths` option to override this safety check. This option
has no effect when `--index` or `--cached` is in use. 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 CONFIGURATION
------------- -------------

View File

@ -4752,8 +4752,10 @@ static int apply_patch(struct apply_state *state,
} }
if (!list && !skipped_patch) { if (!list && !skipped_patch) {
error(_("unrecognized input")); if (!state->allow_empty) {
error(_("No valid patches in input (allow with \"--allow-empty\")"));
res = -128; res = -128;
}
goto end; goto end;
} }
@ -5071,7 +5073,7 @@ int apply_parse_options(int argc, const char **argv,
N_("leave the rejected hunks in corresponding *.rej files")), N_("leave the rejected hunks in corresponding *.rej files")),
OPT_BOOL(0, "allow-overlap", &state->allow_overlap, OPT_BOOL(0, "allow-overlap", &state->allow_overlap,
N_("allow overlapping hunks")), N_("allow overlapping hunks")),
OPT__VERBOSE(&state->apply_verbosity, N_("be verbose")), OPT__VERBOSITY(&state->apply_verbosity),
OPT_BIT(0, "inaccurate-eof", options, OPT_BIT(0, "inaccurate-eof", options,
N_("tolerate incorrectly detected missing new-line at the end of file"), N_("tolerate incorrectly detected missing new-line at the end of file"),
APPLY_OPT_INACCURATE_EOF), APPLY_OPT_INACCURATE_EOF),
@ -5081,6 +5083,8 @@ int apply_parse_options(int argc, const char **argv,
OPT_CALLBACK(0, "directory", state, N_("root"), OPT_CALLBACK(0, "directory", state, N_("root"),
N_("prepend <root> to all filenames"), N_("prepend <root> to all filenames"),
apply_option_parse_directory), apply_option_parse_directory),
OPT_BOOL(0, "allow-empty", &state->allow_empty,
N_("don't return error for empty patches")),
OPT_END() OPT_END()
}; };

View File

@ -66,6 +66,7 @@ struct apply_state {
int threeway; int threeway;
int unidiff_zero; int unidiff_zero;
int unsafe_paths; int unsafe_paths;
int allow_empty;
/* Other non boolean parameters */ /* Other non boolean parameters */
struct repository *repo; struct repository *repo;

View File

@ -11,6 +11,8 @@ test_expect_success setup '
git add empty && git add empty &&
test_tick && test_tick &&
git commit -m initial && 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 for i in a b c d e
do do
echo $i echo $i
@ -27,30 +29,42 @@ test_expect_success setup '
' '
test_expect_success 'apply empty' ' test_expect_success 'apply empty' '
git reset --hard &&
rm -f missing && rm -f missing &&
test_when_finished "git reset --hard" &&
git apply patch0 && git apply patch0 &&
test_cmp expect empty 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' ' test_expect_success 'apply --index empty' '
git reset --hard &&
rm -f missing && rm -f missing &&
test_when_finished "git reset --hard" &&
git apply --index patch0 && git apply --index patch0 &&
test_cmp expect empty && test_cmp expect empty &&
git diff --exit-code git diff --exit-code
' '
test_expect_success 'apply create' ' test_expect_success 'apply create' '
git reset --hard &&
rm -f missing && rm -f missing &&
test_when_finished "git reset --hard" &&
git apply patch1 && git apply patch1 &&
test_cmp expect missing test_cmp expect missing
' '
test_expect_success 'apply --index create' ' test_expect_success 'apply --index create' '
git reset --hard &&
rm -f missing && rm -f missing &&
test_when_finished "git reset --hard" &&
git apply --index patch1 && git apply --index patch1 &&
test_cmp expect missing && test_cmp expect missing &&
git diff --exit-code git diff --exit-code