Alex Rivera | Logout

Git pre-push hooks

Asked 2010-11-16T16:03:10.233
161

I would like to run a unit-tests before every git push and if tests fails, cancel the push, but I can't even find pre-push hook, there is pre-commit and pre-rebase only.

Edit
Report

2 Answers

6

There isn't a hook for it, because a push isn't an operation that modifies your repository.

You can do the checks on the receiving side though, in the post-receive hook. That is where you would usually reject an incoming push. Running unit tests might be a little intensive to do in a hook, but that's up to you.

answered 2010-11-16T16:37:38.620
6

For the record, there is a patch to Git 1.6 that adds a pre-push hook. I don't know whether it works against 1.7.

Rather than mess with that, you could run push script like @kubi recommended. You could also make it a Rake task instead so it's in your repo. ruby-git could help with this. If you check the target repo, you could run tests only when pushing to the production repo.

Finally, you could run your tests in your pre-commit hook but check for what branch is being committed to. Then you could have a, say, a production branch that requires all tests pass before accepting a commit but your master doesn't care. limerick_rake may be useful in that scenario.

answered 2011-02-17T16:55:40.687

Your Answer