Reputation: 514
I'm trying to enforce a git commit message policy to keep my repositories clean and tidy. I've seen the official docs about server-side and client-side hooks and then I bumped on husky.
So far I could work with the first but couldn't set up husky, I've still got plenty to learn. The main idea is to be able to work on a new workstation without having to manually set up any client-side hooks.
Could someone explain how I can set up husky to check my commit messages or even make an example?
This is my commit-msg hook in project-root/githooks
folder:
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
$regex = /([resolved|fixed]) #([0-9])* ([A-Z])\w+/
if !$regex.match(message)
puts "[POLICY] Your message is not formatted correctly!"
puts "Message format must be like:"
puts "resolved #123 Case title (for features)"
puts "fixed #123 Case title (for bugs)"
puts "First letter of 'Case title' must be capitalized!"
exit 1
end
I've tried to add the script to the package.json:
"scripts": {
... : ...,
"commitmsg": "sh hooks/commit-msg",
... : ...
}
The hook does not work. All messages pass. If put in .git/hooks it works normally.
Here's a screenshot of a test project with the package.json, the commit-msg hook and the error it gives out.
The same hook, put in .git/hooks folder, works just fine.
Upvotes: 35
Views: 55312
Reputation: 6748
With Husky 7+, you can add the following to .husky/commit-msg
file:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
message="$(cat $1)"
requiredPattern="^(add|cut|fix|bump|make|start|stop|refactor|reformat|optimise|document|merge) .*$"
if ! [[ $message =~ $requiredPattern ]];
then
echo "-"
echo "-"
echo "-"
echo "🚨 Wrong commit message! 😕"
echo "The commit message must have this format:"
echo "<verb in imperative mood> <what was done>"
echo "Allowed verbs in imperative mood: add, cut, fix, bump, make, start, stop, refactor, reformat, optimise, document, merge"
echo "Example: add login button"
echo "-"
echo "Your commit message was:"
echo $message
echo "-"
echo "For more information, check script in .husky/commit-msg"
echo "-"
exit 1
fi
Upvotes: 22
Reputation: 1324188
See issue 81
First, check
npm config get ignore-scripts # should be false
Then in a git repo:
npm install husky --save-dev
You can then add hooks (here a pre-commit and pre-push) to npm
(package.json
), the idea being those hook definitions are versions in that package.json
file (part of your git repo sources).
Although, Ron Wertlen comments that npm
scripts for anything beside build/package are an anti-pattern.
See Jerry Zheng's answer for a better practice.
You can also declare existing regular bash hooks (issue 92)
{
"scripts": {
"precommit": "sh scripts/my-specific-hook.sh"
}
}
You can then use validate-commit-msg
to validate your commit message.
add
"commitmsg": "validate-commit-msg"
to your npm scripts inpackage.json
.
Upvotes: 7
Reputation: 23737
This is for commitlint
integration as per the docs:
# Add hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
# or
yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'
Most probably; you may easily customize it with something like this:
yarn husky add .husky/commit-msg 'sh scripts/my-specific-hook.sh $1'
Upvotes: 3
Reputation: 59
Like this:
First, add validate script in your husky config:
// package.json
{
...
"husky": {
"hooks": {
"pre-commit": "npm test",
// if you use validate-commit-msg, this can be "validate-commit-msg"
+ "commit-msg": "sh scripts/my-specific-hook.sh",
....
}
}
}
And then, have a try...
Everything seems to be OK.
Upvotes: 5