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.

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 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: []

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/

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' but found none.
  - Expected value for 'delim' but found none.
  - Expected value for 'subject' but found none.

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)

  • 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"]))

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  Optional 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! style 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:

  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

Versioning

Versioning generally follows Semantic Versioning.

Making a release

Releases to PyPI and GitHub are triggered by pushing a tag.

  1. Ensure all changes for the release are present in the main branch
  2. Tag with the new version: git tag vX.Y.Z for regular release, git tag vX.Y.Z-preN for pre-release
  3. Push the new version tag: git push origin vX.Y.Z

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-3.6.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

conventional_pre_commit-3.6.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for conventional_pre_commit-3.6.0.tar.gz
Algorithm Hash digest
SHA256 53dbfdf557c794c09da98c0acdeb26f61c0b49be07753ccf64d8e5f200d5be3f
MD5 65d9ecee76383b6a3373c0a0c600342c
BLAKE2b-256 2932bc8656811b6c3179008ced919ca91a9dbf36bfd09e8abd953b917aebc4fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for conventional_pre_commit-3.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be12b7f8af4363b5e4959fd540c6529eaf025654027e14d6b1e31aad7689be85
MD5 5a5bd992ef774af676d350f62537e977
BLAKE2b-256 cd812e24ab261f1df793f2500c7f941b5082a764aa576cce4621c088b3197b5d

See more details on using hashes here.

Supported by

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