In my daily workflow, I strive to maintain high code quality before any of my changes reach a human reviewer. To achieve this, I developed a bash script that leverages the Claude Code CLI to act as a pre-review gate.
This tool helps me catch "obvious" mistakes—like missing error handling or potential race conditions—before I even create a Merge Request (MR).
The Concept
The script automates the tedious parts of self-review. It generates a diff against the target branch, sends it to an AI with a specialized senior-engineer prompt, and generates a structured report.
It’s not meant to replace human review, but to ensure that the version my teammates see is already polished and free of common pitfalls.
The Script: code-review.sh
This script handles the comparison logic, ensures the environment is ready, and formats the AI's response into a readable Markdown file.
#!/bin/bash
# Code Review Script using Claude Code CLI
# Usage: ./code-review.sh [target-branch]
set -e
# Configuration
TARGET_BRANCH="${1:-main}"
OUTPUT_FILE="code-review-$(date +%Y%m%d-%H%M%S).md"
echo "🔍 Starting AI Code Review..."
# 1. Fetch latest changes
git fetch origin
# 2. Get the diff against origin/main (or specified branch)
DIFF=$(git diff origin/${TARGET_BRANCH}...HEAD)
if [ -z "$DIFF" ]; then
echo "No changes detected."
exit 0
fi
# 3. Create the review prompt with specific criteria
cat >/tmp/review_prompt.txt <<'EOF'
You are a Senior Software Engineer conducting a thorough code review.
Please review this code with the following criteria:
## 🔍 Critical Issues
1. Bugs & Logic Errors: Race conditions, logic flaws, etc.
2. Security: Vulnerabilities, exposed secrets.
3. Resource Leaks: Goroutine leaks, unclosed connections.
## 🎯 Best Practices (Golang)
- Idiomatic Go and performance optimizations.
- Missing tests for critical paths.
## 📋 Review Format
Provide Severity (🔴 Critical, 🟠 Major, 🟡 Minor), Location, Issue, and Solution.
EOF
# 4. Append diff and call Claude
echo "$DIFF" >>/tmp/review_prompt.txt
claude code review </tmp/review_prompt.txt >"$OUTPUT_FILE" 2>&1
# 5. Summary and Alert
if grep -q "🔴 Critical" "$OUTPUT_FILE"; then
echo "⚠️ CRITICAL ISSUES FOUND - Check $OUTPUT_FILE"
exit 1
else
echo "✅ Review completed. Saved to $OUTPUT_FILE"
fi
How It Works
-
Contextual Diff: It uses
git diff origin/main...HEADto get only the changes made in the current feature branch, excluding unrelated commits. -
Persona-Based Prompting: By defining a "Senior Software Engineer" persona, the script forces the AI to look for architectural flaws rather than just syntax.
-
Severity Tagging: The script looks for the
🔴 Criticalstring in the output. If found, it exits with an error code, serving as a signal that the code is not ready to be pushed.
Conclusions
Using this script has significantly reduced the "back-and-forth" during formal code reviews. Instead of reviewers pointing out forgotten nil checks or unoptimized loops, they can focus on higher-level discussions like system design and business logic.
It turns the self-review process from a manual, error-prone task into a consistent, automated habit.
By keeping the script simple and local, it doesn't interfere with the company's CI/CD but provides immense value for the individual developer.