Enhancing git commit message template

My faveourite DVCS git has a method to generate (or at least prepopulate) your commit message. I'm going to tell you how it works.

We're using a principle of feature branches at work. Every ticket, that is created on bugtracker receives it's own git branch. Sometimes you have to see all changes related to one ticket. But branches would be merged to master one day sooner or later so commits from different branches would be mixed. To solve this we have a convention, that every commit message has to start with 'ticket:###'.

It is quite easy rule, but sometimes you forget to follow it (you're in a rush, or too sleepy etc). There's a simple answer for such things - automatization :) You can hook many different actions that happen in git. One of them is preparing commit message. So go to your project's dir. Then
cd .git/hooks/
create file prepare-commit-message
put the following in it:
TEMP=`mktemp /tmp/git-XXXXX`

TICKETNO=`git branch | grep '^\*' | sed 's/[tT_]/ /g' | cut -f 3 -d ' '`

(echo "ticket:$TICKETNO "; cat "$ORIG_MSG_FILE") > "$TEMP"
cat "$TEMP" > "$ORIG_MSG_FILE"
Code was taken and tweaked a bit from StackOverflow question

This will work if a branch is named so it matches regexp: t[0-9]+_.* (e.g. t777_implement_feature)

Everything is simple:
      1. Creating a temporary file
      2. Getting a ticket's number from branch
      3. Writing "ticket:" and ticket number to temporary file AND content of message template file (it maybe there with this script together)
      4. Making temporary file a file, that will be taken as a message template

And don't forget to make it runnable with:
chmod +x prepare-commit_message

There's a lot more interesting things, that you can do with git hooks. Just imagine: start tests on your testserver, automatic deployment, send status emails to your boss ;). Isn't it great?

No comments:

Post a Comment

Thanks for your comment!
Come back and check response later.