Mercurial Precommit Hooks

18 Dec 2014

As our development team has expanded over the last year, debug information has crept into the repository on more than one occasion.

There are two lines of code that our developers frequently use to debug during development that should never be committed.

The first is binding.pry.

Pry is a powerful alternative to the standard IRB shell for Ruby. We use it to stop the execution of code and drop into an interactive ruby shell at any position in the codebase. It’s really useful for development, but if there is a rogue binding.pry left anywhere in the code after it has been pushed to a development server, execution will stop and the server will hang! A number of times while working with another developers code, I have been left scratching my head for a few minutes as to why the page was not loading.

The second line that can be a problem is focus: true.

We use this in our Rspec tests to tell Guard to only run the particular tests we want to work with. It stops Rspec from running your full test suite, which is great when you are developing a new feature. It’s not so great when someone else has left a focus: true in the repository and after you pull the latest code, your tests run suspiciously fast!

To stop myself accidentally commiting these two lines into our Mercurial repository, I’ve added the following to my ~/.hgrc file:

precommit.removeDebug = hg diff -S | grep -v '^-' | (! egrep 'binding.pry')
precommit.removeFocus:True = hg diff -S | grep -v '^-' | (! egrep 'focus: true')

Check out the hgbook for more information on using hooks with Mercurial.

author image

Peter Armstrong

Peter is a software engineer at OnePageCRM.