Message ID | e8b3fd8a5bf8f77795454a901fecaa585701eeb2.1563490164.git.steadmon@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | pre-merge hook | expand |
On Fri, 19 Jul 2019 at 00:57, Josh Steadmon <steadmon@google.com> wrote: > --- /dev/null > +++ b/templates/hooks--pre-merge.sample > @@ -0,0 +1,13 @@ > +#!/bin/sh > +# > +# An example hook script to verify what is about to be committed. > +# Called by "git merge" with no arguments. The hook should > +# exit with non-zero status after issuing an appropriate message to > +# stderr if it wants to stop the merge commit. > +# > +# To enable this hook, rename this file to "pre-merge". > + > +. git-sh-setup > +test -x "$GIT_DIR/hooks/pre-commit" && > + exec "$GIT_DIR/hooks/pre-commit" > +: I'm pretty certain that my comment here was with respect to the ":", which made me think "hmmm, won't : always exit successfully?". My slightly less ignorant self of today answers "sure, but we won't always get that far" -- the "exec" is pretty important, and it will cause the current shell to be replaced with the hook invocation. So however the pre-commit hook decides to exit, that'll be our exit status. I retract my comment from back then. Martin
diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index 786e778ab8..dcc6606d44 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -103,6 +103,13 @@ The default 'pre-commit' hook, when enabled--and with the `hooks.allownonascii` config option unset or set to false--prevents the use of non-ASCII filenames. +pre-merge +~~~~~~~~~ + +This hook is invoked by 'git merge' when doing an automatic merge +commit; it is equivalent to 'pre-commit' for a non-automatic commit +for a merge. + prepare-commit-msg ~~~~~~~~~~~~~~~~~~ diff --git a/builtin/merge.c b/builtin/merge.c index 6e99aead46..5cd7752191 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -816,6 +816,18 @@ static void write_merge_heads(struct commit_list *); static void prepare_to_commit(struct commit_list *remoteheads) { struct strbuf msg = STRBUF_INIT; + const char *index_file = get_index_file(); + + if (run_commit_hook(0 < option_edit, index_file, "pre-merge", NULL)) + abort_commit(remoteheads, NULL); + /* + * Re-read the index as pre-merge hook could have updated it, + * and write it out as a tree. We must do this before we invoke + * the editor and after we invoke run_status above. + */ + if (find_hook("pre-merge")) + discard_cache(); + read_cache_from(index_file); strbuf_addbuf(&msg, &merge_msg); if (squash) BUG("the control must not reach here under --squash"); diff --git a/templates/hooks--pre-merge.sample b/templates/hooks--pre-merge.sample new file mode 100755 index 0000000000..f459b3db44 --- /dev/null +++ b/templates/hooks--pre-merge.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: