I have to say I love subversion (svn) hooks. I’ve only been playing around with the concept fairly recently and I’m surprised that I lasted this long without them. I wanted to take a few minutes and share my favorite ways of using them. As well as a few that I don’t use but I think could be pretty helpful.
Before we keep going, if you don’t know what svn hooks are you can read up on them at http://svnbook.red-bean.com/en/1.1/ch05s02.html. svnbook.red-bean.com is my reference site of choice whenever I need to look up a svn command.
My #1, all-time, greatest in the world, bestest favorite is a svn pre-commit hook to syntax check my PHP code. There is nothing I hate more than a developer checking in code with syntax errors and breaking qa or stage or even, god-forbid, production. It’s as easy as setting up the hook to run a script that calls “/path/to/php -l /path/to/file.php” and on failure reject the commit. This way code that has syntax errors will never make it into SVN. Now if only open-source projects like ZendFramework did something like this before releasing their code. To be fair, the syntax errors are in their documentation not in the /library/ files but that’s still pretty dumb & annoying.
Another nice way to use a post-commit hook is to run something like PHP_CodeSniffer against the commit. CodeSniffer is an awesome PEAR package that scans your file and flags coding standards violations. It comes pre-packaged with sniffers for the PHPCS, PEAR, Squiz and Zend. Set it up to run and if it finds coding standard violations you can have the hook send out nasty little blame messages. ;) This isn’t something that I’d want to run on pre-commit just in case you really need to get something into production NOW. But if I hosted an open source project, I’d consider it.
Of course, let’s not forget about what is probably the most common svn hook; the post-commit email hook. I like svnmailer better than mailer.py that comes with svn by default. I think it’s easier to configure/change and to setup rules for how you want the emails to look or where to go.
If you’re using a ticket tracking system like Bugzilla or Trac that doesn’t have its own way of peeking into svn (like JIRA can with this plugin) you can write a post-commit hook to update the ticket when something is committed. You can see how Trac recommends you do it at http://trac.edgewall.org/browser/trunk/contrib/trac-post-commit-hook.
To help you on the way to creating your own svn hooks, tigris has a few hooks that are distributed with svn. They can be quite helpful so you should take a look at them.
Posted under PHP, Tips & Tricks