180 lines
4.1 KiB
Markdown
180 lines
4.1 KiB
Markdown
# Git Commit Prefix Generator
|
|
|
|
A Rust CLI tool that automatically generates conventional git commit message prefixes by analyzing git diffs. It suggests commit types (feat, fix, docs, style, refactor, test, chore) and auto-detects scopes based on changed file paths, helping developers maintain consistent commit conventions without external dependencies.
|
|
|
|
## Features
|
|
|
|
- **Automatic commit type detection** - Maps changed files to conventional commit types
|
|
- **Scope auto-detection** - Extracts scope names from file paths (e.g., 'auth' from `src/auth/login.rs`)
|
|
- **Multiple output formats** - Short, verbose, compact, and JSON for scripting
|
|
- **Interactive mode** - Prompts for confirming/selecting types and scopes
|
|
- **Configuration support** - Load custom conventions from `.commitlintrc` or custom config files
|
|
- **Staged changes support** - Analyze both working directory and staged changes
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
cargo install git-commit-prefix-gen
|
|
```
|
|
|
|
Or build from source:
|
|
|
|
```bash
|
|
git clone https://github.com/user/git-commit-prefix-gen
|
|
cd git-commit-prefix-gen
|
|
cargo build --release
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Basic usage
|
|
|
|
```bash
|
|
git-commit-prefix-gen
|
|
```
|
|
|
|
### Specify output format
|
|
|
|
```bash
|
|
git-commit-prefix-gen --format short # Default: feat(auth): add login
|
|
git-commit-prefix-gen --format verbose # Detailed output with file list
|
|
git-commit-prefix-gen --format compact # No spaces: feat(auth):add login
|
|
git-commit-prefix-gen --format json # JSON for scripting
|
|
```
|
|
|
|
### Override detected type/scope
|
|
|
|
```bash
|
|
git-commit-prefix-gen --type feat --scope api
|
|
```
|
|
|
|
### Analyze staged changes
|
|
|
|
```bash
|
|
git-commit-prefix-gen --staged
|
|
```
|
|
|
|
### Interactive mode
|
|
|
|
```bash
|
|
git-commit-prefix-gen --interactive
|
|
```
|
|
|
|
### Use custom config file
|
|
|
|
```bash
|
|
git-commit-prefix-gen --config .commitlintrc.json
|
|
```
|
|
|
|
### Dry run (no changes)
|
|
|
|
```bash
|
|
git-commit-prefix-gen --dry-run
|
|
```
|
|
|
|
## Supported Commit Types
|
|
|
|
| Type | Description |
|
|
|------|-------------|
|
|
| `feat` | A new feature |
|
|
| `fix` | A bug fix |
|
|
| `docs` | Documentation only changes |
|
|
| `style` | Changes that do not affect the meaning of the code (white-space, formatting, etc) |
|
|
| `refactor` | A code change that neither fixes a bug nor adds a feature |
|
|
| `test` | Adding missing tests or correcting existing tests |
|
|
| `chore` | Changes to the build process or auxiliary tools |
|
|
| `build` | Changes to the build system |
|
|
| `ci` | Changes to CI configuration |
|
|
| `perf` | A code change that improves performance |
|
|
| `revert` | Reverts a previous commit |
|
|
|
|
## Configuration
|
|
|
|
The tool supports custom configuration files. By default, it searches for:
|
|
|
|
- `.commitlintrc.js`
|
|
- `.commitlintrc.json`
|
|
- `.commitlintrc.yaml`
|
|
- `.commitlintrc.yml`
|
|
- `commitlint.config.js`
|
|
- `.git-commit-prefix-gen.yaml`
|
|
- `.git-commit-prefix-gen.json`
|
|
|
|
### Example Configuration
|
|
|
|
```yaml
|
|
type_mapping:
|
|
- pattern: "src/**/*.rs"
|
|
type: "feat"
|
|
- pattern: "tests/**/*.rs"
|
|
type: "test"
|
|
- pattern: "docs/**/*.md"
|
|
type: "docs"
|
|
|
|
scope_patterns:
|
|
- pattern: "^src/([^/]+)/"
|
|
base: "src"
|
|
- pattern: "^lib/([^/]+)/"
|
|
base: "lib"
|
|
|
|
ignored_patterns:
|
|
- "^target/"
|
|
- "^node_modules/"
|
|
- "^\\.git/"
|
|
```
|
|
|
|
## Exit Codes
|
|
|
|
| Code | Description |
|
|
|------|-------------|
|
|
| 0 | Success |
|
|
| 1 | No changes detected or error |
|
|
| 128 | Not in a git repository |
|
|
|
|
## Library Usage
|
|
|
|
```rust
|
|
use git_commit_prefix_gen::{Analyzer, CommitConvention};
|
|
|
|
fn main() {
|
|
let convention = CommitConvention::default();
|
|
let analyzer = Analyzer::with_convention(convention);
|
|
|
|
let changes = analyzer.analyze(&std::env::current_dir().unwrap()).unwrap();
|
|
let suggestions = analyzer.generate_suggestions(&changes);
|
|
|
|
for suggestion in &suggestions {
|
|
println!("{}", suggestion);
|
|
}
|
|
}
|
|
```
|
|
|
|
## Development
|
|
|
|
```bash
|
|
# Run tests
|
|
cargo test
|
|
|
|
# Run tests with verbose output
|
|
cargo test -- --nocapture
|
|
|
|
# Build
|
|
cargo build
|
|
|
|
# Build release
|
|
cargo build --release
|
|
|
|
# Run examples
|
|
cargo run --example basic_usage
|
|
cargo run --example custom_config
|
|
```
|
|
|
|
## Dependencies
|
|
|
|
- git2 - Git bindings
|
|
- clap - CLI argument parsing
|
|
- serde - Serialization
|
|
- regex - Pattern matching
|
|
- anyhow - Error handling
|
|
- dialoguer - Interactive prompts
|