Snowing Code

Personal notes on software development


Git pre-push hook?

(Publish date: 23/12/2012)

So it seems that git doesn't have a pre-push hook, which could be used for running tests for example, making sure that no code would be pushed to your origin master without verifying the code doesn't fail any tests. Apparently, a couple of patches have been submitted (one of them was by Scott Chacon), but denied unfortunately. So I set out to find another way of doing just that. One of the ideas I had was using pre-commit and post-merge hooks to run only on master to make sure master is never soiled by code failing tests, but it just felt somewhat clumsy and a kind of a workaround, not a real solution.

I have therefore went with a different idea, inspired by what Chacon himself said on the thread linked above. The idea is simply using an alias such as 'send' to run a script and if the script (running the tests) ends successfully, we allow the push itself. Below are the steps to achieve this (for a .NET project, obviously):

  1. You'll need to make sure you've got ruby on your machine. While this is not obligatory, I prefer using ruby for any of my scripting needs.
  2. You need to have a 'global' directory where your unit tests runner lives and from where it'll be used on all of your projects. Let's say it'll be C:/nunit for the sake of this post.
  3. add the alias as seen below to your config file inside the .git folder of your project.
  4. Now, inside, say, c:/TestsRunners create a new file projectA_tests_runner.rb and add the following code:

This is pretty much it. From now on, whenever you wish to push your changes just use the send alias like so: git send origin master and you'll see your tests running; if they fail, the push will not go through, otherwise it'll all just work as expected.

Tags: git
blog comments powered by Disqus