diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..fbcbbb3 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Reject commits containing Go files that aren't gofmt-formatted. +set -euo pipefail + +files="$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true)" + +if [ -z "$files" ]; then + exit 0 +fi + +unformatted="$(gofmt -l $files)" + +if [ -n "$unformatted" ]; then + echo "gofmt: the following files are not formatted:" + echo "$unformatted" + echo + echo "Run 'gofmt -w ...' and re-stage before committing." + exit 1 +fi diff --git a/README.md b/README.md index 685d3f4..fd4cebc 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ The intention for this project is to work as a template, where the developer sho go run ./cmd ``` +6. Enable the repo's git hooks (rejects commits with unformatted Go files): + + ```bash + git config core.hooksPath .githooks + ``` + ## Migrations `goose.sh` loads `.env` and passes through to goose (`up`, `down`, `status`, ...).