Skip to main content

A Git pre-commit hook that detects AI-generated code patterns and prevents low-quality code from being committed

Project description

MergeGuard

A Git pre-commit hook tool that detects AI-generated code patterns and prevents low-quality code from being committed.

Overview

MergeGuard helps AI-first development teams maintain code quality by catching common issues that AI coding assistants generate before they reach code review. It analyzes staged Git files and blocks commits containing:

  • ๐Ÿ” Dead Code: Unused imports and variables
  • ๐Ÿ› Debug Statements: Leftover print() and console.log() calls
  • ๐Ÿ’ฌ Commented Code: Blocks of commented-out code
  • ๐ŸŒ€ Complexity: Deep nesting (> 4 levels)
  • ๐Ÿšซ Empty Handlers: Empty try/except or catch blocks
  • ๐Ÿ“ Meaningless Comments: Comments that don't add value
  • โ†ฉ๏ธ Unnecessary Else: else clauses after return statements
  • โœ๏ธ TODO Spam: TODOs without context or issue numbers

Installation

Prerequisites

  • Python 3.9 or higher
  • Git repository

Install with pip

pip install merge-guard

Install with pipx (recommended for CLI tools)

pipx install merge-guard

Install from source

git clone https://github.com/yourusername/merge-guard.git
cd merge-guard
uv sync
uv pip install -e .

Usage

Basic Usage

MergeGuard runs as a manual check for now (Stage 1 MVP):

# Make your changes
git add .

# Check for issues before committing
merge-guard check

If issues are found, fix them and try again. If no issues are found, proceed with your commit:

git commit -m "Your commit message"

Verbose Output

Get detailed information about the analysis:

merge-guard check --verbose

Example Output

When issues are detected:

Analyzing 2 file(s)...

  Checking src/app.py...
  Checking src/utils.js...

โœ— Merge Guard detected 3 issues in 2 files:

src/app.py
  Line 5: [DEAD_CODE]
    Unused import 'sys'
    ๐Ÿ’ก Remove this import if not needed

  Line 42: [UNUSED_DEBUG]
    Debug statement: print() statement
    ๐Ÿ’ก Remove debug statement before committing

src/utils.js
  Line 128: [COMPLEXITY]
    Nesting depth of 6 exceeds limit of 4
    ๐Ÿ’ก Consider extracting nested logic into separate functions

Fix these issues and try again.

Analyzed 2 files in 0.15s - Found 3 issues

When code is clean:

Analyzing 1 file(s)...

  Checking src/app.py...

โœ“ No issues detected!
Analyzed 1 file in 0.08s - All clear! โœ“

Supported File Types

MergeGuard currently supports:

  • ๐Ÿ Python (.py)
  • ๐Ÿ“œ JavaScript (.js, .jsx)
  • ๐Ÿ“˜ TypeScript (.ts, .tsx)

Detection Rules

1. DEAD_CODE - Unused Imports

Detects imports that are declared but never used:

# โŒ Bad
import sys  # Unused
import os

print(os.getcwd())
# โœ… Good
import os

print(os.getcwd())

2. UNUSED_DEBUG - Debug Statements

Catches leftover debug statements:

# โŒ Bad
def calculate(x):
    print("Debug:", x)  # Should be removed
    return x * 2
// โŒ Bad
function calculate(x) {
  console.log("Debug:", x); // Should be removed
  debugger;
  return x * 2;
}

3. COMMENTED_CODE - Commented Code Blocks

Flags 3+ consecutive lines of commented-out code:

# โŒ Bad
def process():
    # old_value = x * 2
    # result = old_value + 10
    # return result
    return x

4. COMPLEXITY - Deep Nesting

Detects nesting depth exceeding 4 levels:

# โŒ Bad
if a:
    if b:
        if c:
            if d:
                if e:  # Too deep!
                    return "nested"

5. EMPTY_HANDLERS - Empty Exception Blocks

Catches empty try/except or try/catch blocks:

# โŒ Bad
try:
    risky_operation()
except:
    pass  # Should at least log

6. MEANINGLESS_COMMENT - Obvious Comments

Flags comments that just restate the code:

# โŒ Bad
# Initialize variable x
x = 10
# โœ… Good
# Default timeout in seconds for API requests
x = 10

7. UNNECESSARY_ELSE - Else After Return

Detects unnecessary else clauses:

# โŒ Bad
if condition:
    return "yes"
else:  # Unnecessary
    return "no"
# โœ… Good
if condition:
    return "yes"
return "no"

8. TODO_SPAM - Contextless TODOs

Flags TODOs without proper context:

# โŒ Bad
# TODO: fix this
# โœ… Good
# TODO(#123): Refactor to use new authentication API
# TODO(@username): Handle edge case for empty arrays

Configuration

Currently, MergeGuard uses default rules (Stage 1 MVP). Configuration options will be added in future versions.

Exit Codes

  • 0 - No issues detected, commit can proceed
  • 1 - Issues detected, commit should be blocked

Coming Soon

Stage 2: Local LLM Integration

  • Replace rule-based detection with local LLM (DeepSeek/Llama)
  • Smarter detection of AI-generated patterns
  • Issue-by-issue streaming

Stage 3: Pre-commit Hook Installation

  • merge-guard install - Automatic hook installation
  • Runs automatically on every commit
  • merge-guard uninstall - Remove hook

Stage 4: Override Mechanism

  • Mark false positives
  • Store overrides locally
  • Skip known good patterns

Stage 5: Full File Context

  • Intelligent chunking for large files
  • Better dead code detection with full context

Development

Setup

# Clone the repository
git clone https://github.com/yourusername/merge-guard.git
cd merge-guard

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Sync dependencies
uv sync

# Run tests
uv run pytest tests/ -v

Running Tests

# All tests
uv run pytest

# With coverage
uv run pytest --cov=merge_guard tests/

# Specific test file
uv run pytest tests/test_detector.py -v

Project Structure

merge-guard-cli/
โ”œโ”€โ”€ src/merge_guard/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli.py              # CLI commands
โ”‚   โ”œโ”€โ”€ detector.py         # Detection rules
โ”‚   โ”œโ”€โ”€ formatters.py       # Output formatting
โ”‚   โ”œโ”€โ”€ git_utils.py        # Git operations
โ”‚   โ””โ”€โ”€ models.py           # Data models
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ fixtures/           # Sample files
โ”‚   โ”œโ”€โ”€ test_detector.py    # Detector tests
โ”‚   โ””โ”€โ”€ test_git_utils.py   # Git utility tests
โ”œโ”€โ”€ pyproject.toml          # Project config
โ””โ”€โ”€ README.md

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

Proprietary License - This software is proprietary and confidential.

  • Personal/Evaluation Use: Free for evaluation and personal projects
  • Commercial Use: Requires a commercial license
  • Source Code: Not open source - all rights reserved

For commercial licensing inquiries, contact: ashtilawat23@gmail.com

See LICENSE file for full terms and conditions.

Credits

Built for AI-first development teams who embrace coding agents but want to maintain code quality.


Current Version: 0.1.0 (Stage 1 MVP)

Status: โœ… Rule-based detection working | ๐Ÿšง LLM integration coming in Stage 2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

merge_guard-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

merge_guard-0.1.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file merge_guard-0.1.0.tar.gz.

File metadata

  • Download URL: merge_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for merge_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 679e5aaaf2daf75639cb19d0ccf7531ffca164bd745f4e586b627bf543281510
MD5 70e78fdfcc46d71a3608e1b46822c4a6
BLAKE2b-256 df166407b09ac20cc00c091b5374e47146b90f7bde87c290a48f01d4d469320e

See more details on using hashes here.

File details

Details for the file merge_guard-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: merge_guard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for merge_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60d344125c77b2c47229b9ed5a60205b4f8ba88b26b0c09705e1f4edfc0982a6
MD5 8cc6e4879316bf1739476574fb2c1e0f
BLAKE2b-256 0c92aaf94a199a190164efffad993050dfdc08242c8115548510c7c3b9b982b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page