am: allow passing --no-verify flag

The git-am --no-verify flag is analogous to the same flag passed to git-commit. It bypasses the pre-applypatch and applypatch-msg hooks if they are enabled. Signed-off-by: Thierry Reding <treding@nvidia.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thierry Reding committed Nov 30, 2022 at 18:28 UTC 566902f2db3bdad9be7fb083c713f0d21acf111e
3 files changed +47 -4
Documentation/git-am.txt
+7 -1
@@ -9,7 +9,7 @@ git-am - Apply a series of patches from a mailbox
9 SYNOPSIS
10 --------
11 [verse]
12 -'git am' [--signoff] [--keep] [--[no-]keep-cr] [--[no-]utf8]
12 +'git am' [--signoff] [--keep] [--[no-]keep-cr] [--[no-]utf8] [--no-verify]
13 [--[no-]3way] [--interactive] [--committer-date-is-author-date]
14 [--ignore-date] [--ignore-space-change | --ignore-whitespace]
15 [--whitespace=<option>] [-C<n>] [-p<n>] [--directory=<dir>]
@@ -138,6 +138,12 @@ include::rerere-options.txt[]
138 --interactive::
139 Run interactively.
140
141 +-n::
142 +--no-verify::
143 + By default, the pre-applypatch and applypatch-msg hooks are run.
144 + When any of `--no-verify` or `-n` is given, these are bypassed.
145 + See also linkgit:githooks[5].
146 +
147 --committer-date-is-author-date::
148 By default the command records the date from the e-mail
149 message as the commit author date, and uses the time of
builtin/am.c
+8 -3
@@ -117,6 +117,7 @@ struct am_state {
117
118 /* various operating modes and command line options */
119 int interactive;
120 + int no_verify;
121 int threeway;
122 int quiet;
123 int signoff; /* enum signoff_type */
@@ -472,10 +473,12 @@ static void am_destroy(const struct am_state *state)
473 */
474 static int run_applypatch_msg_hook(struct am_state *state)
475 {
475 - int ret;
476 + int ret = 0;
477
478 assert(state->msg);
478 - ret = run_hooks_l("applypatch-msg", am_path(state, "final-commit"), NULL);
479 +
480 + if (!state->no_verify)
481 + ret = run_hooks_l("applypatch-msg", am_path(state, "final-commit"), NULL);
482
483 if (!ret) {
484 FREE_AND_NULL(state->msg);
@@ -1640,7 +1643,7 @@ static void do_commit(const struct am_state *state)
1643 const char *reflog_msg, *author, *committer = NULL;
1644 struct strbuf sb = STRBUF_INIT;
1645
1643 - if (run_hooks("pre-applypatch"))
1646 + if (!state->no_verify && run_hooks("pre-applypatch"))
1647 exit(1);
1648
1649 if (write_cache_as_tree(&tree, 0, NULL))
@@ -2329,6 +2332,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2332 struct option options[] = {
2333 OPT_BOOL('i', "interactive", &state.interactive,
2334 N_("run interactively")),
2335 + OPT_BOOL('n', "no-verify", &state.no_verify,
2336 + N_("bypass pre-applypatch and applypatch-msg hooks")),
2337 OPT_HIDDEN_BOOL('b', "binary", &binary,
2338 N_("historical option -- no-op")),
2339 OPT_BOOL('3', "3way", &state.threeway,
t/t4150-am.sh
+32
@@ -345,6 +345,21 @@ test_expect_success 'am with failing applypatch-msg hook' '
345 test_cmp_rev first HEAD
346 '
347
348 +test_expect_success 'am with failing applypatch-msg hook (no verify)' '
349 + rm -fr .git/rebase-apply &&
350 + git reset --hard &&
351 + git checkout first &&
352 + test_hook applypatch-msg <<-\EOF &&
353 + echo hook-message >"$1"
354 + exit 1
355 + EOF
356 + git am --no-verify patch1 &&
357 + test_path_is_missing .git/rebase-apply &&
358 + git diff --exit-code second &&
359 + git log -1 --format=format:%B >actual &&
360 + test_cmp msg actual
361 +'
362 +
363 test_expect_success 'am with pre-applypatch hook' '
364 rm -fr .git/rebase-apply &&
365 git reset --hard &&
@@ -374,6 +389,23 @@ test_expect_success 'am with failing pre-applypatch hook' '
389 test_cmp_rev first HEAD
390 '
391
392 +test_expect_success 'am with failing pre-applypatch hook (no verify)' '
393 + rm -fr .git/rebase-apply &&
394 + git reset --hard &&
395 + git checkout first &&
396 + touch empty-file &&
397 + test_hook pre-applypatch <<-\EOF &&
398 + rm empty-file
399 + exit 1
400 + EOF
401 + git am --no-verify patch1 &&
402 + test_path_is_missing .git/rebase-apply &&
403 + test_path_is_file empty-file &&
404 + git diff --exit-code second &&
405 + git log -1 --format=format:%B >actual &&
406 + test_cmp msg actual
407 +'
408 +
409 test_expect_success 'am with post-applypatch hook' '
410 rm -fr .git/rebase-apply &&
411 git reset --hard &&