Loved the answer provided by @anthony-sottile above, and I adapted it to work for conventional commit messages! This is my first answer contribution too!
Solution
Create a .pre-commit-config.yaml file in your project's root directory, or just add this hook to your existing file.
# project_root/.pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: commit-msg
name: conventional commit messages
language: pygrep
entry: '^(chore|test|feat|fix|build|docs|refactor)!?: ((?!.*(ing))(?!.*(ed))).*$'
args:
- --multiline
- --negate # fails if the entry is NOT matched
stages:
- commit-msg
Be sure to install the commit-msg
hook as well or this pre-commit hook stage won't work. These commands can be run in a bash terminal individually.
pip install pre-commit && \
pre-commit install --hook-type commit-msg && \
git commit -m "failing commit message" --allow-empty
Adapting Guidance
Some clarification of the regex if anyone needs to repurpose this for their awesome use-case:
- Regex Cheat Sheet to understand the syntax
- Regex Simulator to help getting your text matched