DEV Community

Cover image for Building an AI-Powered Git Diff Reviewer: Catch Bugs Before You Push
Humam Moin
Humam Moin

Posted on

Building an AI-Powered Git Diff Reviewer: Catch Bugs Before You Push

Hey dev.to! ๐Ÿ‘‹

Have you ever pushed a commit in a hurry, only to realize later that you introduced a critical security flaw or a silly bug? We've all been there. The "oops, I'll fix it in the next commit" cycle is a rite of passage for developers.

What if you had a senior developer looking over your shoulder before you even push?

That's exactly why I built Git Diff Reviewer, a CLI tool that integrates directly into your Git workflow. When you commit, it automatically extracts your diff, sends it to an LLM (Gemini, OpenRouter, Groq, or local Ollama), and gives you a structured review right in your terminal.

Let's dive into how it works and how you can set it up!


๐Ÿ› ๏ธ What is Git Diff Reviewer?

Git Diff Reviewer is a pre-commit hook that acts as your personal AI code reviewer. Instead of waiting for CI/CD to fail or a teammate to catch a bug, you get instant feedback.

Key features

  • Multi-Provider Support: Gemini for deep capability, Groq for ultra-low latency, OpenRouter for flexibility, or Ollama for 100% local privacy.
  • Strict Review Taxonomy: It doesn't just say "this looks bad." It categorizes issues (see the table below).
  • Pre-commit Hook: Runs automatically on git commit. Zero friction.
  • .reviewerignore: Filters out lockfiles, build outputs, and .env files so you don't waste tokens.
  • Fully Tested: Comes with 22 unit tests to ensure reliability.
Category What it catches
Critical Bugs Logic errors, infinite loops
๐Ÿ”’ Security Vulnerabilities SQL injection, XSS, hardcoded secrets (aligned with OWASP/CWE)
โš ๏ธ Anti-patterns Code smells and bad practices
โšก Performance Bottlenecks N+1 queries, inefficient loops

How It Works (Architecture)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   git commit -m "feat: update auth"      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Pre-commit hook                        โ”‚
โ”‚   python main.py --staged                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Extract staged diff                    โ”‚
โ”‚   + filter files via .reviewerignore     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   LLM Provider                           โ”‚
โ”‚   Gemini โ”‚ Groq โ”‚ OpenRouter โ”‚ Ollama    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Structured review printed in terminal  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Setup & Installation

Let's get this running on your machine.

1. Clone the repository

git clone https://github.com/humammoin09-blip/git-diff-reviewer.git
cd git-diff-reviewer
Enter fullscreen mode Exit fullscreen mode

2. Create and activate a virtual environment

python -m venv .venv

# Windows:
.\.venv\Scripts\Activate.ps1

# Mac/Linux:
source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

3. Install dependencies

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

4. Set up environment variables

Copy the example file and add your API key:

cp .env.example .env
Enter fullscreen mode Exit fullscreen mode

Open .env and configure your provider (e.g., Gemini):

LLM_PROVIDER=gemini
GEMINI_API_KEY=your_api_key_here
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pro tip: If you want 100% privacy, set LLM_PROVIDER=ollama and run a local model!

5. Install the Git hook

python main.py --install-hook
Enter fullscreen mode Exit fullscreen mode

That's it! Now, every time you commit, the AI will review your code.


๐Ÿ’ป Real-World Example

Imagine you write this code:

# auth.py
def get_user(user_id):
    query = f"SELECT * FROM users WHERE id = {user_id}"
    return db.execute(query)
Enter fullscreen mode Exit fullscreen mode

When you commit, the AI immediately flags it:

=== AI CODE REVIEW ===

[CRITICAL] SQL Injection vulnerability detected
File: auth.py, Line 42
Issue: User input directly interpolated into SQL query.
Fix: Use parameterized queries.
CWE-89: Improper Neutralization of Special Elements used in an SQL Command

[WARNING] Hardcoded secret detected
File: config.py, Line 15
Issue: API_KEY = "sk-abc123..."
Fix: Move to environment variable.
Enter fullscreen mode Exit fullscreen mode

This catches the bug before it ever reaches production. ๐Ÿ›‘


โš ๏ธ Honest Limitations

Real security engineers respect honesty over hype, so here's what the tool cannot do:

  • LLM hallucinations: AI can occasionally give false positives. Treat it as a "second pair of eyes," not an absolute truth.
  • Context window limits: Extremely large diffs might exceed the model's context window.
  • Offline limitation: Unless you are using a local Ollama model, you need an internet connection.

๐Ÿ”ฎ What's Next? (Roadmap)

I'm just getting started with this. For v2, I'm planning:

  • [ ] GitHub Action integration (automatic PR reviews)
  • [ ] Multi-file context understanding
  • [ ] Custom rule engine (define your own linting rules)

๐Ÿค Your Turn, dev.to!

If you find this project useful, please give it a Star on GitHub! โญ

GitHub logo humammoin09-blip / git-diff-reviewer

An automated terminal tool that runs a Git diff before pushing code, uses an LLM to review or roast your code changes, and catches bad practices.

Git Diff Reviewer

A modular, terminal-based CLI tool that inspects local git diffs and sends them to your choice of LLM provider for an automated, high-rigor security and code quality review.


๐ŸŒŸ Key Features

  • Multi-Provider Support: Seamlessly switch between OpenRouter, Google Gemini, Groq, and local Ollama models.
  • Flexible Diff Targets: Review staged changes (--staged), unstaged changes (--unstaged), or diff against specific branches/commits (--branch main).
  • Interactive Mode: Guided terminal prompts for picking diff targets and providers on the fly.
  • Strict Review Taxonomy: Highlights Critical Bugs, Security Vulnerabilities (OWASP/CWE/Secret leaks), Anti-patterns, and Performance Bottlenecks with actionable Before/After code snippets.
  • Rich Terminal Rendering: Formatted markdown and colorized panels right in your terminal.
  • Safe Truncation: Automatically manages large diffs to avoid LLM token context overflow.
  • Export to Markdown: Save reviews directly to .md files for pull request commentsโ€ฆ




Break it, fuzz it, and if you find a bug, open an issue. I read every single one.

Let me know in the comments what features you'd like to see in v2!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.