catching a git post-receive error in a script
Asked Answered
O

2

10

In a bash script I do a

git push

and I check its exit status.

On the remote server there's a post-receive hook which does a few things. If an error occurs the post-receive hook will exit with a non-zero value.

However when the post-receive hook errors out, git push exits normally. Unless I'm specifically checking the output for specific error strings (which I'm not) my script thinks everything went ok.

Is there an easy way for me to determine if the post-receive hook failed?

Orangewood answered 18/10, 2012 at 18:18 Comment(2)
What about examining the output of the git push command rather than just its exit status? Perhaps the hook failure is detectable somewhere in the output which is formatted as: <flag> <summary> <from> -> <to> (<reason>). Just thinking out loud...Bissonnette
That's what I actually want to avoid. But if I can't make git exit with an error when post-receive errors, then I guess that's what I'll have to do.Orangewood
O
5

So the answer is no there is no way to easily check if the post-receive hook failed. The best you can do is have your script check for output from remote and make sure your post-receive hook echos an error message you're looking for.

In my case a pre-receive hook won't work since I'm pushing to another backup repo and the new commit has to be accepted before it can be pushed.

Basically the post-receive hook should do something like:

 some-command-that-might-fail

 RC=$?

 if [ $RC -eq 0 ]; then
echo -e "\nERROR: some-command-that-might-fail FAILED!!!!!!!! PANIC!!!!!!\n"
 fi

Then the script doing the push should grep the output for FAILED or ERROR or PANIC and report the post-receive error.

Orangewood answered 25/10, 2012 at 20:8 Comment(0)
N
0

Quoting from http://www.kernel.org/pub/software/scm/git/docs/githooks.html#post-receive:

post-receive
...
This hook does not affect the outcome of git-receive-pack, as it is called after the real work is done.

Maybe the pre-receive hook is better suited for your purpose, although there's no mention of a return code transmitted.

Ninetieth answered 19/10, 2012 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.