3

In our deployment process on of the last steps will be a merge of the release branch into the master branch.

I want to ensure via a hook, that each of these merges needs a manual message in the commit, so the person merging has the opportunity to write down what general changes will go into the master with this merge. Aka Change.log or release notes.

I found so far that the hook I would need would be the pre-merge. https://gitorious.org/git/mjg/commit/c15bfac6d3f5a28b3cd5745fef71e7f93bc1f8a4 It should only be activated when merging into the master branch. I guess it also should be called when a non-automatic commit into the master takes place.

Has anybody some hints how I can do that? A bash hook would be preferred, but php is also fine. Or I guess from other languages I can try to translate the concept to bash or php.

Thank you for any hints in advance!

Calamity Jane
  • 2,189
  • 5
  • 36
  • 68
  • 1
    Try with `git merge --no-ff`. This will always create a merge commit even when a `f`ast-`f`orward is possible. The editor would prompt for a message to the merge commit (with a pre-populated one). You can even set this in the config with `git config merge.ff false`. – Haralan Dobrev Mar 10 '14 at 10:24
  • I found a good post here, I guess that will help me with the problem. http://addamhardy.com/blog/2013/06/05/good-commit-messages-and-enforcing-them-with-git-hooks/ Now it leaves only the problem how I can get the information in the hook on which branch I am, so the hook should only be active when merging into master or even better when merging release into master. – Calamity Jane Mar 10 '14 at 12:05
  • 1
    Use `git rev-parse --abbrev-ref HEAD` for the branch name. http://stackoverflow.com/a/12142066/679227 – Haralan Dobrev Mar 10 '14 at 12:07
  • I will post the whole script as soon as I have figured out how all this information works successfully together. – Calamity Jane Mar 11 '14 at 10:13

1 Answers1

3

as promised here is my solution. Past this in your commit.msg hook and adapt to your needs.

#!/bin/bash
# give me the branch I am currently working on
mybranch=`git rev-parse --abbrev-ref HEAD`

# Depending on the branch we do different validation
if [ $mybranch = master ]; then
regex="^[^Merge|^#].{71,}"
message="ERROR: Commit message is too short."
else
regex="^(PMS|GKM)-[0-9]{3,4}\s\:\s.{10,}"
message="ERROR: Commit message is missing the Ticketnumber in the format GKM-nnnn: or PMS-nnnn :."
fi

test "" != "$(grep -E $regex "$1")" || {
    cat $1 >&2
    echo >&2 $message
    exit 1
}

Depending on the branch I choose different regular expressions and matching error messages.

This ensures for any branch not being master, that the message starts with a ticket number. For the master branch we don't need a ticket number, but a longer commit message.

Calamity Jane
  • 2,189
  • 5
  • 36
  • 68