Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git-hooks pre-push script does not receive input via STDIN

Tags:

git

bash

githooks

The git-hooks pre-push documentation states that the first line of stdin will be populated with the local ref and sha, and the remote ref and sha as such:

<local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF

However, my simple pre-push script:

#!/bin/bash

echo "params=[$@]"

read line
echo "stdin=[$line]"

exit 1

returns the following output when a $git push is run:

params=[origin [url]:[branch].git]
stdin=[]
error: failed to push some refs to '[remote]'

The parameters to the script are as specified in the documentation (name and path of the remote). The error is expected because my script exits with a status of 1. However, I can't seem to figure out why I'm not receiving the local and remote refs on stdin as specified by the documentation.

Is this a bug in git? Or, am I missing something?

like image 608
dcow Avatar asked Mar 22 '14 23:03

dcow


People also ask

Can Git hooks be pushed?

Push hooksThe act of pushing to a repository is also a valid trigger. A pre-push hook is called with parameters, so for some values, you don't have to query Git the way you might for a pre-commit hook. The parameters are: $1 = The name of the remote being pushed to.

What is core hooksPath?

The core. hooksPath config allows you to set a directory where the hooks are located for a repository. This is particularly useful if trying to commit your hooks to the repository (e.g. for sharing hooks with the team).

What is pre-commit hook in Git?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


1 Answers

Apologies if this is stating the obvious, but if there's nothing to push, you won't get any lines on stdin. The sample .git/hooks/pre-push.sample has a while loop:

IFS=' '
while read local_ref local_sha remote_ref remote_sha
do
    ...
done

and that appears to work when I try it here with an echo inside that loop and nothing else - I get nothing when I have nothing to push, and output when I do.

like image 129
M Somerville Avatar answered Sep 17 '22 18:09

M Somerville