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()andconsole.log()calls - ๐ฌ Commented Code: Blocks of commented-out code
- ๐ Complexity: Deep nesting (> 4 levels)
- ๐ซ Empty Handlers: Empty
try/exceptorcatchblocks - ๐ Meaningless Comments: Comments that don't add value
- โฉ๏ธ Unnecessary Else:
elseclauses afterreturnstatements - โ๏ธ 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 proceed1- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
679e5aaaf2daf75639cb19d0ccf7531ffca164bd745f4e586b627bf543281510
|
|
| MD5 |
70e78fdfcc46d71a3608e1b46822c4a6
|
|
| BLAKE2b-256 |
df166407b09ac20cc00c091b5374e47146b90f7bde87c290a48f01d4d469320e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60d344125c77b2c47229b9ed5a60205b4f8ba88b26b0c09705e1f4edfc0982a6
|
|
| MD5 |
8cc6e4879316bf1739476574fb2c1e0f
|
|
| BLAKE2b-256 |
0c92aaf94a199a190164efffad993050dfdc08242c8115548510c7c3b9b982b0
|