@light-cat/ai-commit-msg
v1.1.4
Published
AI-powered commit message generator for git
Downloads
430
Maintainers
Readme
@light-cat/ai-commit-msg
AI-powered commit message generator for git. Automatically generates conventional commit messages based on your staged changes.
Features
- 🤖 AI-powered: Uses AI to analyze your code changes and generate meaningful commit messages
- 📝 Conventional Commits: Follows the Conventional Commits format
- 🔧 Easy Configuration: Simple
.envsetup with environment variable support - 🛡️ Safe: Doesn't block commit on errors - falls back gracefully
- 🔌 OpenAI Compatible: Works with OpenAI, Azure OpenAI, and any OpenAI-compatible API
- ⚡ CLI Ready: Can be used directly with
pnpm execor installed globally
Installation
Local Installation (Recommended)
# With npm
npm install --save-dev @light-cat/ai-commit-msg
# With pnpm
pnpm add --save-dev @light-cat/ai-commit-msg
# Set up git hooks (installs simple-git-hooks if needed)
pnpm exec ai-commit setupGlobal Installation
# With npm
npm install -g @light-cat/ai-commit-msg
# With pnpm
pnpm add -g @light-cat/ai-commit-msg
# Then set up git hooks in your project
cd /path/to/your/project
pnpm exec ai-commit setupConfiguration
Create a .env file in your project root or set environment variables:
# Required
AI_API_KEY=your-api-key-here
# Optional (defaults shown)
AI_API_BASE_URL=https://api.openai.com/v1
AI_MODEL=gpt-4Supported AI Providers
| Provider | AI_API_BASE_URL | AI_MODEL |
|----------|-------------------|------------|
| OpenAI | https://api.openai.com/v1 | gpt-4, gpt-3.5-turbo |
| Azure OpenAI | Your Azure endpoint | Your deployment name |
| Local/Other | Your custom endpoint | Any OpenAI-compatible model |
Getting API Keys
- OpenAI: Get your API key from platform.openai.com
Usage
Basic Usage
After setting up the git hook, simply commit your changes without providing a message:
# Stage your changes
git add .
# Commit - AI will automatically generate a commit message
git commitThe tool will:
- Fetch staged changes
- Send them to the AI service
- Generate a conventional commit message
- Write it to the commit message file
Automatic Setup
# Install hooks and configure simple-git-hooks automatically
pnpm exec ai-commit setupManual Setup (if you already have simple-git-hooks)
Add to your package.json:
{
"simple-git-hooks": {
"prepare-commit-msg": "pnpm exec ai-commit"
}
}Or if using lefthook:
# lefthook.yml
pre-commit:
commands:
generate-commit-msg:
run: pnpm exec ai-commitStandalone Usage
# Dry run (show message without writing)
pnpm exec ai-commit --dry-run
# Verbose mode (show debug info)
pnpm exec ai-commit --verbose
# Help
pnpm exec ai-commit --helpHow It Works
- When you run
git commit, theprepare-commit-msghook is triggered - The tool fetches:
- Staged changes (
git diff --cached) - Current branch name
- Last commit message (if available)
- Staged changes (
- Sends this information to the AI service
- Generates a conventional commit message
- Writes it to the commit message file
Example
Given this staged change:
diff --git a/src/auth.ts b/src/auth.ts
+export function validateEmail(email: string): boolean {
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
+}The tool might generate:
feat(auth): add email validation functionTroubleshooting
"AI_API_KEY is not set"
Make sure your .env file exists and contains AI_API_KEY, or set the environment variable before committing.
Commit message not being generated
- Check if changes are staged (
git addfirst) - Run with
--verboseto see debug information - Ensure your API key has sufficient credits/permissions
- Verify git hooks are installed:
cat .git/hooks/prepare-commit-msg
Want to use a custom message?
Simply provide a commit message when running git commit -m "your message" - the tool will detect an existing message and skip generation.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Local Development
Debugging
1. Direct Source Execution
Run the source code directly with tsx (recommended for ESM projects):
cd /path/to/node-ai-commit
pnpm tsx bin/ai-commit.js --helpOr using the dev script:
pnpm dev -- --helpNote:
ts-nodehas limited ESM support. Usetsxinstead for better compatibility.
2. Using npm link
Link the local package globally and test in other projects:
cd /path/to/node-ai-commit
pnpm link
# In another project
cd /path/to/your-project
pnpm link @light-cat/ai-commit-msg
pnpm exec ai-commit setup3. Local Path Installation
In another project's package.json:
{
"devDependencies": {
"@light-cat/ai-commit-msg": "link:/path/to/node-ai-commit"
}
}Then run pnpm install or npm install in that project.
4. Build and Test
# Build the TypeScript
pnpm build
# Run locally (requires build first)
node bin/ai-commit.js --help
# Or run directly without building
pnpm tsx bin/ai-commit.js --helpTesting Setup
- Create a test repository:
mkdir test-ai-commit && cd test-ai-commit
git init- Link the local package:
cd /path/to/node-ai-commit
pnpm link --global
cd /path/to/test-ai-commit
pnpm link --global @light-cat/ai-commit-msg- Set up and test:
# Create .env with your API key
pnpm exec ai-commit setup
# Make some changes and commit (don't provide message, let AI generate it)
git add .
git commit