How to test git hooks
Asked Answered
A

5

46

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?

Adherence answered 16/7, 2012 at 20:38 Comment(0)
P
16

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.

Prevocalic answered 16/7, 2012 at 20:47 Comment(4)
That's about what I did but instead by manually inspecting (echoing) the relevant variables.Adherence
what would such a "dumper" hook look like?Westmorland
@JonWatson, I just used echo $@;echo; pwd; echo; setShooter
This may be obvious to some, but ensure your hook script is executable! I created mine in a text editor, rather than copying the .sample, and it did not execute.Makkah
F
21

Answer this four-years-old question.

If you'd like to test hook, you need to test in local environment first, I give the detail commands for following up, use post-receive as sample:

$ mkdir /tmp/hook_test
$ cd /tmp/hook_test

$ # set local git repo, where you put hooks in it.
$ git clone --bare https://github.com/git/git.git

$ # set develop environment which is cloned from the new created repo. 
$ git clone git.git repo 
    
$ # copy and rename the hook you need test to "post-receive"
$ cd git.git/hooks
$ cp ~/post-receive-test post-receive

$ # suppose the hook script is bash script.
$ # edit "post-receive" and add "set -x" to second line in it to active debug

$ cd /tmp/hook_test/repo
$ # emulate a hook trigger, do some changes, "git add" and "git commit" it 

$ git push
 
$ # Now you should see the script "post-receive" runs automatically with debug details.

You should be free to run git push, that the updates are only pushed to local repo /tmp/hook_test/git.git

Fatigue answered 5/9, 2016 at 2:12 Comment(1)
I did all of these steps, and I can see the command touch file, but I can't see the file is created in the test repoHamal
P
16

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.

Prevocalic answered 16/7, 2012 at 20:47 Comment(4)
That's about what I did but instead by manually inspecting (echoing) the relevant variables.Adherence
what would such a "dumper" hook look like?Westmorland
@JonWatson, I just used echo $@;echo; pwd; echo; setShooter
This may be obvious to some, but ensure your hook script is executable! I created mine in a text editor, rather than copying the .sample, and it did not execute.Makkah
D
8

My approach is to dial the HEAD at the remote repo back one commit, and then push again:

ssh <repo> 'cd /<repo_path>; git update-ref refs/heads/master HEAD^' && git push origin master
Disagreeable answered 7/12, 2018 at 20:30 Comment(1)
Perfect, just what I needed.Contrabass
E
5

For debugging you could also end your hook in something like this:

echo "No errors found."
exit 1

If you are happy with you hook you of course take out the last line again.

Exoskeleton answered 3/4, 2020 at 13:0 Comment(0)
C
0

The original question was about post-receive hook, but I have a solution that should work for some other hooks, such as pre-push.

Some git commands have a --dry-run parameter:

$ git push --help | grep -A2 -- --dry-run
…
       -n, --dry-run
           Do everything except actually send the updates.

$ git pull --help | grep -A2 -- --dry-run
       --dry-run
           Show what would be done, without making any changes.

$ git commit --help | grep -A2 -- --dry-run
…
       --dry-run
           Do not create a commit, but show a list of paths that are to be
           committed, paths with local changes that will be left uncommitted

Thus, you can test a pre-push hook by running git push --dry-run. It will run the hook, but won't push any changes.

This may also work for some other commands and hooks. But I understand this isn't a general answer. (e.g. It doesn't apply to post-receive.)

Corneous answered 24/6 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.