.claude/hooks/pre-commit-tests.sh · chapter Tests and team tooling, file 22 · 65 lines · open in the repository
What it does
PreToolUse hook (configured in .claude/settings.json): before Claude runs a Bash command that runsgit commit, run the test suite on exactly what will be committed
and block the commit if it fails. Exit code 2 tells Claude Code to block the tool call
and show stderr to Claude.
The tests run on a copy of what the commit will contain, built from a copy of the
index: the staged files for git commit, plus changes to tracked files for
git commit -a (untracked files are left out, as git leaves them out). A commit that
misses a file it needs fails here instead of breaking main.
The copy is taken before the command runs, so a command that stages files and then
commits (git add x && git commit) is refused: the tests would not see the git add.
Source
.claude/hooks/pre-commit-tests.sh