A Git pre-commit hook that validates commit messages against specified rules, ensuring consistent message formatting and language usage.
- Validates commit messages against the Conventional Commits format
- Supports various text validation rules:
- Restrictive rules (allow ONLY specified characters):
noCyrillic: Prevents Cyrillic charactersnoLatin: Prevents Latin charactersnoDigits: Prevents digitscyrillicOnly: Allows only Cyrillic characterslatinOnly: Allows only Latin charactersdigitsOnly: Allows only digits
- Permissive rules (ALLOW but don't require):
allowLatin: Allows Latin characters, digits, and basic punctuationallowCyrillic: Allows Cyrillic characters, digits, and basic punctuationallowDigits: Allows Latin characters, digits, and basic punctuationallowScope: Special rule for scopes that allows Latin, digits, and hyphens (must start and end with alphanumeric)allowPathScope: Special rule for scopes that allows slash-delimitedallowScopepath segments
- Restrictive rules (allow ONLY specified characters):
- Configurable validation rules for different parts of the commit message (type, scope, description)
- Body text is not validated by default
To use this hook in your project:
- Install pre-commit if you haven't already:
pip install pre-commit- Add this to your
.pre-commit-config.yaml:
repos:
- repo: https://github.com/AnruKitakaze/commit-msg-guardian
rev: v0.1.6 # Use the latest version
hooks:
- id: commit-msg-guardian
# Optional: override default rules
args:
- --type-rules=allowLatin
- --scope-rules=allowScope
- --description-rules=noCyrillic- Install the commit-msg hook:
pre-commit install --hook-type commit-msgNote: The standard pre-commit install command won't work for this hook as it's a commit-msg hook, not a pre-commit hook. Make sure to use the command above.
The hook validates commit messages against the following format:
type(scope): description
[optional body]
The following commit types are supported:
feat: New featurefix: Bug fixdocs: Documentation changesstyle: Code style changes (formatting, etc.)refactor: Code refactoringperf: Performance improvementstest: Adding or modifying testsbuild: Build system changesci: CI configuration changeschore: General maintenancerevert: Reverting changes
You can customize the validation rules using command line arguments:
--type-rules: Comma-separated rules for commit type (default: "allowLatin")--scope-rules: Comma-separated rules for commit scope (default: "allowScope")--description-rules: Comma-separated rules for commit description (default: "noCyrillic")
Use --scope-rules=allowPathScope to allow slash-delimited scopes such as app/api or this/is/some/path.
Valid commit messages:
feat(TGK-1827): This is an example
docs(T-1): This is valid too
feat(T1): Another valid example
feat(task-123): Valid with hyphen
feat(app/api): Valid with slash-delimited folders when using --scope-rules=allowPathScope
With кириллица in description (body is not validated by default)
Invalid commit messages:
feat(TGK-1827): Забыл убрать кириллицу # Contains Cyrillic in description
random: Not a valid type # Invalid commit type
feat[scope]: Wrong scope format # Invalid scope format
feat(-T1): Invalid scope format # Scope can't start with hyphen
feat(T1-): Invalid scope format # Scope can't end with hyphen
feat(app//api): Invalid scope format # Scope can't contain empty slash segments
Feel free to open issues and pull requests!