Thursday 24 January 2013

Git Tip: Get A Warning When A File Has Changed

Every project has one1. The file. If that file changes, you need to know, because if you don't then your life is going to be very unpleasant in very short order.

At Hogarth, that file is called development.conf. It is, roughly speaking, a dump of the way our various elements and components are wired together2, and as it's completely unmergable only one person can be working on it at any one time (something which we're working to fix). If you want to work on it, you need to make sure that you have the latest version in the database, so that when you dump out your new version, it includes all previous changes.

At various times, we've all been caught out by missing an update, and had to completely redo (often complex) changes just to incorporate a (often minor) previous change. One of my colleagues (Patrick), having just been caught out by this for (I think) the first time, suggested that it would be useful to get a warning when this changes. He reasoned that as we use git to manage all change to our codebase, it would be natural to write a git hook which did this for us.

We had a look together at the list of git hooks, and couldn't really see anything appropriate. post-merge seemed like what we wanted, but we couldn't work out how we could determine what had actually changed. So Patrick went off to do some real work, and I turned to the ever-reliable #git IRC channel on Freenode. ojacobson suggested a solution, which works beautifully.

To understand how this works, you'll need to know about the git reflog. +Alex Blewitt has a good introduction here. If you do one thing as a result of this article, educate yourself about the reflog. It's incredible!

The important take-away from that article is that:
git diff "HEAD@{1}"
will show you the diff between what you currently have in your tree and the commit before the last action that changed your history. Importantly, it treats merges as a single entry, so if you are immediately post merge, running the above command will show you a diff containing everything that changed in that merge, regardless of the number of commits the merge contained.

Pulling these various bits together, with some shell wizardry, gives us:
set -eu

git diff "HEAD@{1}" --name-only | grep config/development.conf 2>&1 > /dev/null

if [ $CHANGED ]; then
    echo -e "\e[41m!!! development.conf HAS CHANGED !!!\033[0m"
    echo "You should reload config using (something like):"
    echo "  ./ loadconfig config/development.conf"
We check if anything has changed, and if it has then we print out a BRIGHT RED WARNING MESSAGE. Drop that in .git/hooks/post-merge, make it executable, and you're good to go.

1 Well, maybe not every project. If you don't, I'm jealous.  But a lot of "enterprise" or otherwise unloved codebases will have one. And, more pertinently, ours does.

2 You can tell that ours is not a Java project because this is not an XML file.


  1. Nice tip thanks!
    Can the hooks be versionned and shared between developers ?

    1. put them somewhere and use symbolic links.

      chmod +x githooks/post-merge
      git add githooks/post-merge
      git commit -m "added post-merge git hooks"
      cd .git/hooks
      ln -s ../../githooks/post-merge .

  2. Why not use it in pre-push instead of post-merge ? Just curious why you recommend post merge specifically.

  3. This comment has been removed by the author.

  4. It is a extremely convenient way to access favourite video games for gamers worldwide. To play for real money, instant play is just obtainable after creating an account. Following a set of pointers for slot game development will ensure the the} success of the slot game. This slot machine tutorial 아인 카지노 has detailed the steps needed for slot game development.