Videos
So, I generally use commits as a saving mechanism, but after adding a linting and formatting pre-commit hook, I do find myself committing less often. While this does help me catch syntax errors, and I guess I could argue that my commits are cleaner, this does seem to be a bit inconvenient. I think part of it is breaking the mold of what I'm used to, but I also wonder if I would be more productive if I moved it to a pre-push, or even to part of my CI pipeline (running before my tests). Does anyone have any recommendations?
I’d make it part of merge checks.
Can’t merge unless all the requirements are OK, if formatting/linting are requirements then it’s nice to see failed checks and know what requires fixing.
Why not both? Provide the tools to run it as a pre-commit hook and require the checks to pass for each PR.
It seems like there was an issue running npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1' because the command part was more than one word. A workaround I found was to split it up into two parts.
1 - Call npx husky add .husky/commit-msg
This created an empty/ default file in the right place with the following content:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
undefined
2 - Then I just replaced undefined with npx --no-install commitlint --edit $1 and it works
This part of the commitlint docs helped me understand that doing it that way was okay
Hope this helps anyone else who encounters the same issue!
You are using Windows. So, try this (it worked for me)
npx husky add .husky/commit-msg "npx --no-install commitlint --edit "$1""
with double quotes instead. Note that $1 also needs double quotes.
Taking commit-msg for example.
#!/bin/bash
MSG="$1"
if ! grep -qE "updated" "$MSG";then
cat "$MSG"
echo "Your commit message must contain the word 'updated'"
exit 1
fi
chmod 755 commit-msg and copy it as .git/hooks/commit-msg.
You can use commitlint with git pre-commit hook.
The simplest way I found to use it is via pre-commit framework. The nice thing is that you can easily add and customize any other check before commiting, here's a list of supported hooks.
Adding commitlint with pre-commit
pip install pre-commit
or brew install pre-commit
Create your .pre-commit-config.yaml (check and update rev if needed):
repos:
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v2.3.0
hooks:
- id: commitlint
stages: [commit-msg]
additional_dependencies: ['@commitlint/config-conventional']
args: ["--config",".commitlintrc.yaml"]
You have a lot of options with a commitlint configuration file .commitlintrc.yaml:
rules:
body-leading-blank: [1, always]
body-max-line-length: [2, always, 100]
footer-leading-blank: [1, always]
footer-max-line-length: [2, always, 100]
header-max-length: [2, always, 100]
subject-case:
- 2
- never
- [sentence-case, start-case, pascal-case, upper-case]
subject-empty: [2, never]
subject-full-stop: [2, never, "."]
type-case: [2, always, lower-case]
type-empty: [2, never]
type-enum:
- 2
- always
- [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]
Install commit message hook
pre-commit install --hook-type commit-msg
And then you can try to git commit.
You can also install commitlint directly via npm, here's how.
# Install commitlint cli and conventional config
npm install --save-dev @commitlint/{config-conventional,cli}
# Configure commitlint to use conventional config
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
I recommend Conventional Commits format.
The commit message should be structured as follows:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Default types:
build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test
Examples:
feat(parser): add ability to parse arraysdocs: correct spelling of CHANGELOGci: add docker build
Some benefits of using this structure:
- Automatically generating CHANGELOGs.
- Automatically determining a semantic version bump (based on the types of commits landed).
- Communicating the nature of changes to teammates, the public, and other stakeholders.