A pre-commit hook that checks commit messages for Conventional Commits formatting.
Project description
conventional-pre-commit
A pre-commit hook to check commit messages for
Conventional Commits formatting.
Works with Python >= 3.8.
Usage
Make sure pre-commit is installed.
Create a blank configuration file at the root of your repo, if needed:
touch .pre-commit-config.yaml
Add/update default_install_hook_types and add a new repo entry in your configuration file:
default_install_hook_types:
- pre-commit
- commit-msg
repos:
# - repo: ...
- repo: https://github.com/compilerla/conventional-pre-commit
rev: <git sha or tag>
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: []
Install the pre-commit script:
pre-commit install --install-hooks
Make a (normal) commit :x::
$ git commit -m "add a new feature"
[INFO] Initializing environment for ....
Conventional Commit......................................................Failed
- hook id: conventional-pre-commit
- duration: 0.07s
- exit code: 1
[Bad commit message] >> add a new feature
Your commit message does not follow Conventional Commits formatting
https://www.conventionalcommits.org/
And with the --verbose arg:
$ git commit -m "add a new feature"
[INFO] Initializing environment for ....
Conventional Commit......................................................Failed
- hook id: conventional-pre-commit
- duration: 0.07s
- exit code: 1
[Bad commit message] >> add a new feature
Your commit message does not follow Conventional Commits formatting
https://www.conventionalcommits.org/
Conventional Commit messages follow a pattern like:
type(scope): subject
extended body
Please correct the following errors:
- Expected value for type from: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test
Run:
git commit --edit --file=.git/COMMIT_EDITMSG
to edit the commit message and retry the commit.
Make a (conventional) commit :heavy_check_mark::
$ git commit -m "feat: add a new feature"
[INFO] Initializing environment for ....
Conventional Commit......................................................Passed
- hook id: conventional-pre-commit
- duration: 0.05s
Install with pip
conventional-pre-commit can also be installed and used from the command line:
pip install conventional-pre-commit
Then run the command line script:
conventional-pre-commit [types] input
-
[types]is an optional list of Conventional Commit types to allow (e.g.feat fix chore) -
inputis a file containing the commit message to check:
conventional-pre-commit feat fix chore ci test .git/COMMIT_MSG
Or from a Python program:
from conventional_pre_commit.format import is_conventional
# prints True
print(is_conventional("feat: this is a conventional commit"))
# prints False
print(is_conventional("nope: this is not a conventional commit"))
# prints True
print(is_conventional("custom: this is a conventional commit", types=["custom"]))
Passing args
conventional-pre-commit supports a number of arguments to configure behavior:
$ conventional-pre-commit -h
usage: conventional-pre-commit [-h] [--no-color] [--force-scope] [--scopes SCOPES] [--strict] [--verbose] [types ...] input
Check a git commit message for Conventional Commits formatting.
positional arguments:
types Optional list of types to support
input A file containing a git commit message
options:
-h, --help show this help message and exit
--no-color Disable color in output.
--force-scope Force commit to have scope defined.
--scopes SCOPES List of scopes to support. Scopes should be separated by commas with no spaces (e.g. api,client).
--strict Force commit to strictly follow Conventional Commits formatting. Disallows fixup! and merge commits.
--verbose Print more verbose error output.
Supply arguments on the command-line, or via the pre-commit hooks.args property:
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: <git sha or tag>
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: [--strict, --force-scope, feat, fix, chore, test, custom]
NOTE: when using as a pre-commit hook, input is supplied automatically (with the current commit's message).
Development
conventional-pre-commit comes with a VS Code devcontainer
configuration to provide a consistent development environment.
With the Remote - Containers extension enabled, open the folder containing this repository inside Visual Studio Code.
You should receive a prompt in the Visual Studio Code window; click Reopen in Container to run the development environment
inside the devcontainer.
If you do not receive a prompt, or when you feel like starting from a fresh environment:
Ctrl/Cmd+Shift+Pto bring up the command palette in Visual Studio Code- Type
Remote-Containersto filter the commands - Select
Rebuild and Reopen in Containerto completely rebuild the devcontainer - Select
Reopen in Containerto reopen the most recent devcontainer build
Versioning
Versioning generally follows Semantic Versioning.
Making a release
Releases to PyPI and GitHub are triggered by pushing a tag.
- Ensure all changes for the release are present in the
mainbranch - Tag with the new version:
git tag vX.Y.Zfor regular release,git tag vX.Y.Z-preNfor pre-release - Push the new version tag:
git push origin vX.Y.Z
License
Inspired by matthorgan's pre-commit-conventional-commits.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file conventional_pre_commit-4.3.0.tar.gz.
File metadata
- Download URL: conventional_pre_commit-4.3.0.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e88b81076aa343f0f629e2d1ebce9da0fa6af1069063160c1784b21a8ec8f80b
|
|
| MD5 |
afa9dd05f86eec702ae50e29f146252f
|
|
| BLAKE2b-256 |
1971936b974533b09fffb8c23e595551099cc714ff57ddbfcdcac009c5756ba0
|
Provenance
The following attestation bundles were made for conventional_pre_commit-4.3.0.tar.gz:
Publisher:
release.yml on compilerla/conventional-pre-commit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
conventional_pre_commit-4.3.0.tar.gz -
Subject digest:
e88b81076aa343f0f629e2d1ebce9da0fa6af1069063160c1784b21a8ec8f80b - Sigstore transparency entry: 599606214
- Sigstore integration time:
-
Permalink:
compilerla/conventional-pre-commit@5c3f3c61b143a94e9ab5d767ff4d0dc9d6801aa3 -
Branch / Tag:
refs/tags/v4.3.0 - Owner: https://github.com/compilerla
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5c3f3c61b143a94e9ab5d767ff4d0dc9d6801aa3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file conventional_pre_commit-4.3.0-py3-none-any.whl.
File metadata
- Download URL: conventional_pre_commit-4.3.0-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03806ca2a81a90eeda50b57a09c9f0df12aebe0913d2c970eeaaabdb7b3671f7
|
|
| MD5 |
9fabbc1f39db8aea262568c0ae43ed3b
|
|
| BLAKE2b-256 |
80355ee9d397c83c41aafd4cfb0c8a75804e7f061960b2633e6010528d572069
|
Provenance
The following attestation bundles were made for conventional_pre_commit-4.3.0-py3-none-any.whl:
Publisher:
release.yml on compilerla/conventional-pre-commit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
conventional_pre_commit-4.3.0-py3-none-any.whl -
Subject digest:
03806ca2a81a90eeda50b57a09c9f0df12aebe0913d2c970eeaaabdb7b3671f7 - Sigstore transparency entry: 599606242
- Sigstore integration time:
-
Permalink:
compilerla/conventional-pre-commit@5c3f3c61b143a94e9ab5d767ff4d0dc9d6801aa3 -
Branch / Tag:
refs/tags/v4.3.0 - Owner: https://github.com/compilerla
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5c3f3c61b143a94e9ab5d767ff4d0dc9d6801aa3 -
Trigger Event:
push
-
Statement type: