Skip to content

Instantly share code, notes, and snippets.

@jredh
Forked from cvogt/pre-commit-hook-install.sh
Last active November 6, 2018 21:38
Show Gist options
  • Save jredh/e1af2f274889d9ec780dc86603150e6e to your computer and use it in GitHub Desktop.
Save jredh/e1af2f274889d9ec780dc86603150e6e to your computer and use it in GitHub Desktop.
git pre-push hook for Scala test passing
#!/bin/sh
cd "$(dirname "$0")"
touch .git/hooks/pre-push
rm .git/hooks/pre-push
ln -s ../../pre-push-hook.sh .git/hooks/pre-push
# checks if locally staged changes pass tests.
echo "starting pre-push hook"
_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DIR=$( echo $_DIR | sed 's/\/.git\/hooks$//' )
echo "Running pre-commit hook ... (you can omit this with --no-verify, but don't)"
git diff --quiet
hadNoNonStagedChanges=$?
if ! [ $hadNoNonStagedChanges -eq 0 ]
then
echo "* Stashing non-staged changes"
git stash --keep-index -u > /dev/null
fi
[ ! -f $DIR/build.sbt ]
sbtProject=$?
if [ ! sbtProject -eq 0 ]
then
echo "* Not an SBT Project."
exit 0
fi
echo "* This is an SBT project."
echo "* Running Tests."
sbt ";clean;test" > /dev/null
passes=$?
echo "* Compiles?"
if [ $passes -eq 0 ]
then
echo "* Yes!"
else
echo "* No!"
fi
if ! [ $hadNoNonStagedChanges -eq 0 ]
then
echo "* Scheduling stash pop of previously stashed non-staged changes for 1 second after commit"
sleep 1 && git stash pop --index > /dev/null & # sleep and & otherwise commit fails when this leads to a merge conflict
fi
if [ $passes -eq 0 ]
then
echo "... done. Proceeding with commit."
exit 0
else
echo "CANCELLING commit due to COMPILE or TEST ERROR."
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment