Reputation: 2153
How do I use husky to check for a particular git commit message pattern? Whenever the git commit -m "message"
command is invoked, I want to parse the message. How could I do this? How could I receive the git commit message and initiate the parsing?
Currently, my package.json has husky included:
"husky": {
"hooks": {
"commit-msg": "./shell-scripts/commit-msg-hook.sh && commitlint -E HUSKY_GIT_PARAMS"
}
},
But I am not sure what to do in shell script file. How do I receive the commit message and then parse it?
Here is the shell script, that I am trying. It is very similar to what the git documentation has
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
echo message
$regex = /([#) #([0-9])* ([A-Z])\w+/
if !$regex.match(message)
puts "Incorrect format"
exit 1
end
Upvotes: 1
Views: 2264
Reputation: 160
From https://www.atlassian.com/git/tutorials/git-hooks
The only argument passed to this hook is the name of the file that contains > the message.
I am not a ruby expert but working on a shell script I ran into a similar problem where my $1
was not giving me the message as I would hope so but rather a path to a file. so doing
commit_message="$(cat "$1")"
gets me the actual message to then validate.
Upvotes: 1