#!/bin/sh # Pre-commit hook: runs go vet, go fmt check, and go test on staged Go files. # Install: cp scripts/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit set -e # Check for Go files in staging STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true) if [ -z "$STAGED_GO_FILES" ]; then exit 0 fi echo "Running pre-commit checks on Go files..." # Check formatting UNFORMATTED=$(gofmt -l $STAGED_GO_FILES 2>/dev/null || true) if [ -n "$UNFORMATTED" ]; then echo "ERROR: The following files are not formatted with gofmt:" echo "$UNFORMATTED" echo "Run 'go fmt ./...' to fix." exit 1 fi # Run go vet echo "Running go vet..." go vet ./... echo "Pre-commit checks passed."