A pre-commit hook for parsing git branch names and prefixing them with valid headers as per the Conventional Commits standards.
This hook supports commit message creation from the command line in two ways:
git commit
- which will pull in a commit message template if one existsgit commit -m '<message>
git commit -f
should work, but is untested. If you use this hook and use that method, let me know.
Note
If you have run pre-commit install
but are still not seeing the hook run before every commit, try running pre-commit install --hook-type prepare-commit-msg
-
In order to use this pre-commit hook, you first need to install the pre-commit library. More info here
-
In the root of the project you're interested in working in, create a
.pre-commit-config.yaml
file. -
Run
pre-commit install
to install the githook in your repository -
Enjoy!
default_install_hook_types: [pre-commit, prepare-commit-msg]
repos:
- repo: https://github.com/SethAngell/semantic-commit-formatter
rev: v1.1.4
hooks:
- id: semantic_commit
stages: [prepare-commit-msg]
There are a number of prefix types defined in the conventional commit standard. This hook comes preloaded with the following mappings:
{
"bug": "fix",
"pdi": "fix",
"tech-debt": "fix",
"story": "feat",
"tech-task": "chore"
}
If you'd like to configure your own mappings, you can create a json file at ~/.config/pre-commit-type-mappings.json
in the format of <YOUR BRANCH PREFIX> : <CONVENTIONAL COMMIT TYPE>
.
If you want to really lean into the semantic commit standards, you can add the --strict
arg to your config file. This will cause errors to be thrown when commits and branches don't adhere closely to the semantic configuration standard
If you're experiencing issues with the hook, adding the arg --log
to your config will generate a log file with further information as to what's happening within the hook. This file is created at ~/Library/Logs/prepare-commit-message-hook/hook.log
.
This hook expects that branch names will be written in the following format: <branch_type>/<ticket_name>
. If you have enabled the strict setting the hook will fail if your branch does not conform to this format.
Conventional Commits are traditionally capped at a 72 character line length. If you have enabled the strict setting the hook will fail if your commit message ends up being longer than 72 characters.