#! /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, install it in the local git repository. # # `curl -sL https://github.com/crystal-lang/crystal/raw/master/scripts/git/pre-commit > .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit`. # # Alternatively, in the Crystal repo you can directly link it: `ln -s scripts/git/pre-commit .git/hooks`. # # 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 # shellcheck disable=SC2086 exec bin/crystal tool format --check $changed_cr_files >&2 else # shellcheck disable=SC2086 exec crystal tool format --check $changed_cr_files >&2 fi