5

Using strace I noticed that git-revert invokes only two hooks:

  • prepare-commit-msg
  • post-commit

But git-commit invoke these four:

  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit

Since git-revert produces a commit, why doesn't it invoke the same hooks as git-commit?

I asked this on the Git mailing list but got no response. So I'm asking here.

I ended up researching this when I was implementing a hook to detect and deny commits which revert merge-commits, since they are troublesome.

I tried to implement it as a commit-msg hook to search for the string "This reverts commit SHA-1" in the commit message. But git-revert doesn't invoke the commit-msg hook.

So, for now I implemented my check as a pre-receive hook. But I find it useful to have all pre-receive checks implemented also as a pre-commit or a commit-msg hook so that I can detect problems at commit time instead of only at push time.

2
  • 1
    Just to be clear, is the issue that the revert commit is not triggering a client side hook, or a server side hook? I would think that you could create a server side hook which could detect such a commit and then block it. But +1 to a great question. Feb 18, 2018 at 14:54
  • The issue is that the command git-revert doesn't invoke all client side hooks that the command git-commit invokes
    – Gnustavo
    Feb 18, 2018 at 18:59

1 Answer 1

2

The sequencer code actually runs git commit to produce the commit. But this code (see link above) contains the fragment:

    if (!(flags & VERIFY_MSG))
            argv_array_push(&cmd.args, "-n");

which means it runs git commit -n by default. The -n flag:

bypasses the pre-commit and commit-msg hooks

as noted in the git commit documentation. The things that set VERIFY_MSG are interactive rebase "edit" or "reword" and the like, but not git cherry-pick.

Note that once the sequencer is done, it will invoke your post-rewrite hook (if you have one), so you should put your check there instead. As you note, you can specifically edit the commit message, and if you do so, it seems like your commit messge hook ought to be used, but it isn't.

2
  • Your lead allowed me to find why the git-commit is being invoked with the -n flag by git-revert. It happened on commit 9fa4db544e2e, by Junio Hamano, in 2005! I got it, but since you can edit the commit message in a git-revert, I think there's a case to be made to make the pre-commit and the commit-msg being invoked by default. Anyways, I'll bring this discussion to Git's mailing list. However, I checked and git-revert doesn't invoke the post-rewrite hook. Even if it did, since it's a "post" hook it wouldn't be able to abort the command. But you answered my question. Thanks!
    – Gnustavo
    Feb 19, 2018 at 13:18
  • Oops, right, the post-rewrite hook invocation is only if the sequencer is being used for git rebase -i.
    – torek
    Feb 19, 2018 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.