Tristan French
Tristan French

Reputation: 51

git "pre-push" hook stdin is empty

I'm trying to create a pre-push hook that will prevent the user from pushing to a branch other than the one they're on.

Based on the the pre-push.sample (and various threads I've read) it looks like I should be able to read which local branch I am pushing and the remote one I'm pushing to via stdin with:

while read local_ref local_sha remote_ref remote_sha
do
...
done

However when I do this, local_ref and the other variables are empty (even when running the unedited sample hook, it never enters the loop).

Is there some configuration or other step I need to do to access the variables at stdin?

I'm using git version 2.17.1

Upvotes: 5

Views: 572

Answers (2)

Johannes Filter
Johannes Filter

Reputation: 1933

Please check if you run commands before the loop that already read stdin. In my case, I had some commands from git lfs in my pre-push script. I had to move the loop before that command.

command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/pre-push'.\n"; exit 2; }
git lfs pre-push "$@"

Upvotes: 0

James Walsh
James Walsh

Reputation: 101

From running into this same issue myself I believe you're seeing the same thing I was, if the push contains no changes i.e. when you run git push you see "Everything up-to-date" then the input line from stdin will be blank, there has to be some change being pushed for that line to be populated.

Upvotes: 3

Related Questions