Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-push hook missing stdin values #147

Closed
ThisIsMissEm opened this issue Aug 19, 2020 · 7 comments · Fixed by #732
Closed

pre-push hook missing stdin values #147

ThisIsMissEm opened this issue Aug 19, 2020 · 7 comments · Fixed by #732

Comments

@ThisIsMissEm
Copy link
Contributor

According to the git hooks documentation, pre-push receives a line of input from stdin, which gives the branch names:

This hook is called by git-push[1] and can be used to prevent a push from taking place. The hook is called with two parameters which provide the name and location of the destination remote, if a named remote is not being used both values will be the same.
Information about what is to be pushed is provided on the hook’s standard input with lines of the form:

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

For instance, if the command git push origin master:foreign were run the hook would receive a line like the following:

refs/heads/master 67890 refs/heads/foreign 12345

However, the script that lefthook uses for executing pre-push doesn't forward this input through to the script.

I'm not sure what the right way to fix this is?

@ThisIsMissEm
Copy link
Contributor Author

So, I managed to get this working with the following git hook:

#!/bin/sh

if [ "$LEFTHOOK" = "0" ]; then
  exit 0
fi

debug () {
  if [ "$LEFTHOOK_DEBUG" = "true" ] || [ "$LEFTHOOK_DEBUG" = "1" ]; then
    echo "lefthook:debug $1"
  fi
}

hookName="$(basename "$0")"

cmd="lefthook run $hookName $@"

case $hookName in
  "pre-push"|"post-rewrite")
    stdin="$(cat)";;
esac

debug "$hookName"
debug "stdin: $stdin"
debug "cmd: $cmd"

if [ -t 1 ] ; then
  exec < /dev/tty ; # <- enables interactive shell
fi

if lefthook -h >/dev/null 2>&1
then
  debug "eval"
  eval $cmd <<< $stdin
elif bundle exec lefthook -h >/dev/null 2>&1
then
  debug "bundle"
  bundle exec $cmd <<< $stdin
elif npx lefthook -h >/dev/null 2>&1
then
  debug "npx"
  npx $cmd <<< $stdin
elif yarn lefthook -h >/dev/null 2>&1
then
  debug "yarn"
  yarn $cmd <<< $stdin
else
  echo "Can't find lefthook in PATH"
fi

Note: rather than outputting a templated file with the hook name as the .git/hooks/<name> file, you can just grab the hook name from the file name that is currently being executed; that's what I've done above.

There's probably a better way to solve this.

@ThisIsMissEm
Copy link
Contributor Author

As a further note pre-push and post-push are the only two that can receive stdin when executed by git, however, git isn't always going to give you information via stdin, despite what the documentation on the pre-push hook says. If your local branch is up-to-date with the remote branch, then no line for that will be printed to stdin, which basically means you can't really use this for validating git branch names pre-push

n.b., it'd be wrong to use git rev-parse HEAD here, which is what other tutorials suggest as you may be pushing to a renamed branch (i.e., git push origin local:remote, you wanna verify either local or remote, in my case remote, not what the current HEAD is

So, end of day, whilst executing pre-push hooks with lefthook is currently broken, even if they're fixed, there's an issue in git which causes the pre-push hook to not run as expected, hence you can't use pre-push for validating branch names.

@sanmai-NL
Copy link
Contributor

@ThisIsMissEm A number of changes to stdin handling have been implemented in the meantime. Is this issue this current?

@ThisIsMissEm
Copy link
Contributor Author

No idea, I don't use the project anymore (i've changed companies twice since then!)

@carhartl
Copy link

It works now, I had changed our pre-push hooks the other day! Thank you 🙏 @sanmai-NL

@sanmai-NL
Copy link
Contributor

@ThisIsMissEm This can be closed now then, right? @carhartl See #732

@carhartl
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants