Skip to content

Commit 62a3a27

Browse files
committed
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
2 parents 5536415 + 324eb77 commit 62a3a27

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

Documentation/git-apply.txt

Lines changed: 10 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] [--unsafe-paths] [<patch>...]
19+
[--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
2020

2121
DESCRIPTION
2222
-----------
@@ -228,6 +228,11 @@ behavior:
228228
current patch being applied will be printed. This option will cause
229229
additional information to be reported.
230230

231+
-q::
232+
--quiet::
233+
Suppress stderr output. Messages about patch status and progress
234+
will not be printed.
235+
231236
--recount::
232237
Do not trust the line counts in the hunk headers, but infer them
233238
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
251256
the `--unsafe-paths` option to override this safety check. This option
252257
has no effect when `--index` or `--cached` is in use.
253258

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+
254263
CONFIGURATION
255264
-------------
256265

apply.c

Lines changed: 7 additions & 3 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

@@ -5071,7 +5073,7 @@ int apply_parse_options(int argc, const char **argv,
50715073
N_("leave the rejected hunks in corresponding *.rej files")),
50725074
OPT_BOOL(0, "allow-overlap", &state->allow_overlap,
50735075
N_("allow overlapping hunks")),
5074-
OPT__VERBOSE(&state->apply_verbosity, N_("be verbose")),
5076+
OPT__VERBOSITY(&state->apply_verbosity),
50755077
OPT_BIT(0, "inaccurate-eof", options,
50765078
N_("tolerate incorrectly detected missing new-line at the end of file"),
50775079
APPLY_OPT_INACCURATE_EOF),
@@ -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
@@ -11,6 +11,8 @@ test_expect_success setup '
1111
git add empty &&
1212
test_tick &&
1313
git commit -m initial &&
14+
git commit --allow-empty -m "empty commit" &&
15+
git format-patch --always HEAD~ >empty.patch &&
1416
for i in a b c d e
1517
do
1618
echo $i
@@ -27,30 +29,42 @@ test_expect_success setup '
2729
'
2830

2931
test_expect_success 'apply empty' '
30-
git reset --hard &&
3132
rm -f missing &&
33+
test_when_finished "git reset --hard" &&
3234
git apply patch0 &&
3335
test_cmp expect empty
3436
'
3537

38+
test_expect_success 'apply empty patch fails' '
39+
test_when_finished "git reset --hard" &&
40+
test_must_fail git apply empty.patch &&
41+
test_must_fail git apply - </dev/null
42+
'
43+
44+
test_expect_success 'apply with --allow-empty succeeds' '
45+
test_when_finished "git reset --hard" &&
46+
git apply --allow-empty empty.patch &&
47+
git apply --allow-empty - </dev/null
48+
'
49+
3650
test_expect_success 'apply --index empty' '
37-
git reset --hard &&
3851
rm -f missing &&
52+
test_when_finished "git reset --hard" &&
3953
git apply --index patch0 &&
4054
test_cmp expect empty &&
4155
git diff --exit-code
4256
'
4357

4458
test_expect_success 'apply create' '
45-
git reset --hard &&
4659
rm -f missing &&
60+
test_when_finished "git reset --hard" &&
4761
git apply patch1 &&
4862
test_cmp expect missing
4963
'
5064

5165
test_expect_success 'apply --index create' '
52-
git reset --hard &&
5366
rm -f missing &&
67+
test_when_finished "git reset --hard" &&
5468
git apply --index patch1 &&
5569
test_cmp expect missing &&
5670
git diff --exit-code

0 commit comments

Comments
 (0)