Package Exports
- ai-commit-guard
- ai-commit-guard/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ai-commit-guard) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
AI Commit Guard
๐ค AI-powered pre-commit code review tool that automatically checks your code changes before commit using OpenAI GPT-4 or Anthropic Claude.
โจ Features
- ๐ Smart Detection - Only reviews staged
.js
,.ts
,.jsx
,.tsx
,.vue
,.py
files - ๐ Lightning Fast - Cached results for unchanged code
- ๐ฏ Custom Rules - Define your own coding standards
- ๐ Multi-Provider - Works with OpenAI GPT-4 or Anthropic Claude
- ๐ก๏ธ Zero Config - Works out of the box with sensible defaults
- ๐ Detailed Feedback - Get specific line-by-line suggestions
๐ Quick Start
1. Install Globally
npm install -g ai-commit-guard
2. Setup in Your Project
# Install husky (if not already installed)
npm install --save-dev husky
npx husky init
# Add AI Guard to pre-commit hook
echo "npx ai-commit-guard" > .husky/pre-commit
chmod +x .husky/pre-commit
3. Set Your AI API Key
Option A: OpenAI (Recommended)
export OPENAI_API_KEY="sk-your-openai-key-here"
Option B: Anthropic Claude
export CLAUDE_API_KEY="sk-ant-your-claude-key-here"
export AI_PROVIDER="claude"
4. That's It! ๐
Now AI Guard will automatically review your code on every commit:
git add .
git commit -m "feat: add user authentication"
# โ AI Guard automatically reviews your changes
๐ Custom Rules (Optional)
Create a .code-rules.md
file in your project root to define custom coding standards:
# My Code Review Rules
## Magic Strings
- No string literals in code, use constants
- Event properties should use EVENT_FIELDS constant
## Functions
- Functions should not exceed 20 lines
- Use descriptive function names
- Follow single responsibility principle
## Variables
- Use camelCase for variables
- Boolean variables should start with is/has/can/should
- No single letter variables except for loops
## Error Handling
- Always use try-catch for async operations
- Log errors with appropriate context
## Examples
โ **Bad:**
```javascript
event['status'] = 'active';
function getData() { /* 30 lines of mixed responsibilities */ }
let x = true;
โ Good:
const EVENT_FIELDS = { STATUS: 'status' };
event[EVENT_FIELDS.STATUS] = 'active';
function fetchUserData() { /* single responsibility */ }
function processUserData() { /* separate concern */ }
let isUserActive = true;
## ๐ ๏ธ Usage
### Automatic Mode (Recommended)
AI Guard runs automatically when you commit:
```bash
git add src/
git commit -m "refactor: improve error handling"
# ๐ Checking staged files...
# ๐ Reviewing 3 files...
# ๐ค Sending to AI for review...
# โ
Code review passed!
Manual Mode
Run review without committing:
npx ai-commit-guard
Test Mode
Check if everything is working:
# Test with sample code
npx ai-commit-guard --test
๐ Example Output
โ Passing Review
๐ Checking staged files...
๐ Reviewing 2 files...
๐ค Sending to AI for review...
โ
Code review passed!
๐ก Suggestions:
โข Consider adding JSDoc comments for public functions
โข Variable naming looks great, well done!
โ Failing Review
๐ Checking staged files...
๐ Reviewing 3 files...
๐ค Sending to AI for review...
โ Code review failed!
Issues found:
โข src/utils.js: Magic string 'status' on line 15
Fix: Use EVENT_FIELDS.STATUS constant
โข src/api.js: Function getUserData() is 25 lines long
Fix: Split into fetchUser() and processUser() functions
โข src/components/User.tsx: Variable 'x' is not descriptive
Fix: Rename to 'isUserLoggedIn' or similar
โ๏ธ Configuration
Environment Variables
Variable | Description | Default |
---|---|---|
OPENAI_API_KEY |
Your OpenAI API key | - |
CLAUDE_API_KEY |
Your Claude API key | - |
AI_PROVIDER |
AI provider: openai or claude |
openai |
Files
File | Purpose |
---|---|
.code-rules.md |
Your custom coding rules |
.ai-guard-cache/ |
Cache directory (auto-created) |
๐ฏ Supported File Types
- JavaScript:
.js
- TypeScript:
.ts
- React:
.jsx
,.tsx
- Vue:
.vue
- Python:
.py
๐ง Advanced Usage
Different Rules Per Project
# Create project-specific rules
echo "# Strict React Rules\n- All components must have PropTypes" > .code-rules.md
Temporary Disable
# Skip AI review for this commit
git commit -m "docs: update README" --no-verify
Clear Cache
# Remove cached reviews
rm -rf .ai-guard-cache
๐ค Why AI Commit Guard?
Problem | Solution |
---|---|
๐ฐ Inconsistent code style across team | โ Enforces rules automatically |
๐ด Bugs slip through manual review | โ AI catches common mistakes |
๐ฌ Junior developers need guidance | โ Educational feedback on every commit |
๐ค Code review takes too long | โ Pre-filter obvious issues |
๐ต Forget to follow team standards | โ Instant feedback at commit time |
๐ Comparison
Feature | AI Commit Guard | ESLint | Manual Review |
---|---|---|---|
Custom Rules | โ Natural language | โ ๏ธ Config syntax | โ Human judgment |
Context Aware | โ Understands logic | โ Syntax only | โ Full context |
Learning | โ Teaches best practices | โ Just flags errors | โ ๏ธ Inconsistent |
Speed | โ Instant (cached) | โ Very fast | โ Slow |
Flexibility | โ Any rule you write | โ ๏ธ Predefined rules | โ Completely flexible |
๐ Troubleshooting
Common Issues
"No AI API key found"
# Check if key is set
echo $OPENAI_API_KEY
# Set the key
export OPENAI_API_KEY="sk-your-key-here"
# Make it permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export OPENAI_API_KEY="sk-your-key-here"' >> ~/.bashrc
"Not a git repository"
# Initialize git first
git init
git add .
git commit -m "initial commit"
"No staged files to review"
# Make sure files are staged
git status
git add .
"AI connection failed"
# Check your API key and internet connection
curl -H "Authorization: Bearer $OPENAI_API_KEY" https://api.openai.com/v1/models
Performance Tips
- ๐โโ๏ธ Enable caching: Cache is enabled by default
- ๐ Stage only what you need:
git add specific-file.js
- ๐ฏ Use specific rules: More specific rules = faster reviews
- ๐งน Clean cache periodically:
rm -rf .ai-guard-cache
๐ Roadmap
- Support for more file types (Go, Rust, C++)
- Integration with popular IDEs
- Team dashboards and analytics
- Custom AI model fine-tuning
- Slack/Discord notifications
- CI/CD pipeline integration
๐ค Contributing
We love contributions! Here's how to help:
- ๐ด Fork the repository
- ๐ฑ Create your feature branch:
git checkout -b my-feature
- โ
Commit your changes:
git commit -m 'Add cool feature'
- ๐ Push to the branch:
git push origin my-feature
- ๐ Open a Pull Request
๐ License
MIT ยฉ Adem Alkan
Made with โค๏ธ for better code quality
Star โญ this repo if it helps you write better code!