-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-coderabbit-hook.sh
More file actions
executable file
·70 lines (57 loc) · 1.82 KB
/
install-coderabbit-hook.sh
File metadata and controls
executable file
·70 lines (57 loc) · 1.82 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
#!/bin/bash
#
# Install CodeRabbit pre-commit hook
# Usage: ./install-coderabbit-hook.sh
#
HOOK_PATH=".git/hooks/pre-commit"
# Check if we're in a git repository
if [ ! -d ".git" ]; then
echo "❌ Error: Not in a git repository root directory"
exit 1
fi
# Create the pre-commit hook
cat > "$HOOK_PATH" << 'EOF'
#!/bin/sh
#
# CodeRabbit AI Code Review - Pre-commit Hook
# This hook runs CodeRabbit CLI on staged changes before allowing a commit.
#
# Create reviews directory if it doesn't exist
REVIEW_DIR=".coderabbit-reviews"
mkdir -p "$REVIEW_DIR"
# Generate timestamp for the review file
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
REVIEW_FILE="$REVIEW_DIR/review_$TIMESTAMP.md"
echo "Running CodeRabbit AI code review on staged changes..."
echo "📄 Review will be saved to: $REVIEW_FILE"
echo ""
# Run CodeRabbit review and save output to file
# Using --plain for detailed feedback with fix suggestions
coderabbit review --plain 2>&1 | tee "$REVIEW_FILE"
# Capture the exit code from coderabbit (not tee)
CODERABBIT_EXIT_CODE=${PIPESTATUS[0]}
# If CodeRabbit finds issues, you can choose to:
# 1. Block the commit (exit 1)
# 2. Allow the commit but show warnings (exit 0)
if [ $CODERABBIT_EXIT_CODE -ne 0 ]; then
echo ""
echo "⚠️ CodeRabbit found issues with your code."
echo "Review the suggestions above and consider fixing them."
echo "📄 Full review saved to: $REVIEW_FILE"
echo ""
echo "To commit anyway, use: git commit --no-verify"
exit 1
fi
echo ""
echo "✅ CodeRabbit review passed!"
echo "📄 Review saved to: $REVIEW_FILE"
exit 0
EOF
# Make it executable
chmod +x "$HOOK_PATH"
echo "✅ CodeRabbit pre-commit hook installed successfully!"
echo "📍 Location: $HOOK_PATH"
echo ""
echo "To use in other repos:"
echo " 1. Copy this script to the repo root"
echo " 2. Run: ./install-coderabbit-hook.sh"