Question

I have a prepush hook that tests my code, however, it also runs when I do a git push --tags. Is there any way to avoid this?

Maybe there's some way to check if its a regular push or if its a --tags push?

Update - These are the only arguments I was able to find:

  • $1 is the name of the remote
  • $2 is url to which the push is being done
Was it helpful?

Solution

I have a solution to this, but it's really kludgey. A while back, I set up a pre-commit hook to stop me from accidentally using -a when I have files staged. My solution is to read the command that invoked the original git command (probably only works on linux too).

while read -d $'\0' arg ; do
    if [[ "$arg" == '--tags' ]] ; then
        exit 0
    fi
done < /proc/$PPID/cmdline
# and perform your check here

Original

That being said, try calling env in the hook; git sets a few extra vars (starting with GIT_ prefixes, like GIT_INDEX_FILE).

OTHER TIPS

You can put this in your push hook before anything you don't want to run:

# If pushing tags, don't test anything.
grep "refs/tags/" < /dev/stdin > /dev/null
if [ "$?" -eq "0" ] ; then
    exit 0
fi

If the first line of stdin refers to a tag, it'll exit 0 and push. It's not perfect because if you're pushing a tag and a branch at the same time, it might see the tag first and not run the rest of the hook. But it'll work in most cases.

You can use env variable to control it:

#.git/hooks/pre-push
if [[ $SKIP_HOOKS ]]; then
    exit 0
fi
# do things you want...

and run command like this:

SKIP_HOOKS=true git push --tags

You are looking for the --no-verify flag. So:

git push --tags --no-verify

This is what git help push tells you about that flag:

       --[no-]verify
           Toggle the pre-push hook (see githooks(5)). The default is --verify, giving the hook a chance to prevent the push.
           With --no-verify, the hook is bypassed completely.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top