Lazy Developer’s Toolbox #1 - Prepend Git Commit Messages

Being a good citizen of the open source community means following the rules concerning software development defined by the team. One of them, sadly the one very frequently neglected, is meaningful commit message with a reference to the task you worked on. Such an info can be later leveraged by tools like FishEye, but that’s another story. So what’s the problem with that for lazy guys like me?

On my open source night shifts I’m mainly involved in Arquillian project (more about it soon). All our code is on GitHub. Each time I work on some task I’m creating a feature branch with the corresponding name (for instance ARQ-1186). When I’m done with the work (or consistent part of it) I commit my changes:

$ git commit -m"[ARQ-1186] Introduced new property in configuration  \
file to define default (global) transaction mode"

But there’s a little annoying problem with this… I’m not really that keen to memorize the issue number and type these few extra characters by hand each and every time (yes! I’m that lazy!). Obviously you can always customize your shell by using recipies from this blog for instance, but that only solves part of the problem. You don’t need to remember this bloody issue number, but you still need to type it.

So one day I was wondering if it would be possible to prepend commit message using branch information. It turned out to be damn easy.

Git hooks

Git is very powerful tool so no surprise that it has a concept of hooks. Hooks are scripts executed at the certain point of the git workflow. They can be written in any language your shell supports. In our case we are interested in executing script on client side before we commit. Let’s have a look what kind of local hooks we have in our repository’s .git/hooks folder :

├── applypatch-msg.sample
├── commit-msg.sample
├── post-update.sample
├── pre-applypatch.sample
├── pre-commit.sample
├── prepare-commit-msg.sample
├── pre-rebase.sample
└── update.sample

The names are rather self explanatory - we should use prepare-commit-message hook to achieve our goal.

A little bit of bash fu

In order to increase our productivity (or keep our lazy nature happy) we simply rename prepare-commit-msg.sample to prepare-commit-msg, paste the script listed below and ensure that the file is executable.

From now on we don’t need to remember the issue number anymore! Executing $ git commit -m”Introduced new property in configuration file \ to define default (global) transaction mode” will result with [ARQ-1186] Introduced new property in configuration file to define default (global) transaction mode as commit message. Life is so easier now ;)

Note: If you are using Eclipse git integration this approach won’t work. EGit is not aware of commit hooks.

Nov 7th, 2012