git pre-push hook, don't run if is --tags

拟墨画扇 提交于 2019-12-10 13:37:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!