> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# .claude/hooks/pre-commit-tests.sh

> PreToolUse hook (configured in .claude/settings.json): before Claude runs a Bash command that runs `git 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.

`.claude/hooks/pre-commit-tests.sh` · chapter [Tests and team tooling](https://calendar-effects.mintlify.site/tour/10-tests-and-tooling.md), file 22 · 65 lines · [open in the repository](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/.claude/hooks/pre-commit-tests.sh)

## What it does

PreToolUse hook (configured in .claude/settings.json): before Claude runs a Bash
command that runs `git 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

```bash title=".claude/hooks/pre-commit-tests.sh" lines expandable theme={null}
#!/bin/bash
# PreToolUse hook (configured in .claude/settings.json): before Claude runs a Bash
# command that runs `git 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`.

repo="${CLAUDE_PROJECT_DIR:-$(cd "$(dirname "$0")/../.." && pwd)}"
cd "$repo" || exit 0

python=python3
# On Windows, `python3` may be a Microsoft Store shortcut and venvs only have `python`.
python3 -c '' 2>/dev/null || python=python
[ -x "$repo/.venv/bin/python" ] && python="$repo/.venv/bin/python"
[ -x "$repo/.venv/Scripts/python.exe" ] && python="$repo/.venv/Scripts/python.exe"   # Windows venv layout

input=$(cat)
command=$(printf '%s' "$input" | "$python" -c '
import json, sys
data = json.load(sys.stdin)
print(data.get("tool_input", data).get("command", ""))' 2>/dev/null) || command=$input

starts_with() {  # does any part of the command (after ; & | or a newline) start with "git <$1>"?
  printf '%s\n' "$command" | grep -Eq "(^|[;&|(])[[:space:]]*git[[:space:]]+($1)([[:space:]]|$)"
}

starts_with commit || exit 0

if starts_with 'add|rm|mv|stage|restore|reset|apply'; then
  echo "Commit blocked: run git add (or rm, mv, restore) and git commit as separate commands." >&2
  echo "The tests run on what is staged when the command starts, so they would miss the git add." >&2
  exit 2
fi

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
cp "$(git rev-parse --git-path index)" "$tmp/index"

if printf '%s\n' "$command" | grep -Eq 'git[[:space:]]+commit[^;&|]*[[:space:]](-[A-Za-z]*a[A-Za-z]*|--all)([[:space:]]|$)'; then
  where="the files git commit -a will commit"
  GIT_INDEX_FILE="$tmp/index" git add -u
else
  where="the staged files"
fi
GIT_INDEX_FILE="$tmp/index" git checkout-index -a --prefix="$tmp/tree/"
output=$(cd "$tmp/tree" && "$python" -m pytest -q -p no:cacheprovider 2>&1)
status=$?

if [ $status -ne 0 ]; then
  {
    echo "Commit blocked: python3 -m pytest -q failed on $where (AGENTS.md rule 11)."
    echo "Fix the cause, or stage the files the change needs; do not weaken or skip tests."
    echo "If pytest is missing: pip install -r requirements.txt -r requirements-dev.txt"
    echo "$output" | tail -n 40
  } >&2
  exit 2
fi
exit 0
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.