Last active
September 19, 2018 13:29
-
-
Save ssaavedra/b496c645d13013a0aa4f6d42b1ba83d1 to your computer and use it in GitHub Desktop.
Setup a repo pre-commit hook to use scalafmt
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 | |
# Licensed under CC0 in jurisdictions where this is not directly in the Public Domain | |
# Authored by Santiago Saavedra (github.com/ssaavedra) | |
git scalafmt --test || exit 1 | |
# If there are whitespace errors, print the offending file names and fail. | |
exec git diff-index --check --cached $against -- |
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/bash | |
# Licensed under CC0 in jurisdictions where this is not directly in the Public Domain | |
# Authored by Santiago Saavedra (github.com/ssaavedra) | |
scalafmt="$(git rev-parse --git-path bin/scalafmt)" | |
download-scalafmt () { | |
if [[ -x "$scalafmt" ]]; then | |
echo "scalafmt is already downloaded" | |
return 0 | |
fi | |
mkdir -p "$(git rev-parse --git-path bin)" | |
if which scalafmt >/dev/null 2>&1; then | |
echo "There is a scalafmt on the PATH. We will use that, but you can customize it at:" | |
echo "$scalafmt" | |
ln -s "$(which scalafmt)" "$scalafmt" | |
return 0 | |
fi | |
if [[ $(uname -s) == "Linux" && $(uname -i) == "x86_64" ]]; then | |
echo "Downloading binary-image of scalafmt..." | |
curl -L -o "$scalafmt" https://github.com/ssaavedra/scalafmt/releases/download/v1.6.0-RC4-graalvm1/scalafmt-1.6.0-rc4-graalvm1-linux-x86_64-stripped-static | |
else | |
echo "Could not find a binary scalafmt available for your platform. Falling back to using sbt scalafmtCheck" | |
echo "#!/bin/bash\nexec sbt scalafmtCheck\n" > "$scalafmt" | |
fi | |
chmod +x "$scalafmt" | |
} | |
setup-hooks () { | |
ln -fsv "$PWD/_git-hooks/pre-commit" "$(git rev-parse --git-path hooks/pre-commit)" | |
} | |
setup-config-alias () { | |
git config --replace-all alias.scalafmt '! $(git rev-parse --git-path bin/scalafmt) -Xmn1g --git 1' | |
} | |
download-scalafmt | |
setup-hooks | |
setup-config-alias |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment