Skip to content

Instantly share code, notes, and snippets.

@Unayung
Created July 23, 2025 05:58
Show Gist options
  • Save Unayung/bc329f4d0f9d8175a8a44d950a7816a7 to your computer and use it in GitHub Desktop.
Save Unayung/bc329f4d0f9d8175a8a44d950a7816a7 to your computer and use it in GitHub Desktop.
gemini-code-review.sh
#!/bin/bash
# Gemini Code Review Hook for Claude Code
# This script calls Gemini CLI to review code changes made by Claude Code
# Read JSON input from Claude Code
json_input=$(cat)
# Extract tool information
tool_name=$(echo "$json_input" | jq -r '.tool_name // empty')
tool_input=$(echo "$json_input" | jq -r '.tool_input // {}')
# Only proceed if this is a code editing tool
if [[ "$tool_name" != "Edit" && "$tool_name" != "MultiEdit" && "$tool_name" != "Write" ]]; then
exit 0
fi
# Extract file path and content changes
file_path=$(echo "$tool_input" | jq -r '.file_path // empty')
new_string=$(echo "$tool_input" | jq -r '.new_string // empty')
content=$(echo "$tool_input" | jq -r '.content // empty')
# Skip if no meaningful code changes
if [[ -z "$file_path" || (-z "$new_string" && -z "$content") ]]; then
exit 0
fi
# Skip non-code files
case "$file_path" in
*.md|*.txt|*.json|*.yml|*.yaml|*.xml) exit 0 ;;
esac
# Check if gemini CLI is available
if ! command -v gemini &> /dev/null; then
echo "⚠️ Gemini CLI not found. Please install it first."
echo " Install: npm install -g @google-ai/generativelanguage"
exit 0
fi
echo "πŸ€– Gemini reviewing code changes in $(basename "$file_path")..."
# Prepare the review prompt
review_prompt="Please review this code change and provide feedback on:
1. Code quality and best practices
2. Potential bugs or issues
3. Security considerations
4. Performance implications
5. Suggestions for improvement
File: $file_path
Changes: ${new_string:-$content}"
# Create temporary file for the review
temp_file=$(mktemp)
echo "$review_prompt" > "$temp_file"
# Create review output file with timestamp
review_file="gemini-reviews/review-$(date +%Y%m%d-%H%M%S).md"
mkdir -p "$(dirname "$review_file")"
# Call Gemini CLI in non-interactive mode
echo "πŸ“ Gemini Code Review:"
echo "----------------------------------------"
# Create review header for file
{
echo "# Gemini Code Review - $(date)"
echo ""
echo "**File:** $file_path"
echo ""
echo "## Review"
echo ""
} > "$review_file"
# Get the review from Gemini and capture it
if review_output=$(gemini -p "$(cat "$temp_file")" 2>/dev/null); then
# Display the review in Claude (this won't show in conversation but will be in hook logs)
echo "$review_output"
echo ""
echo "---"
echo "πŸ’Ύ Review saved to: $review_file"
# Save the review to file
echo "$review_output" >> "$review_file"
echo "" >> "$review_file"
echo "---" >> "$review_file"
echo "Review saved to: $review_file" >> "$review_file"
# Create/update a latest review file for easy access
echo "$review_output" > "gemini-reviews/latest-review.md"
echo "πŸ“ Latest review available at: gemini-reviews/latest-review.md"
else
error_msg="❌ Failed to get Gemini review. Please check your Gemini CLI setup.
Make sure Gemini CLI is properly authenticated."
echo "$error_msg"
echo "$error_msg" >> "$review_file"
echo "$error_msg" > "gemini-reviews/latest-review.md"
fi
echo "----------------------------------------"
# Clean up temp file
rm "$temp_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment