git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 3, 2020, 19:28 UTC
Message-ID
<xmqq367crlti.fsf@gitster.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...
Previous: Kalle KromannNext: Kalle Kromann
Message 2 of 5 in “The commit-msg hook is not invoked by git-merge as specified in documentation”
  1. Kalle KromannJun 3, 2020
  2. Junio C HamanoJun 3, 2020
  3. Kalle KromannJun 4, 2020
  4. Junio C HamanoJun 4, 2020
  5. Kalle KromannJun 4, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.