cargo-commitlint
A Rust-based commit message linter following the Conventional Commits specification. Similar to Node.js's commitlint, but written entirely in Rust and designed for the Rust ecosystem.
Features
- ✅ Validates commit messages against Conventional Commits specification
- ✅ Configurable via TOML (similar to
commitlint) - ✅ Git hook integration (similar to
cargo-husky) - ✅ Supports all standard Conventional Commit types
- ✅ Customizable rules for type, scope, subject, body, and footer
- ✅ Regex-based commit message parsing
- ✅ Ignore patterns for skipping validation
Installation
From Source
From Crates.io (when published)
Usage
After installation, cargo-commitlint is available as a cargo subcommand. Use it with:
Git Hooks with cargo-husky
This project uses cargo-husky to manage git hooks. The hooks are automatically installed when you run cargo test.
Available Hooks
- pre-commit: Runs
cargo fmt --checkandcargo clippyto ensure code quality - pre-push: Runs
cargo testto ensure all tests pass before pushing - commit-msg: Validates commit messages using
cargo commitlintand Conventional Commits
Installing Hooks
Simply run:
This will compile the project and install all git hooks configured in .cargo-husky/hooks/.
Manual Git Hook Installation (Alternative)
If you prefer to use cargo commitlint's built-in hook installer instead of cargo-husky:
This will create a .git/hooks/commit-msg hook that validates all commit messages using cargo commitlint.
Uninstall Git Hook
Remove the git hook:
Validate Commit Messages
Validate a commit message directly:
# Validate from command line
# Validate from stdin
|
Configuration
Create a commitlint.toml or .commitlint.toml file in your project root. You can copy commitlint.example.toml as a starting point:
Example Configuration
[]
# Type validation
[]
= ["feat", "fix", "docs", "style", "refactor", "test", "chore"]
= "lowercase"
# Scope validation
[]
= [] # Empty means all scopes allowed
= "lowercase"
# Subject validation
= ["sentence-case"]
= false
= "."
# Header validation
= 72
= 0
# Body validation
= true
= 100
# Footer validation
= true
= 100
# Parser configuration
[]
= "^(?P<type>\\w+)(?:\\((?P<scope>[^)]+)\\))?(?P<breaking>!)?:\\s(?P<subject>.*)$"
# Ignore patterns (regex)
= [
# "Merge.*",
# "Revert.*",
]
Conventional Commits Format
The tool validates commit messages in the following format:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Types
feat: A new featurefix: A bug fixdocs: Documentation only changesstyle: Changes that do not affect the meaning of the coderefactor: A code change that neither fixes a bug nor adds a featureperf: A code change that improves performancetest: Adding missing tests or correcting existing testsbuild: Changes that affect the build system or external dependenciesci: Changes to CI configuration files and scriptschore: Other changes that don't modify src or test filesrevert: Reverts a previous commit
Examples
Valid commit messages:
feat: add user authentication
feat(api): add new endpoint
fix: resolve memory leak in parser
docs: update README with installation instructions
feat!: breaking change in API
feat: add feature
This is a longer description of the change.
Closes #123
Invalid commit messages:
invalid: bad commit type
feat:Missing colon
feat: subject too long and exceeds the maximum length limit of 72 characters
Configuration Options
Rules
rules.type.enum: List of allowed commit types (empty = all allowed)rules.type.case: Case requirement (lowercase,uppercase,camel-case,kebab-case,pascal-case,snake-case)rules.scope.enum: List of allowed scopes (empty = all allowed)rules.scope.case: Case requirement for scoperules.subject_case: List of allowed case formats (sentence-case,lowercase,uppercase,start-case)rules.subject_empty: Whether subject can be emptyrules.subject_full_stop: Character that should not appear at end of subjectrules.header_max_length: Maximum header lengthrules.header_min_length: Minimum header lengthrules.body_leading_blank: Require blank line before bodyrules.body_max_line_length: Maximum line length in bodyrules.footer_leading_blank: Require blank line before footerrules.footer_max_line_length: Maximum line length in footer
Parser
parser.pattern: Regex pattern for parsing conventional commitsparser.correspondence: Map regex capture groups to commit fields
Ignores
ignores: List of regex patterns for commits to skip validation
Integration with Cargo
This tool is designed to work seamlessly with Rust projects and integrates with cargo-husky for comprehensive git hook management.
Using cargo-husky (Recommended)
This project includes cargo-husky configuration with pre-commit, pre-push, and commit-msg hooks:
- Pre-commit hook: Ensures code is formatted (
cargo fmt) and passes clippy checks - Pre-push hook: Runs all tests before allowing pushes
- Commit-msg hook: Validates commit messages using
cargo commitlint
To activate the hooks, simply run:
The hooks are defined in .cargo-husky/hooks/ and will be automatically installed.
Using cargo commitlint's Built-in Hook Installer
Alternatively, you can use cargo commitlint's built-in installer:
This will install only the commit-msg hook for commit message validation.
License
Licensed under the MIT License.
Copyright (c) 2025 Pegasus Heavy Industries LLC
See LICENSE or LICENSE-MIT for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.