Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 239
GxXc Avatar asked Dec 12 '13 18:12

GxXc


People also ask

Are tags pushed with git push?

Sharing tags is similar to pushing branches. By default, git push will not push tags. Tags have to be explicitly passed to git push .

How do you ignore a pre-commit hook?

Quick tip if you want to skip the pre-commit validations and quickly want to get a commit out there. To get your commit through without running that pre-commit hook, use the --no-verify option. Voila, without pre-commit hooks running!

How do you enforce a pre-commit hook?

If you want enforcement, use an update hook in the central repo. If the hook is doing per-commit verification, you can still provide a pre-commit hook; developers will likely adopt it voluntarily, so that they can find out right away when they've done something wrong, rather than waiting until they try to push.

How pre-commit hook works?

Committing-Workflow Hooks The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


3 Answers

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).

like image 85
forivall Avatar answered Sep 21 '22 10:09

forivall


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
like image 38
Million Avatar answered Sep 18 '22 10:09

Million


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.
like image 35
Sil Westerveld Avatar answered Sep 19 '22 10:09

Sil Westerveld