merge: --no-verify to bypass pre-merge-commit hook

Analogous to commit, introduce a '--no-verify' option which bypasses the pre-merge-commit hook. The shorthand '-n' is taken by '--no-stat' already. [js: * reworded commit message to reflect current state of --no-stat flag and new hook name * fixed flag documentation to reflect new hook name * cleaned up trailing whitespace * squashed test changes from the original series' patch 4/4 * modified tests to follow pattern from this series' patch 1/4 * added a test case for --no-verify with non-executable hook * when testing that the merge hook did not run, make sure we actually have a merge to perform (by resetting the "side" branch to its original state). ] Improved-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Michael J Gruber <git@grubix.eu> Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael J Gruber committed Aug 7, 2019 at 11:57 UTC bc40ce4de612942e9a6a755f7432806ee095bd82
3 files changed +43 -3
Documentation/githooks.txt
+2 -1
@@ -106,7 +106,8 @@ the use of non-ASCII filenames.
106 pre-merge-commit
107 ~~~~~~~~~~~~~~~~
108
109 -This hook is invoked by linkgit:git-merge[1]. It takes no parameters, and is
109 +This hook is invoked by linkgit:git-merge[1], and can be bypassed
110 +with the `--no-verify` option. It takes no parameters, and is
111 invoked after the merge has been carried out successfully and before
112 obtaining the proposed commit log message to
113 make a commit. Exiting with a non-zero status from this script
builtin/merge.c
+2 -2
@@ -287,7 +287,7 @@ static struct option builtin_merge_options[] = {
287 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
288 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
289 OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
290 - OPT_BOOL(0, "no-verify", &no_verify, N_("bypass commit-msg hook")),
290 + OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
291 OPT_END()
292 };
293
@@ -818,7 +818,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
818 struct strbuf msg = STRBUF_INIT;
819 const char *index_file = get_index_file();
820
821 - if (run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
821 + if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
822 abort_commit(remoteheads, NULL);
823 /*
824 * Re-read the index as pre-merge-commit hook could have updated it,
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
+39
@@ -84,6 +84,15 @@ test_expect_success '--no-verify with no hook' '
84 test_path_is_missing actual_hooks
85 '
86
87 +test_expect_success '--no-verify with no hook (merge)' '
88 + test_when_finished "rm -f actual_hooks" &&
89 + git branch -f side side-orig &&
90 + git checkout side &&
91 + git merge --no-verify -m "merge master" master &&
92 + git checkout master &&
93 + test_path_is_missing actual_hooks
94 +'
95 +
96 test_expect_success 'with succeeding hook' '
97 test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
98 cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
@@ -131,6 +140,16 @@ test_expect_success '--no-verify with succeeding hook' '
140 test_path_is_missing actual_hooks
141 '
142
143 +test_expect_success '--no-verify with succeeding hook (merge)' '
144 + test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
145 + cp "$HOOKDIR/success.sample" "$PREMERGE" &&
146 + git branch -f side side-orig &&
147 + git checkout side &&
148 + git merge --no-verify -m "merge master" master &&
149 + git checkout master &&
150 + test_path_is_missing actual_hooks
151 +'
152 +
153 test_expect_success 'with failing hook' '
154 test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
155 cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
@@ -160,6 +179,16 @@ test_expect_success 'with failing hook (merge)' '
179 test_cmp expected_hooks actual_hooks
180 '
181
182 +test_expect_success '--no-verify with failing hook (merge)' '
183 + test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
184 + cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
185 + git branch -f side side-orig &&
186 + git checkout side &&
187 + git merge --no-verify -m "merge master" master &&
188 + git checkout master &&
189 + test_path_is_missing actual_hooks
190 +'
191 +
192 test_expect_success POSIXPERM 'with non-executable hook' '
193 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
194 cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
@@ -188,6 +217,16 @@ test_expect_success POSIXPERM 'with non-executable hook (merge)' '
217 test_path_is_missing actual_hooks
218 '
219
220 +test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
221 + test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
222 + cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
223 + git branch -f side side-orig &&
224 + git checkout side &&
225 + git merge --no-verify -m "merge master" master &&
226 + git checkout master &&
227 + test_path_is_missing actual_hooks
228 +'
229 +
230 test_expect_success 'with hook requiring GIT_PREFIX' '
231 test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
232 cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&