Skip to content

Instantly share code, notes, and snippets.

@ssaavedra
Last active September 19, 2018 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaavedra/b496c645d13013a0aa4f6d42b1ba83d1 to your computer and use it in GitHub Desktop.
Save ssaavedra/b496c645d13013a0aa4f6d42b1ba83d1 to your computer and use it in GitHub Desktop.
Setup a repo pre-commit hook to use scalafmt
#!/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 --
#!/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