Jakub Gruszecki

Prepend ticket number from branch name to commit message

I work in a big company as my daily job. We are using JIRA as our ticketing system, and having a JIRA ticket number around is very convenient. We create a branch for every task that contains a task number. JIRA assigns numbers to tasks according to the following pattern ABCD-1234, where ABCD is a code of a project in JIRA, and appended number is a consequent number of task in that project. So a branch number for a task can look similar to this: bug/ABCD-1234-my-important-task.

When a branch is merged it is later deleted so we would normally loose an information what changes were introduced in what task. To keep track of that we are prepending a JIRA ticket number to every commit message we do in that branch ex. ABCD-1234 updated NUnit package. It has additional benefits as Atlasian tools have a lot of nifty integrations based on these numbers and our corporate overlords are can run all sorts of reports that are making them happy.

If you are working on a code base with a ticketing system in a larger organization it is likely that you work with a similar arrangement. The main problem with it is that you need to remember to add this numbers to every message and since humans are just humans many our team members (including me) kept forgetting about it.

To make life easier I come up with a simple git hook that will automatically extract a jira ticket number from current branch name and will prepend it to commit message on every commit:

#!/bin/sh
#

case "$2" in
    message )
        JIRA_ISSUE=$(git symbolic-ref --short HEAD | grep -oi '[A-Z]*-[1-9][0-9]*' | head -1)
        if [ ! -z "$JIRA_ISSUE" ]
            then
                echo -e "$JIRA_ISSUE $(cat $1)" > $1
        fi
        ;;
esac

Save this script as prepare-commit-msg in .git\hooks folder and voila! It will do the work for you.

Let’s look a bit on how it works.

Git defines a set of hooks which are triggered by various git operations. Each hook is just a bash script placed in a .git\hooks folder. One of these hooks is prepare-commit-msg which is run before commit is made and can change a commit message.

The hook receives in a parameters $1 a name of a file where a commit message is kept, and in $2 a value indicating how a commit message was made. Value message means that a commit message was passed in -m parameter, and that is the only case when we want to prepend a commit message. We don’t want to do that on merge or squash. If you want a different behvior check out the hooks documentation for other values or remove the case statement altogether to have a number prepended always.

Another important part is regex that searches for the ticket number in branch name: [A-Z]*-[1-9][0-9]*. If you are not using JIRA then you should modify it to match ticket numbers generated by your tool.

Then the script checks if a number was found and if yes then it adds it to commit message. If you commit directly to branches without a ticket number like develop or master (you shouldn’t do that though) then your commit messages won’t be modified.

Happy commiting! ;-)