observing / pre-commit

Automatically installs a git pre-commit script in your git repository which runs your `npm test` on pre-commit
MIT License
1.88k stars 150 forks source link

Only run commit hook on master #49

Closed shawn-simon closed 9 years ago

shawn-simon commented 9 years ago

Is there a way to configure this to only run the commit hooks on master?

mroderick commented 9 years ago

Is there a way to configure this to only run the commit hooks on master?

That seems like a really bad idea. You'll want to verify things as early as possible. By the time things get to master they should be clean.

I supposed this is related to #50, check out the script to only verify staged changes.

shawn-simon commented 9 years ago

Yes, it was more because of the performance issue- the scripts taking so long to run. I think I'll make it so on master it forces to run on the whole repo, but outside of the branch, only on staged changes. I'll figure it out from here. Thanks again!

shawn-simon commented 9 years ago
#!/bin/bash

git stash -q --keep-index

branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ "$branch" ==  "master" ]; then
    echo 'Commit on master: Running jshint on entire depo.'
    $(npm bin)/jshint .

else
    echo 'Commit on non-master branch: Running jshint on current changeset.'
    git diff-index --cached HEAD --name-only --diff-filter ACMR | egrep '.js$' | xargs $(npm bin)/jshint
fi

RESULT=$?

git stash pop -q

[ $RESULT -ne 0 ] && exit 1
exit 0

final script we went with for future googlers