Created
December 3, 2017 20:29
-
-
Save markusjura/ac7bdb011419ced3cced27f6a1bbc7d9 to your computer and use it in GitHub Desktop.
git pre-commit hook for Scala compile and scapegoat checking (put both in your git project root, needs to be installed in each clone)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
cd "$(dirname "$0")" | |
touch .git/hooks/pre-commit | |
rm .git/hooks/pre-commit | |
ln -s ../../pre-commit-hook.sh .git/hooks/pre-commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Pre-commit hook to verify what is about to be committed. | |
# Called by "git commit" with no arguments. The hook should | |
# exit with non-zero status after issuing an appropriate message if | |
# it wants to stop the commit. | |
# Set PATH to support pre-commit hok is supported by git GUI clients | |
PATH=$PATH:/usr/local/bin:/usr/local/sbin | |
echo "" | |
echo "Running pre-commit hook ... (you can omit this with --no-verify, but don't)" | |
echo "* Moving to the project directory…" | |
_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | |
DIR=$( echo $_DIR | sed 's/\/.git\/hooks$//' ) | |
echo "* Stashing non-staged changes so we don't check them"… | |
git diff --quiet | |
hadNoNonStagedChanges=$? | |
if ! [ $hadNoNonStagedChanges -eq 0 ] | |
then | |
git stash --keep-index -u > /dev/null | |
fi | |
echo "* Compiling.." | |
sbt test:compile > /dev/null | |
compiles=$? | |
if [ $compiles -ne 0 ] | |
then | |
echo " [ERROR] Compilation failed " | |
else | |
echo "* Checking scapegoat static analyzer" | |
sbt scapegoat > /dev/null | |
scapegoat=$? | |
if [ $scapegoat -ne 0 ] | |
then | |
echo " [ERROR] scapegoat found static analyzer errors" | |
fi | |
fi | |
echo "* Applying the stash with the non-staged changes…" | |
if ! [ $hadNoNonStagedChanges -eq 0 ] | |
then | |
sleep 1 && git stash pop --index > /dev/null & # sleep because otherwise commit fails when this leads to a merge conflict | |
fi | |
# Final result | |
echo "" | |
if [ $compiles -eq 0 ] && [ $scapegoat -eq 0 ] | |
then | |
echo "[OK] Your code will be committed young padawan" | |
exit 0 | |
elif [ $compiles -ne 0 ] | |
then | |
echo "[ERROR] Cancelling commit due to compile error (run 'sbt test:compile' for more information)" | |
exit 1 | |
elif [ $scapegoat -ne 0 ] | |
then | |
echo "[ERROR] Cancelling commit due to static analyzer error (run 'sbt scapegoat' for more information)" | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment