Skip to content

Instantly share code, notes, and snippets.

@stevermeister
Created March 12, 2015 15:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevermeister/e530409b19daac932ee2 to your computer and use it in GitHub Desktop.
Save stevermeister/e530409b19daac932ee2 to your computer and use it in GitHub Desktop.
git pre-push hook to run tests
#!/bin/sh
# check for how many uncommitted changes we have
# stash changes
# run grunt task
# restore stashed files if anything was stashed
# exit with error if grunt fails
NAME=$(git branch | grep '*' | sed 's/* //')
echo "Running pre-push hook on: " $NAME
# don't run on rebase
if [ $NAME != '(no branch)' ]
then
CHANGES=$(git diff --numstat | wc -l)
CHANGES_CACHED=$(git diff --cached --numstat | wc -l)
TOTAL_CHANGES=$(($CHANGES + $CHANGES_CACHED))
git stash -k # the "-k" makes git stash all changes, staged & unstaged
grunt test
RETVAL=$?
if [ $TOTAL_CHANGES -ne "0" ]
then
echo "Popping" $TOTAL_CHANGES "changes off the stack..."
git stash pop -q
fi
if [ $RETVAL -ne 0 ]
then
echo "Grunt task failed, exiting..."
exit 1
fi
echo "Complete."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment