20

so the git hook only puts change Id into commits. Although merge commits can be pushed to review branch even Gerrit is configured to require Change-Id in the commit messages. And when a merge commit has been pushed all the subsequent commit will depends on the merge commit - since there is no change Id. So what is the purpose not to include change Id into merge commit?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
laplasz
  • 3,359
  • 1
  • 29
  • 40

1 Answers1

22

The underlying problem in Git is that commit-msg hooks are not called for merge commits without conflicts.

However, the prepare-commit-msg is called for (any) merge commits. So what I currently do to get Gerrit's Change-Id also added to merge commits without conflicts is to use a prepare-commit-msg hook like this:

#!/bin/sh

if [ "$2" = "merge" -a -f .git/MERGE_MSG ]; then
    # Explicitly call Gerrit's commit-msg hook for merge commits.
    .git/hooks/commit-msg "$1"
fi

The check for .git/MERGE_MSG ensures that commit-msg will not be called if amending a merge commit, because in that case that hook is called directly by Git anyway. Note that for merge commits with conflicts this approach will result in commit-msg being called twice, once as part of this prepare-commit-msg hook and once by Git when it calls commit-msg after committing the conflicts resolution, but that does not cause any problems as Gerrit's commit-msg hook checks whether the Change-Id has already been added and does not add it again if that's the case.

If you're now asking your self why Gerrit does not simply use only a prepare-commit-msg hook to add the Change-Id, I guess that just to prevent the user from accidentally deleting the Change-Id from the commit message. Adding it as part of a commit-msg hook after the editor has closed is just safer.

sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • That's good, though it does have one minor disadvantage: the `commit-msg` hook strips the comments (`#` lines) from the original commit message, before it opens in your editor. – Richard Fearn Mar 15 '16 at 08:54
  • 1
    Out of curiosity, what comments would be there in the commit message in case of a non-conflicting merge? – sschuberth Mar 15 '16 at 09:44
  • The usual "Please enter a commit message..." and "Lines starting with..." comments. As I said, a minor disadvantage - but still, slightly different to the initial file you'd normally see in your editor. – Richard Fearn Mar 15 '16 at 16:42