# The commit-msg hook is not invoked by git-merge as specified in documentation

5 messages from 2020-06-03 to 2020-06-04. Participants: Kalle Kromann, Junio C Hamano.
Thread: https://gitlist.dev/t/53606

## Kalle Kromann, 2020-06-03 12:46

Subject: The commit-msg hook is not invoked by git-merge as specified in documentation
Message-ID: <AM0PR08MB55379DD182941E93F3BD3D37D7880@AM0PR08MB5537.eurprd08.prod.outlook.com>
URL: https://gitlist.dev/e/AM0PR08MB55379DD182941E93F3BD3D37D7880%40AM0PR08MB5537.eurprd08.prod.outlook.com

```
Hello,

At least two places in the git docs it is specified that the hook "commit-msg" is invoked by both git-commit and git-merge.

https://git-scm.com/docs/githooks#_commit_msg
https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---no-verify

I'm experiencing a different behavior however. The hook seems to work fine on git-commit, but is not invoked by git-merge.
When I use git-merge my text editor opens with the default message, but when I close the editor the hook is not invoked as I would expect.

For reference, the prepare-commit-msg hook works with both commands as expected.

Is there something subtle I'm overlooking?
Of course a fast forward merge should not invoke the commit-msg hook, because no commit is made.
But in my tests with non FF-merges the hook is still not invoked.

Kind regards,
Kalle Kromann
This is an e-mail from MAN Energy Solutions, branch of MAN Energy Solutions SE, Germany (CVR no. 31611792).
This e-mail (including any attachments) is confidential and may be privileged. If you have received this e-mail by mistake, please notify the sender by e-mail and delete it from your system.
Any unauthorized use or dissemination of this e-mail or its contents in whole or in part is strictly prohibited. Please note that e-mails are susceptible to change.
MAN Energy Solutions SE (including its group companies) shall not be liable for improper or incomplete transmission of the information contained in this e-mail or for any delay in its receipt.
MAN Energy Solutions SE (or its group companies) does not guarantee that this e-mail has not been compromised or that it is free of viruses, interceptions or interference.

```

## Junio C Hamano, 2020-06-03 19:28

Subject: Re: The commit-msg hook is not invoked by git-merge as specified in documentation
Message-ID: <xmqq367crlti.fsf@gitster.c.googlers.com>
URL: https://gitlist.dev/e/xmqq367crlti.fsf%40gitster.c.googlers.com
In-Reply-To: <AM0PR08MB55379DD182941E93F3BD3D37D7880@AM0PR08MB5537.eurprd08.prod.outlook.com>

```
Kalle Kromann <kalle.kromann@man-es.com> writes:

> For reference, the prepare-commit-msg hook works with both commands as expected.
>
> Is there something subtle I'm overlooking?

On what platform, and which version of Git are we talking about?
What is the difference between these two hooks?  I.e. perhaps

    $ ls -l .git/hooks/prepare-commit-msg .git/hooks/commit-msg

tells you some subtle differences between the two?

> Of course a fast forward merge should not invoke the commit-msg
> hook, because no commit is made.  But in my tests with non
> FF-merges the hook is still not invoked.

Other than the last one, nothing comes to my mind.  And it does not
reproduce for me.

I just did this:

    $ echo exit 1 >.git/hooks/commit-msg
    $ chmod +x .git/hooks/commit-msg
    $ git checkout master^0
    $ git merge ds/line-log-on-bloom
	    ... editor launches and I save the message ...
    Auto-merging revision.c
    Auto-merging line-log.c
    Auto-merging bloom.h
    Auto-merging bloom.c
    Not committing merge; use 'git commit' to complete the merge.

The last line comes from builtin/merge.c::abort_commit(), which is
called from this piece of code in builtin/merge.c::prepare_to_commit()

	if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
			    git_path_merge_msg(the_repository), "merge", NULL))
		abort_commit(remoteheads, NULL);
	if (0 < option_edit) {
		if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
			abort_commit(remoteheads, NULL);
	}

	if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
					  "commit-msg",
					  git_path_merge_msg(the_repository), NULL))
		abort_commit(remoteheads, NULL);

I do not have prepare-commit-msg, and my launch_editor() thing
successfully returned, and the call to abort_commit() is cleraly
coming from the last line, where run_commit_hook() noticed that the
commit-msg hook ran and reported a failure.

After this, I did this to just confirm:

    $ echo exit 0 >.git/hooks/commit-msg
    $ git reset --hard
    $ git merge ds/line-log-on-bloom

This time, instead of "Not committing...", I got

    Merge made by the 'recursive' strategy.

followed by the usual diffstat.

The above demonstrates that commit-msg does run well enough to cause
its exit status affects the outcome in both cases.

For fun, I tried another.

    $ echo exit 1 >.git/hooks/commit-msg
    $ chmod -x .git/hooks/commit-msg
    $ git reset --hard master
    $ GIT_EDITOR=: git merge ds/line-log-on-bloom
    Auto-merging revision.c
    Auto-merging line-log.c
    Auto-merging bloom.h
    Auto-merging bloom.c
    hint: The '.git/hooks/commit-msg' hook was ignored because it's not set as executable.
    hint: You can disable this warning with `git config advice.ignoredHook false`.
    Merge made by the 'recursive' strategy.
    ... diffstat follows ...

So...


```

## Kalle Kromann, 2020-06-04 08:27

Subject: RE: The commit-msg hook is not invoked by git-merge as specified in documentation
Message-ID: <AM0PR08MB553711CE7CE5D39EC2CE8422D7890@AM0PR08MB5537.eurprd08.prod.outlook.com>
URL: https://gitlist.dev/e/AM0PR08MB553711CE7CE5D39EC2CE8422D7890%40AM0PR08MB5537.eurprd08.prod.outlook.com
In-Reply-To: <xmqq367crlti.fsf@gitster.c.googlers.com>

```
Hello, thanks for the reply.

Sorry for not including it originally, the platform I was using to test was WSL (Windows subsystem for Linux).
And the git version was the latest available from apt-get in WSL, 2.7.4.

It seems like one of these things is affecting the execution of the hook.
I just did the exact same test in Cygwin with git version 2.17 and it works as expected here.

Do you know if it's a recent change that git-merge should invoke the hook as well?
Or do you think it is related to using git in WSL?

Thanks again!
This is an e-mail from MAN Energy Solutions, branch of MAN Energy Solutions SE, Germany (CVR no. 31611792).
This e-mail (including any attachments) is confidential and may be privileged. If you have received this e-mail by mistake, please notify the sender by e-mail and delete it from your system.
Any unauthorized use or dissemination of this e-mail or its contents in whole or in part is strictly prohibited. Please note that e-mails are susceptible to change.
MAN Energy Solutions SE (including its group companies) shall not be liable for improper or incomplete transmission of the information contained in this e-mail or for any delay in its receipt.
MAN Energy Solutions SE (or its group companies) does not guarantee that this e-mail has not been compromised or that it is free of viruses, interceptions or interference.

```

## Junio C Hamano, 2020-06-04 15:11

Subject: Re: The commit-msg hook is not invoked by git-merge as specified in documentation
Message-ID: <xmqq8sh2rhm5.fsf@gitster.c.googlers.com>
URL: https://gitlist.dev/e/xmqq8sh2rhm5.fsf%40gitster.c.googlers.com
In-Reply-To: <AM0PR08MB553711CE7CE5D39EC2CE8422D7890@AM0PR08MB5537.eurprd08.prod.outlook.com>

```
Kalle Kromann <kalle.kromann@man-es.com> writes:

> And the git version was the latest available from apt-get in WSL, 2.7.4.

Wow, that's ancient.  

"git merge" started calling commit-msg hook f8b86359 (builtin/merge:
honor commit-msg hook for merges, 2017-09-07), which first appeared
in 2.15 (from Oct 2017, that is also ancient in Git timescale) IIUC.



```

## Kalle Kromann, 2020-06-04 15:14

Subject: RE: The commit-msg hook is not invoked by git-merge as specified in documentation
Message-ID: <AM0PR08MB55374383893333BEA293041ED7890@AM0PR08MB5537.eurprd08.prod.outlook.com>
URL: https://gitlist.dev/e/AM0PR08MB55374383893333BEA293041ED7890%40AM0PR08MB5537.eurprd08.prod.outlook.com
In-Reply-To: <xmqq8sh2rhm5.fsf@gitster.c.googlers.com>

```
Thank you so much for your help.

I'm still quite new to git, and this was definitely a rookie mistake.
This is an e-mail from MAN Energy Solutions, branch of MAN Energy Solutions SE, Germany (CVR no. 31611792).
This e-mail (including any attachments) is confidential and may be privileged. If you have received this e-mail by mistake, please notify the sender by e-mail and delete it from your system.
Any unauthorized use or dissemination of this e-mail or its contents in whole or in part is strictly prohibited. Please note that e-mails are susceptible to change.
MAN Energy Solutions SE (including its group companies) shall not be liable for improper or incomplete transmission of the information contained in this e-mail or for any delay in its receipt.
MAN Energy Solutions SE (or its group companies) does not guarantee that this e-mail has not been compromised or that it is free of viruses, interceptions or interference.

```
