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
Comments
|
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 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. |
|
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: |
|
I ended up using https://github.com/kevinoid/git-branch-is.
|
|
When I try using
|
|
@budajeff try this: |
|
I ended up with this in order to limit the hooks to For some reason extracting into a variable didn't work for me (via |
|
This worked for me. I added this into |
Is there a way to configure husky to apply a specific git hook, i.e.
prepushONLY 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).
The text was updated successfully, but these errors were encountered: