diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..629ffd7 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Pre-commit hook: format Haskell sources with fourmolu before each commit. +# Aborts the commit if formatting changes are needed. +# +# To install: make sure .githooks is CONFIGURED as the hooks directory +# in git config (or copy this to .git/hooks/pre-commit). +set -euo pipefail + +# Only run if fourmolu is available (skips on systems without the hs wrapper) +if command -v ./hs &>/dev/null && ./hs fourmolu --version &>/dev/null 2>&1; then + # Check which .hs files are staged for commit + STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.hs$' || true) + if [ -n "$STAGED_FILES" ]; then + # shellcheck disable=SC2086 + if ! ./hs fourmolu -m check $STAGED_FILES >/dev/null 2>&1; then + echo "❌ Formatting check failed. Run the following to fix:" + echo " ./hs fourmolu -i $STAGED_FILES" + echo " git add $STAGED_FILES" + exit 1 + fi + fi +fi