Skip to main content

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.

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 a new repo entry to your configuration file:

repos:

# - repo: ...

  - repo: https://github.com/compilerla/conventional-pre-commit
    rev: <git sha or tag>
    hooks:
      - id: conventional-pre-commit
        stages: [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-msg

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/

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 test

Example commit message adding a feature:

    feat: implement new API

Example commit message fixing an issue:

    fix: remove infinite loop

Optionally, include a scope in parentheses after the type for more context:

    fix(account): remove infinite loop

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

Where [types] is an optional list of Conventional Commit types to allow (e.g. feat fix chore)

And input is 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"]))

Versioning

Versioning generally follows Semantic Versioning.

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:

  1. Ctrl/Cmd+Shift+P to bring up the command palette in Visual Studio Code
  2. Type Remote-Containers to filter the commands
  3. Select Rebuild and Reopen in Container to completely rebuild the devcontainer
  4. Select Reopen in Container to reopen the most recent devcontainer build

License

Apache 2.0

Inspired by matthorgan's pre-commit-conventional-commits.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

conventional_pre_commit-2.1.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

conventional_pre_commit-2.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file conventional_pre_commit-2.1.1.tar.gz.

File metadata

  • Download URL: conventional_pre_commit-2.1.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for conventional_pre_commit-2.1.1.tar.gz
Algorithm Hash digest
SHA256 fcd21246afe8db9892a90cf1440639d2bae948b9cc99d5f92b5589b845185863
MD5 bfee07822d0b2a5133dba78de475af75
BLAKE2b-256 51d404fcde6317de1c9739fa59057852de9914b3fdb2529c153815bde41c4de4

See more details on using hashes here.

File details

Details for the file conventional_pre_commit-2.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for conventional_pre_commit-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d7ccc2a7556903119911a89b0b30a102b89c0001b22582978c00600252b4a5
MD5 e94b41fb081d1d3f9717285a7fb89434
BLAKE2b-256 0499a4ae01dff8499b2f71108aa3ec5216bfc1ef1882cbe90cf9bf4f176a7ec4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page