Skip to content

Commit 6530e7f

Browse files
Add hook to require tests before git commit/push
Add a Bash hook script (.claude/hooks/require-tests-before-git.sh) that reads PreToolUse JSON from stdin, extracts tool_input.command using jq, and blocks git commit or git push operations by exiting with code 2 and printing instructions to run the test suite. Also register the hook in .claude/settings.json under a Bash matcher so the hook is invoked for relevant tool runs.
1 parent c17e338 commit 6530e7f

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
# Block git commit/push if tests haven't been run in this session.
3+
# Reads PreToolUse JSON from stdin, checks tool_input.command.
4+
5+
INPUT=$(cat)
6+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // ""')
7+
8+
# Match git commit or git push (with any flags/args after)
9+
if echo "$COMMAND" | grep -qE '(^|\&\&\s*)git (commit|push)'; then
10+
echo "BLOCKED: Run tests locally before committing or pushing." >&2
11+
echo " dotnet test project/dbatools.Tests/dbatools.Tests.csproj" >&2
12+
echo " pwsh -c \"& scripts/ralph-test-runner.ps1 -Path '<relevant test>'\"" >&2
13+
exit 2
14+
fi
15+
16+
exit 0

.claude/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
"command": "bash .claude/hooks/enforce-psd1-rules.sh"
2727
}
2828
]
29+
},
30+
{
31+
"matcher": "Bash",
32+
"hooks": [
33+
{
34+
"type": "command",
35+
"command": "bash .claude/hooks/require-tests-before-git.sh"
36+
}
37+
]
2938
}
3039
]
3140
}

0 commit comments

Comments
 (0)