The question says it all. Is there a way to perform an action before a merge? I'm guessing there's a way to make use of a pre-commit
hook, but I'm not quite sure.
You can try using the prepare-commit-msg
hook. The second argument will be merge
"if the commit is a merge or a .git/MERGE_MSG
file exists". A non-zero exit status will abort the commit.
I don't think this will work with a fast-forward merge, since there won't be a commit message.
More info on hooks: https://www.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg
exit 1
at the top of prepare-commit-msg and perform a merge commit. –
Pitchstone printf -- '%s\n%s\n' '#!/bin/sh' 'echo nope && exit 1' > prepare-commit-msg
–
Rafflesia With Git 2.24 (Q4 2019), no need for script wrapper, or prepare-message hook.
A new "pre-merge-commit" hook has been introduced.
See commit bc40ce4, commit 6098817, commit a1f3dd7 (07 Aug 2019) by Michael J Gruber (mjg
).
See commit f78f6c7 (07 Aug 2019) by Josh Steadmon (steadmon
).
(Merged by Junio C Hamano -- gitster
-- in commit f76bd8c, 18 Sep 2019)
git-merge
: honorpre-merge-commit
hook
git-merge
does not honor thepre-commit
hook when doing automatic merge commits, and for compatibility reasons this is going to stay.Introduce a
pre-merge-commit
hook which is called for an automatic merge commit just like pre-commit is called for a non-automatic merge commit (or any other commit).
The documentation now includes:
pre-merge-commit
This hook is invoked by
git-merge
.
It takes no parameters, and is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message to make a commit.
Exiting with a non-zero status from this script causes thegit merge
command to abort before creating a commit.The default 'pre-merge-commit' hook, when enabled, runs the 'pre-commit' hook, if the latter is enabled.
This hook is invoked with the environment variable
GIT_EDITOR=:
if the command will not bring up an editor to modify the commit message.If the merge cannot be carried out automatically, the conflicts need to be resolved and the result committed separately (see
git-merge
).
At that point, this hook will not be executed, but the 'pre-commit
' hook will, if it is enabled.
With Git 2.36 (Q2 2022), the callers of run_commit_hook()
learn if it got "success" because the hook succeeded or because there wasn't any hook.
See commit a8cc594, commit 9f6e63b (07 Mar 2022) by Ævar Arnfjörð Bjarmason (avar
).
(Merged by Junio C Hamano -- gitster
-- in commit 7431379, 16 Mar 2022)
merge
: don't run post-hook logic on--no-verify
Signed-off-by: Ævar Arnfjörð Bjarmason
Fix a minor bug introduced in bc40ce4 ("
merge
:--no-verify
to bypass pre-merge-commit hook", 2019-08-07, Git v2.24.0-rc0 -- merge listed in batch #3), when that change made the--no-verify
option bypass thepre-merge-commit
hook it didn't update the correspondingfind_hook()
(laterhook_exists()
) condition.As can be seen in the preceding commit in 6098817 ("
git-merge
: honor pre-merge-commit hook", 2019-08-07, Git v2.24.0-rc0 -- merge listed in batch #3) the two should go hand in hand.
There's no point in invokingdiscard_cache()
here if the hook couldn't have possibly updated the index.It's buggy that we use
"hook_exist()
" here, and as discussed in the subsequent commit it's subject to obscure race conditions that we're about to fix, but for now this change is a strict improvement that retains any caveats to do with the use of"hooks_exist()
" as-is.
Warning: Devin Rhode reports (Nov. 2022) in the comments:
I think I actually found a bug with the pre-merge-commit hook not running.
See PR "pre-merge-commit hook is not running"
But Pascal Jufer adds:
I came to the conclusion that the pre-merge-commit hook is actually working as intended, which means this is not a bug.
The documentation states the following:[...] is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message to make a commit [...]
and
[...] If the merge cannot be carried out automatically, the conflicts need to be resolved and the result committed separately (see git-merge1).
At that point, this hook will not be executed, but the pre-commit hook will, if it is enabled.That means:
If there's a conflict like provoked in your reproduction (merge cannot be carried out automatically), the pre-merge-commit hook won't be called at all.
Understandably, after resolving the conflict and committing the changes, Git treats it like a normal commit and only calls the pre-commit hook:$ git merge remotes/origin/feature/make-eggs $ git add readme.md $ git commit pre-commit hook [main 26262e1] Merge remote-tracking branch 'remotes/origin/feature/make-eggs'
When executing a "conflict-free" merge, there's no commit log message to obtain (fast-forward updates do not create a merge commit) which means the pre-merge-commit hook is skipped here as well.
The pre-merge-commit is getting called (also with custom
hooksPath
) as intended when a merge commit is generated, for example usinggit merge --no-ff
: $ git merge test --no-ff pre-merge-commit hook Merge made by the 'ort' strategy.
You can try using the prepare-commit-msg
hook. The second argument will be merge
"if the commit is a merge or a .git/MERGE_MSG
file exists". A non-zero exit status will abort the commit.
I don't think this will work with a fast-forward merge, since there won't be a commit message.
More info on hooks: https://www.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg
exit 1
at the top of prepare-commit-msg and perform a merge commit. –
Pitchstone printf -- '%s\n%s\n' '#!/bin/sh' 'echo nope && exit 1' > prepare-commit-msg
–
Rafflesia Another nice workaround would be to add a shell script, call it like you want, then add these lines to the script:
git() {
if [ "$1" == "merge" ]; then
echo "seems to work like a charme"
fi
command git "$@"
}
git "$@"
Then make an
alias git="./my-pre-merge-script.sh"
Then you are good to go. You just added your own pre-merge hook. I know, that you do not have access to whatever arguments git would pass to a real pre-merge hook, but you can prepare files or whatever you want to prepare for merge now; I personally am very happy with this approach: I spent 2 or 3 whole days to find something for pre-merge, then I had to go with the pre-commit-msg which I did not find accurate enough for my needs. This solves all my problems. Hope this helps anybody in the future.
merge
isn't always the first argument to git
: consider something like git -c foo=bar merge foobar
. –
Tyrus merge
? What if someone merges with git pull
instead of git merge
, or even with a custom git script? Your script needs to understand git to determine when the user is actually merging. –
Tyrus © 2022 - 2024 — McMap. All rights reserved.