Skip to content

Instantly share code, notes, and snippets.

@doriancodes
Last active September 18, 2018 13:18
Show Gist options
  • Save doriancodes/c42da82b888869bf935242b1160e05b7 to your computer and use it in GitHub Desktop.
Save doriancodes/c42da82b888869bf935242b1160e05b7 to your computer and use it in GitHub Desktop.
scalafmt and the git pre-commit hook inspired by https://gist.github.com/cvogt/2676ed6c6d1abafa3d6a
#!/bin/sh
echo "$(tput setaf 3)* installing scalafmt library... $(tput sgr 0)"
brew install --HEAD olafurpg/scalafmt/scalafmt
touch ../.git/hooks/pre-commit #create the file if not exist
rm ../.git/hooks/pre-commit #delete the file
ln -s pre-commit-hook.sh ../.git/hooks/pre-commit #create a file link
echo "$(tput setaf 3)* link to ../.git/hooks/pre-commit successfully created.$(tput sgr 0)"
cp pre-commit-hook.sh ../.git/hooks/pre-commit-hook.sh
echo "$(tput setaf 3)* pre-commit-hook successfully installed.$(tput sgr 0)"
#!/bin/sh
# checks if locally staged changes are
# formatted properly. Ignores non-staged
# changes.
# Intended as git pre-commit hook
_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DIR=$( echo $_DIR | sed 's/\/.git\/hooks$//' )
#COLOR CODES:
#tput setaf 3 = yellow -> Info
#tput setaf 1 = red -> warning/not allowed commit
#tput setaf 2 = green -> all good!/allowed commit
echo ""
echo "$(tput setaf 3)Running pre-commit hook ... (you can omit this with --no-verify, but don't)$(tput sgr 0)"
git diff --quiet
hadNoNonStagedChanges=$?
if ! [ $hadNoNonStagedChanges -eq 0 ]
then
echo "$(tput setaf 3)* Stashing non-staged changes$(tput sgr 0)"
git stash --keep-index -u > /dev/null
fi
echo "$(tput setaf 3)* Compiling staged changes... $(tput sgr 0)"
(cd $DIR/; sbt test:compile)
compiles=$?
echo "$(tput setaf 3)* Compiles?$(tput sgr 0)"
if [ $compiles -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
fi
echo "$(tput setaf 3)* Formatting staged changes... $(tput sgr 0)"
(cd $DIR/; scalafmt)
git diff --quiet
formatted=$?
echo "$(tput setaf 3)* Properly formatted?$(tput sgr 0)"
if [ $formatted -eq 0 ]
then
echo "$(tput setaf 2)* Yes$(tput sgr 0)"
else
echo "$(tput setaf 1)* No$(tput sgr 0)"
echo "$(tput setaf 1)The following files need formatting (in stage or commited):$(tput sgr 0)"
git diff --name-only
echo ""
echo "$(tput setaf 1)Please run 'scalafmt' to format the code.$(tput sgr 0)"
echo ""
fi
echo "$(tput setaf 3)* Undoing formatting$(tput sgr 0)"
git stash --keep-index > /dev/null
git stash drop > /dev/null
if ! [ $hadNoNonStagedChanges -eq 0 ]
then
echo "$(tput setaf 3)* Scheduling stash pop of previously stashed non-staged changes for 1 second after commit.$(tput sgr 0)"
sleep 1 && git stash pop --index > /dev/null & # sleep and & otherwise commit fails when this leads to a merge conflict
fi
if [ $compiles -eq 0 ] && [ $formatted -eq 0 ]
then
echo "$(tput setaf 2)... done. Proceeding with commit.$(tput sgr 0)"
echo ""
exit 0
elif [ $compiles -eq 0 ]
then
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to NON-FORMATTED CODE.$(tput sgr 0)"
echo ""
exit 1
else
echo "$(tput setaf 1)... done.$(tput sgr 0)"
echo "$(tput setaf 1)CANCELLING commit due to COMPILE ERROR.$(tput sgr 0)"
echo ""
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment