5

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

1
  • Can you show us (a) exactly what your pre-push script looks like and (b) how you're resting it? The pre-push script does receive that information on stdin; without more context that loop looks fine. Commented Mar 12, 2019 at 2:42

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 "$@" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.