diff --git a/hooks/pre-commit b/hooks/pre-commit index 7b319e187..8d29bef9d 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -12,3 +12,16 @@ if git diff --cached --name-only | grep -q '\.go$'; then echo "🔍 Checking for stale import paths..." python3 tests/contract/check-import-paths.py fi + +# Check gofmt on staged Go files (--diff-filter=d: gofmt cannot read +# deleted files; xargs -I{} keeps one file per line, spaces-safe) +if git diff --cached --name-only | grep -q '\.go$'; then + unformatted=$(git diff --cached --name-only --diff-filter=d -- '*.go' | xargs -I{} gofmt -l -- {}) + if [ -n "$unformatted" ]; then + echo "❌ gofmt found unformatted files:" + echo "$unformatted" + echo "" + echo "Run: gofmt -w " + exit 1 + fi +fi