Skip to content

Commit 224eab1

Browse files
AlexsJonesclaude
andcommitted
chore: catch codegen drift in pre-commit hook
Extends .githooks/pre-commit to run 'make generate' and fail the commit if CRDs/deepcopy are out of sync, but only when api/ type files are staged (so normal commits stay fast). Also documents required status checks for main branch protection, so the Codegen & Helm sync job blocks PR merges instead of failing silently post-merge. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 7852c46 commit 224eab1

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

.githooks/pre-commit

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
2-
# Pre-commit hook: ensure all Go files are gofmt-formatted before committing.
2+
# Pre-commit hook:
3+
# 1. ensure all Go files are gofmt-formatted
4+
# 2. if api/ types changed, ensure generated CRDs/deepcopy are in sync
35

46
set -euo pipefail
57

@@ -12,3 +14,20 @@ if [ -n "$unformatted" ]; then
1214
echo "Run 'make fmt' or 'gofmt -s -w .' to fix, then re-add and commit."
1315
exit 1
1416
fi
17+
18+
# Only run codegen check if API types are part of this commit.
19+
if git diff --cached --name-only | grep -qE '^api/.*\.go$'; then
20+
echo "api/ types changed — verifying generated code is in sync..."
21+
if ! make generate >/dev/null 2>&1; then
22+
echo "ERROR: 'make generate' failed. Fix the errors, then re-commit."
23+
exit 1
24+
fi
25+
drift=$(git diff --name-only -- config/crd/bases charts/sympozium/crds api/ 2>/dev/null || true)
26+
if [ -n "$drift" ]; then
27+
echo "ERROR: Generated code is out of sync with api/ types. Drifted files:"
28+
echo "$drift" | sed 's/^/ /'
29+
echo ""
30+
echo "Run 'make generate', then 'git add' the updated files and re-commit."
31+
exit 1
32+
fi
33+
fi

CONTRIBUTING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,31 @@ make build # compile all binaries
108108
go build ./... # quick compile check
109109
```
110110

111+
### Pre-commit hook
112+
113+
The repo ships a pre-commit hook at `.githooks/pre-commit` that:
114+
115+
1. verifies all Go files are `gofmt`-formatted, and
116+
2. if any `api/` type files are staged, runs `make generate` and fails the commit if CRDs/deepcopy are out of sync.
117+
118+
Enable it once per clone:
119+
120+
```bash
121+
make setup-hooks
122+
```
123+
124+
This catches codegen drift locally — without it, the drift only surfaces in the `Codegen & Helm sync` CI job after push.
125+
126+
### Required status checks
127+
128+
The following CI jobs should be configured as **required status checks** on the `main` branch in GitHub branch protection, so PRs can't merge while red:
129+
130+
- `Vet`
131+
- `Test`
132+
- `Build`
133+
- `Codegen & Helm sync`
134+
- `Format`
135+
111136
---
112137

113138
## Multi-Architecture Builds

0 commit comments

Comments
 (0)