问题
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
回答1:
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
).
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/20551613/git-pre-push-hook-dont-run-if-is-tags