Skip to content

Commit 4bd34d3

Browse files
antonio2368claude
andcommitted
Add Claude Code pre-commit hook to enforce cargo fmt and clippy
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b6bb535 commit 4bd34d3

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

.claude/hooks/pre-commit.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
# Block git commit if cargo fmt or clippy fail
3+
4+
INPUT=$(cat)
5+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')
6+
7+
# Only check git commit commands
8+
if ! echo "$COMMAND" | rg -q 'git commit'; then
9+
exit 0
10+
fi
11+
12+
ERRORS=""
13+
14+
if ! cargo fmt --check 2>&1; then
15+
ERRORS="cargo fmt --check failed. Run 'cargo fmt' first."
16+
fi
17+
18+
if ! cargo clippy -- -D warnings 2>&1; then
19+
ERRORS="${ERRORS:+$ERRORS\n}cargo clippy failed. Fix lint warnings first."
20+
fi
21+
22+
if [ -n "$ERRORS" ]; then
23+
jq -n --arg reason "$ERRORS" '{
24+
"hookSpecificOutput": {
25+
"hookEventName": "PreToolUse",
26+
"permissionDecision": "deny",
27+
"permissionDecisionReason": $reason
28+
}
29+
}'
30+
exit 0
31+
fi
32+
33+
exit 0

.claude/settings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"hooks": {
3+
"PreToolUse": [
4+
{
5+
"matcher": "Bash",
6+
"hooks": [
7+
{
8+
"type": "command",
9+
"command": ".claude/hooks/pre-commit.sh"
10+
}
11+
]
12+
}
13+
]
14+
}
15+
}

0 commit comments

Comments
 (0)