A pre-commit hook to check commit messages for
Conventional Commits formatting.
Make sure pre-commit is installed.
Create a blank configuration file at the root of your repo, if needed:
touch .pre-commit-config.yamlAdd a new repo entry to your configuration file:
repos:
# - repo: ...
- repo: https://github.com/compilerla/conventional-pre-commitrev: <git sha or tag>hooks:
- id: conventional-pre-commitstages: [commit-msg]args: [] # optional: list of Conventional Commits types to allow e.g. [feat, fix, ci, chore, test]Install the pre-commit script:
pre-commit install --hook-type commit-msgMake a (normal) commit ❌:
$ 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 featureYour commit message does not follow Conventional Commits formattinghttps://www.conventionalcommits.org/Conventional Commits start with one of the below types, followed by a colon,followed by the commit message: build chore ci docs feat fix perf refactor revert style testExample commit message adding a feature: feat: implement new APIExample commit message fixing an issue: fix: remove infinite loopOptionally, include a scope in parentheses after the type for more context: fix(account): remove infinite loopMake a (conventional) commit ✔️:
$ git commit -m "feat: add a new feature"[INFO] Initializing environment for ....Conventional Commit......................................................Passed- hook id: conventional-pre-commit- duration: 0.05sconventional-pre-commit can also be installed and used from the command line:
pip install conventional-pre-commitThen 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_MSGOr from a Python program:
fromconventional_pre_commit.formatimportis_conventional# prints Trueprint(is_conventional("feat: this is a conventional commit"))
# prints Falseprint(is_conventional("nope: this is not a conventional commit"))
# prints Trueprint(is_conventional("custom: this is a conventional commit", types=["custom"]))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 generally follows Semantic Versioning.
Releases to PyPI are triggered by publishing a release on GitHub.
Create a branch
chore/releaseBump the version in
pyproject.tomlPR, merge
chore/releaseintomainTag
mainwith the new version (prefixed byv):git fetch git reset --hard origin/main git tag vX.Y.Z git push origin vX.Y.Z
Publish a pre-release to push the new package to TestPyPI
Publish a regular Release to push the new package to PyPI
Inspired by matthorgan's pre-commit-conventional-commits.