Last active
August 13, 2025 10:23
-
-
Save monarchmaisuriya/2b0f59f07004fb89dd4d2bb7a7477fff to your computer and use it in GitHub Desktop.
Gemini Based Code Review Hook ( Bash, Zsh)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ============================================== | |
| # === Start of Gemini Diff-Based Code Review === | |
| # ============================================== | |
| # Detailed review prompt (multi-line, neatly formatted) | |
| __gemini_prompt_text=$( | |
| cat <<'EOF' | |
| Thoroughly review the changes, identifying potential issues including edge cases, syntax errors, | |
| logical flaws, unintended side effects, performance bottlenecks, and coding standards violations. | |
| Pay special attention to complex logic, input validation, and modifications affecting stability/scalability. | |
| Provide a clear, actionable, detailed report without modifying the code. | |
| Evaluate changes against: | |
| - SOLID Principles: | |
| * Single Responsibility: One reason to change per class/module. | |
| * Open/Closed: Open for extension, closed for modification. | |
| * Liskov Substitution: Subtypes must be substitutable without breaking correctness. | |
| * Interface Segregation: Prefer minimal, specific interfaces. | |
| * Dependency Inversion: Depend on abstractions, not concrete types. | |
| - KISS: Keep the design as simple as possible. | |
| - YAGNI: Avoid features/abstractions without proven need. | |
| - DRY: Eliminate duplication without premature abstractions. | |
| - Separation of Concerns: Clear boundaries, high cohesion, low coupling. | |
| - Law of Demeter: Limit method chaining; call only immediate collaborators. | |
| - Composition over Inheritance. | |
| - Principle of Least Astonishment. | |
| Also check: | |
| - Input validation & error handling (clear errors, fail-fast where needed). | |
| - Performance & scalability (complexity, memory, concurrency). | |
| - Security (injection, secrets, data exposure). | |
| - Testing (coverage, structure, assertions). | |
| - Observability (logging, metrics, tracing without leaking sensitive data). | |
| - Language/framework coding standards compliance. | |
| - Backward compatibility & migration strategy. | |
| - Feature flags & rollback safety. | |
| Deliverables: | |
| - Findings by severity (Critical, High, Medium, Low) with code references. | |
| - Specific remediation steps (no code edits). | |
| - Risk assessment for stability & scalability, covering edge cases/failures. | |
| - Summary of adherence to principles with examples. | |
| EOF | |
| ) | |
| # Track last git action and its exit code | |
| LAST_GIT_CMD="" | |
| LAST_GIT_EXIT_CODE="" | |
| # Wrapper for git to detect commits | |
| git() { | |
| if [[ "$1" == "commit" ]]; then | |
| LAST_GIT_CMD="commit" | |
| else | |
| LAST_GIT_CMD="" | |
| fi | |
| command git "$@" | |
| LAST_GIT_EXIT_CODE=$? | |
| return $LAST_GIT_EXIT_CODE | |
| } | |
| # Hook runs before prompt display | |
| precmd() { | |
| if [[ "${LAST_GIT_CMD:-}" == "commit" ]] && [[ "${LAST_GIT_EXIT_CODE:-1}" -eq 0 ]]; then | |
| LAST_GIT_CMD="" | |
| LAST_GIT_EXIT_CODE="" | |
| echo | |
| read -r "REPLY?Do you want a code review (performed by Gemini CLI) on the committed changes? [y/n] " | |
| if [[ "$REPLY" =~ ^[Yy]$ ]]; then | |
| echo "Running Gemini CLI code review..." | |
| # Get changed files (handles initial commit too) | |
| if git rev-parse --verify HEAD~1 >/dev/null 2>&1; then | |
| files=$(git diff --name-only HEAD~1 HEAD) | |
| else | |
| files=$(git show --name-only --pretty="" HEAD) | |
| fi | |
| if [[ -z "${files//[[:space:]]/}" ]]; then | |
| echo "No files changed in the last commit." | |
| return | |
| fi | |
| for file in $files; do | |
| if [[ -f "$file" ]]; then | |
| echo "Reviewing changes in: $file" | |
| git diff HEAD~1 HEAD -- "$file" | \ | |
| gemini -p "$__gemini_prompt_text" | |
| fi | |
| done | |
| fi | |
| fi | |
| } | |
| # ============================================ | |
| # === End of Gemini Diff-Based Code Review === | |
| # ============================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment