-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-msg
More file actions
executable file
·102 lines (84 loc) · 4.08 KB
/
Copy pathcommit-msg
File metadata and controls
executable file
·102 lines (84 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
#
# Global commit-msg hook — calls Claude Code to review every commit.
# Install via: ~/ai-commit-hook/install.sh
# Bypass with: git commit --no-verify
#
set -euo pipefail
MODEL="__MODEL__"
COMMIT_MSG_FILE="$1"
# ── Gather context ──────────────────────────────────────────────────
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"
REPO_NAME="$(basename "$REPO_ROOT")"
BRANCH="$(git branch --show-current 2>/dev/null || echo "detached")"
COMMIT_MSG="$(git stripspace --strip-comments < "$COMMIT_MSG_FILE")"
FILE_LIST="$(git diff --cached --name-status)"
DIFF="$(git diff --cached)"
# Nothing staged? Let git handle the error.
if [ -z "$DIFF" ]; then
exit 0
fi
# ── Guard against huge diffs ────────────────────────────────────────
MAX_LINES=20000
DIFF_LINES="$(echo "$DIFF" | wc -l | tr -d ' ')"
TRUNCATED=""
if [ "$DIFF_LINES" -gt "$MAX_LINES" ]; then
DIFF="$(echo "$DIFF" | head -n "$MAX_LINES")"
TRUNCATED="[WARNING: Diff truncated from $DIFF_LINES to $MAX_LINES lines]"
fi
# ── Build the prompt ────────────────────────────────────────────────
PROMPT="You are a senior code reviewer. Analyze this Git commit and provide a SHORT report (max 10 lines).
Repository: ${REPO_NAME}
Branch: ${BRANCH}
== Commit Message ==
${COMMIT_MSG}
== Files Changed ==
${FILE_LIST}
== Diff ==
${DIFF}
${TRUNCATED}
Review checklist:
- Does the commit message accurately describe the changes?
- Any obvious bugs, logic errors, or typos in the code?
- Any security concerns (secrets, injection, unsafe operations)?
- Any leftover debug code (console.log, print, TODO)?
Format:
SUMMARY: One-line overall assessment
ISSUES: Bulleted list of concerns (or \"None found\")
SUGGESTIONS: Bulleted list of optional improvements (or \"Looks good\")"
# ── Call Claude Code ────────────────────────────────────────────────
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo " Claude Code Review · ${REPO_NAME} @ ${BRANCH}"
echo "═══════════════════════════════════════════════════════════════"
echo ""
REVIEW="$(claude -p "$PROMPT" --model "$MODEL" --output-format text --max-turns 1 2>&1)" || {
echo "⚠ Claude review failed (exit $?). Proceeding with commit."
echo "$REVIEW"
# Don't block commits if Claude is unavailable
exit 0
}
echo "$REVIEW"
echo ""
echo "═══════════════════════════════════════════════════════════════"
# ── Prompt user ─────────────────────────────────────────────────────
printf "\nProceed with commit? [Y/n] "
read -r REPLY </dev/tty || REPLY=""
case "$REPLY" in
[nNaA]*)
echo ""
echo "Commit aborted. Your commit message was:"
echo "───────────────────────────────────────────"
echo "$COMMIT_MSG"
echo "───────────────────────────────────────────"
exit 1
;;
*)
echo "Continuing with commit."
;;
esac
# ── Chain to local repo hook if it exists ───────────────────────────
LOCAL_HOOK="${REPO_ROOT}/.git/hooks/commit-msg"
if [ -x "$LOCAL_HOOK" ]; then
exec "$LOCAL_HOOK" "$@"
fi