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

Apply husky git hook to specific branch only #186

Closed
IAMtheIAM opened this issue Sep 19, 2017 · 7 comments
Closed

Apply husky git hook to specific branch only #186

IAMtheIAM opened this issue Sep 19, 2017 · 7 comments

Comments

@IAMtheIAM
Copy link

Is there a way to configure husky to apply a specific git hook, i.e. prepush ONLY to a specific branch i.e. master?

I know I can modify the hooks in the package, but that makes it more complicated because husky installs its own hooks, then I have to go overwrite it with a modified version using some scripts.

Mainly the purpose is to only run e2e or unit tests before pushing to master, but not every time I push to dev (which gets annoying waiting and seeing the browsers popup every time).

@IAMtheIAM
Copy link
Author

IAMtheIAM commented Sep 19, 2017

I modified the pre-push hook like this and it works like a charm

#!/bin/sh
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then

        # If we're on the Master branch, then Do something
        # Original Pre-push script from husky 0.14.3

      command_exists () {
        command -v "$1" >/dev/null 2>&1
      }

      has_hook_script () {
        [ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
      }

      cd "frontend"

      # Check if prepush script is defined, skip if not
      has_hook_script prepush || exit 0

      # Node standard installation
      export PATH="$PATH:/c/Program Files/nodejs"

      # Check that npm exists
      command_exists npm || {
        echo >&2 "husky > can't find npm in PATH, skipping prepush script in package.json"
        exit 0
      }

      # Export Git hook params
      export GIT_PARAMS="$*"

      # Run npm script
      echo "husky > npm run -s prepush (node `node -v`)"
      echo

      npm run -s prepush || {
        echo
        echo "husky > pre-push hook failed (add --no-verify to bypass)"
        exit 1
      }
    fi
done

Then I used this npm command line script to override the husky default hook

    "postinstall": "rimraf ../.git/hooks/pre-push && mklink /H \"../.git/hooks/pre-push\" \"config/git-hooks/pre-push.sh\"",

That deletes the original hook, then makes a hard link between the place where git looks for hooks and the new hook I created within my project directly. Works awesome.

It would be even nice if this can be some kind of config option. Just an idea. I hope this helps someone else.

@Asuza
Copy link

Asuza commented Dec 1, 2017

Could you also accomplish this by checking what branch you're in within your package.json file? Something along the lines of this pseudo-code:

"prepush": "if branch is master; then npm run master_prepush; fi",
"master_prepush": "echo 'cool master stuff'"

@dominik1001
Copy link

I ended up using https://github.com/kevinoid/git-branch-is.

"prepush": "if git-branch-is master; then yarn test; fi"

@budajeff
Copy link

budajeff commented Mar 6, 2019

When I try using git-branch-is i get the error "master was unexpected at this time." Any ideas?

"pre-commit": "if git-branch-is master; then lint-staged && git-authors-cli && npm run gen-docs && node tools/add-size-badge.js && git add package.json README.md docs/*; else echo 0; fi"

@giusnadd
Copy link

giusnadd commented May 24, 2019

@budajeff try this:
"pre-commit": "(git-branch-is master && lint-staged && git-authors-cli && npm run gen-docs && node tools/add-size-badge.js && git add package.json README.md docs/*) || true"

@denisk20
Copy link

denisk20 commented Oct 10, 2019

I ended up with this in order to limit the hooks to dev and master:

"husky": {
	"hooks": {
		"pre-commit": "branch=`git branch | grep \\* | cut -d ' ' -f2`;branchesToCheck=(\"dev\" \"master\"); [[ \" ${branchesToCheck[@]} \" =~ \" ${branch} \" ]] &&  yarn check-flow-lint || exit 0",
		"pre-push": "branch=`git branch | grep \\* | cut -d ' ' -f2`;branchesToCheck=(\"dev\" \"master\"); [[ \" ${branchesToCheck[@]} \" =~ \" ${branch} \" ]] &&  yarn check-flow-lint-test || exit 0"
	}
}

For some reason extracting into a variable didn't work for me (via $npm_package_myVar) so I had to duplicate large pieces of code.

@cope
Copy link

cope commented Nov 30, 2022

This worked for me.

I added this into .husky/pre-commit and .husky/pre-push files:

branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "whatever" ]; then
	echo "whatever branch is special, blabla..."
	...some package.json script calls, eg. pnpm format or pnpm test, etc...
fi

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

No branches or pull requests

8 participants