git-hacking: pre-commit hook and custom command

Posted by Konrad 'ktoso' Malawski on 21/11/2010 – 03:07;

As my team is using a kinda weird source eclipse code formatter setup, that other IDEs can’t emulate in 100% I sometimes found myself committing in a file with only a few changed spaces. Of course, I could use “external-formatter” plugins etc but that’s no good. Running eclipse’s formatter each time I want to format my source (_very_ often) proved to be too slow and really getting on my nerves.

So… Since I’ve started using git-svn at work, I do have an possibility to make things better now! The answer lies in client-side-hooks and git aliases (“custom commands”). First let’s write this as an simple bash script, that we’ll locate in the .git/hooks/ folder:
It’s a fairly simple script, so I wont bore you explaining it in detail – if in doubt, feel free to ask. We only need to place this script as “.git/hooks/pre-commit” and make it executable and git will take care of the rest for us. Oh and in case you’re wondering where that org.eclipse.jdt.core.prefs came from. It’s created by eclipse into the .settings folder when you check some option to “use formatter per project” – that’s the only time I had to fire up and click around in eclipse to get it’s formatter running “headless”. Ok, let’s check if it works…

Yup, you can agree or not with the formatting rules – but what matters is that the whole team decided on it and that we keep it consistent throughout the whole project. This hook will definitely help with this. :-) Step two for me was changing this into a git command, so that I wouldn’t launch eclipse each time I just do some quick local commiting. The solution is to drop the hook idea (although it’s quite nice) and create an custom git command. We do this by doing an alias like that:

git config --global alias.eclipse-formatter '!~/git-hook-eclipse-formatter'

The !command alias support is with us in git since 1.5.0 and it enables us to launch any program/script as an git command. Great, just what I wanted! Let’s now see what this command really did:

As you can see, adding an alias is as simple as adding it to your ~/.gitconfig or, if you want to setup the alias “per project”, to .git/config – pretty cool, ay? Also, this command will be included in bash-autocompletition suggestions! :-)

That’s it for today… happy hacking!

Tags: , , , , , , , , ,

This post is under “coding, english, freedom, fun, java, terminal heroes” and has 3 respond so far.

3 Responds so far- Add one»

  1. 1. darkv Said:

    Very cool approach but this will only work as long as you are committing whole files, won’t it? What happens if you commit only part of the file and the reformatting shifts those lines?

  2. 2. Ktoso Said:

    True, that idea is only a good idea if you don’t do interactive adds – you could otherwise commit something that won’t yet compile, that gets formatted (super weirdly) adding non-sense changes and then formatting it well again after you’ve added all you wanted to…

    Anyway, thank goodness I’ve moved on to a team which cares about code style (and it’s actually quite “sane”), so I don’t use this trick anymore :-)

Post a reply