A Use For Git’s Post-Commit Hook

[Authored by Sam]

I love git and I love to be lazy, so lately I’ve been playing with git hooks.

Git’s default pre-commit hook is really handy, but today I’m in more of a post-commit kind of mood.  Like all git’s hooks it comes with an example script.

This one isn’t particularly useful.  It evaluates Nothing and returns a successful exit code.  Big deal.

Git’s default post-commit hook.

 

Enabling this git hook is as easy as making the file executable.

But that doesn’t change the fact that it does nothing.

I’m also a big fan of rtags.  It lets you jump directly to where a method is defined with one keystroke.  Sometimes though I forget to reindex my tag file for a while and I get the dreaded “tag not found” message.  Then I have to spend 30 seconds running the reindex command.  Fear no more, git hooks to the rescue.

A post-commit script for reindexing rtags.

So that was fun.  Now my rtags file is always up to date with my last commit.

But you may be saying, “What do I care about rtags?  I use TextMate and rtags only works with cool-kids editors like vi and emacs.”  Well, I have one more post-commit trick up my sleave.  Say after each commit you want to run your tests, unobtrusively in the background, and have Growl (you’re on OS X right?) warn you if you’ve checked in code with failing tests.

Run your tests after you commit and notify you unobtrusively using OS X growlnotify.

Anyone with any git hook gems, feel free to comment.

One thought on “A Use For Git’s Post-Commit Hook

  1. Say after each commit you want to run your tests, unobtrusively in the background, and have Growl (you’re on OS X right?) warn you if you’ve checked in code with failing tests.

    I advise against this. Use the pre-commit hook instead to run tests and abort the commit when a test fails. Why commit changes that introduce a preventable issue? Sure you can fix it, but better to prevent it. You can also mistakenly push your bad commit and you don’t want to rewrite history for others. Much safer to do this in pre-commit.

Leave a Reply