How to test git hooks

Errol picture Errol · Jul 16, 2012 · Viewed 20.6k times · Source

Since "testing" is a common use for a Git hook, my question is hard to search for.

I'm writing a fairly involved git post-receive hook and want to know what the best way to test it is. Currently my process is:

  • make changes to post-receive in a dummy "remote" repo
  • make a change to a dummy local repo
  • commit change in dummy local repo
  • push change to dummy remote repo

Is there any easier way to test this? Ideally it would look like:

  • make change(s) to post-receive in a dummy repo
  • issue "magic" command to test post-receive

Perhaps I can "reissue" a previous push or have the remote repo act as though it just received a push with a specific hash?

Answer

Lily Ballard picture Lily Ballard · Jul 16, 2012

Write a hook that just records its arguments/environment and dumps that to a file. Then you can just re-invoke the real hook at your leisure with the same environment/arguments and it will act as though you just re-issued the exact same push.