Skip to content

Instantly share code, notes, and snippets.

@straight-shoota
Last active April 4, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save straight-shoota/fdaf4cf1954e084cd5abccf8f77975f6 to your computer and use it in GitHub Desktop.
Save straight-shoota/fdaf4cf1954e084cd5abccf8f77975f6 to your computer and use it in GitHub Desktop.
Git pre-commit hook for `crystal tool format`
#!/bin/sh
#
# This script ensures Crystal code is correctly formatted before committing it.
# It won't apply any format changes automatically.
#
# Only staged files (the ones to be committed) are being processed, but each file is checked
# entirely as it is stored on disc, even parts that are not staged.
#
# To use this script, it needs to be installed in the local git repository. For example by running
# curl https://gist.githubusercontent.com/straight-shoota/fdaf4cf1954e084cd5abccf8f77975f6/raw/pre-commit > .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
#
# 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.
changed_cr_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cr$')
[ -z "$changed_cr_files" ] && exit 0
if [ -x bin/crystal ]; then
# use bin/crystal wrapper when available to run local compiler build
exec bin/crystal tool format --check $changed_cr_files >&2
else
exec crystal tool format --check $changed_cr_files >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment