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
- ๐ก๏ธ Security First - Automatically ignores sensitive files and masks secrets
- ๐ก๏ธ Zero Config - Works out of the box with sensible defaults
- โฑ๏ธ Timeout Protection - Handles network issues gracefully with commit message flags
- ๐ 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 & Security (Optional)
Custom Coding Rules
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;
### Security & Ignore Patterns
Create a `.ai-guard-ignore` file to exclude sensitive files from AI review:
```bash
# Sensitive files (automatically ignored)
*.env*
*.key
*.pem
*password*
*secret*
*token*
*api-key*
# Custom patterns
config/database.js
secrets/
.env.*
*-secret.json
api-keys.txt
# Large files
dist/*
build/*
*.min.js
package-lock.json
๐ Built-in Security Features:
- Automatically detects and ignores sensitive files
- Masks API keys and secrets in code diffs
- Filters out files containing
password
,secret
,token
,key
- Skips large files (>50KB by default)
๐ ๏ธ Usage
Automatic Mode (Recommended)
AI Guard runs automatically when you commit:
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
โฑ๏ธ Timeout Protection
๐ Checking staged files...
๐ Reviewing 2 files...
๐ค Sending to AI for review (timeout: 30s)...
โฑ๏ธ AI review timed out after 30 seconds
๐ Added [AI-REVIEW-FAILED: Timeout] to commit message
๐ Security Protection
๐ Checking staged files...
๐ Ignored 2 sensitive/excluded files
๐ Reviewing 1 files...
โ
Code review passed!
๐ 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 |
AI_GUARD_TIMEOUT |
Review timeout in milliseconds | 30000 (30s) |
AI_GUARD_MAX_FILE_SIZE |
Max file size for review in bytes | 50000 (50KB) |
Files
File | Purpose |
---|---|
.code-rules.md |
Your custom coding rules |
.ai-guard-ignore |
Files to exclude from AI review |
.ai-guard-cache/ |
Cache directory (auto-created) |
Advanced Configuration
# Custom timeout (45 seconds)
export AI_GUARD_TIMEOUT=45000
# Custom file size limit (100KB)
export AI_GUARD_MAX_FILE_SIZE=100000
# Use Claude instead of OpenAI
export AI_PROVIDER=claude
export CLAUDE_API_KEY="sk-ant-your-key"
๐ฏ Supported File Types
- JavaScript:
.js
- TypeScript:
.ts
- React:
.jsx
,.tsx
- Vue:
.vue
- Python:
.py
๐ง Advanced Usage
Custom Ignore Patterns
# Create project-specific ignore rules
cat > .ai-guard-ignore << 'EOF'
# Database configs
config/database.js
config/secrets.json
# Generated files
dist/*
build/*
*.generated.js
# Third-party
vendor/*
libs/*
EOF
Different Rules Per Project
# Create project-specific rules
echo "# Strict React Rules\n- All components must have PropTypes" > .code-rules.md
Timeout Configuration
# Longer timeout for large projects
export AI_GUARD_TIMEOUT=60000 # 60 seconds
# Commit with custom timeout
AI_GUARD_TIMEOUT=45000 git commit -m "feat: large refactor"
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
๐ท๏ธ Commit Message Flags
When AI review fails or times out, flags are automatically added to your commit messages:
Timeout Cases
feat: add user authentication
[AI-REVIEW-FAILED: Timeout]
Error Cases
fix: update validation logic
[AI-REVIEW-SKIPPED: Error]
Flag Meanings:
- No flag = AI review completed successfully โ
[AI-REVIEW-FAILED: Timeout]
= Review timed out, manual review recommended โฑ๏ธ[AI-REVIEW-SKIPPED: Error]
= Error occurred (no API key, network issue, etc.) โ
This helps your team track which commits received AI review and which need manual attention.
๐ค 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 |
Security Protection | โ Auto-masks secrets | โ No security features | โ ๏ธ Manual vigilance |
Learning | โ Teaches best practices | โ Just flags errors | โ ๏ธ Inconsistent |
Speed | โ Instant (cached) | โ Very fast | โ Slow |
Flexibility | โ Any rule you write | โ ๏ธ Predefined rules | โ Completely flexible |
Timeout Handling | โ Graceful degradation | โ Always works | โ Always works |
๐ 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
"AI review timed out"
# Increase timeout for large projects
export AI_GUARD_TIMEOUT=60000 # 60 seconds
# Check network connection
curl -I https://api.openai.com
# Try with smaller changesets
git add specific-file.js # Instead of git add .
"Sensitive files being reviewed"
# Check ignore patterns
cat .ai-guard-ignore
# Add patterns for your project
echo "config/secrets.js" >> .ai-guard-ignore
echo "*.env.*" >> .ai-guard-ignore
"Review is too slow"
# Enable caching (default: enabled)
ls -la .ai-guard-cache/
# Reduce file size limit
export AI_GUARD_MAX_FILE_SIZE=25000 # 25KB
# Stage fewer files at once
git add src/specific-file.js
"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
๐ฌ Support & Community
- ๐ Bug Reports: GitHub Issues
- ๐ก Feature Requests: GitHub Discussions
Made with โค๏ธ for better code quality
Star โญ this repo if it helps you write better code!