如何使用 husky 检查 git commit 消息格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41785974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use husky to check a git commit message format?
提问by arjel
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.
我正在尝试执行 git commit 消息策略以保持我的存储库干净整洁。我看过关于服务器端和客户端钩子的官方文档,然后我碰到了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.
到目前为止,我可以使用第一个但无法设置 husky,我还有很多东西要学。主要思想是能够在新工作站上工作,而无需手动设置任何客户端挂钩。
Could someone explain how I can set up husky to check my commit messages or even make an example?
有人可以解释我如何设置 husky 来检查我的提交消息甚至举个例子吗?
This is my commit-msg hook in project-root/githooks
folder:
这是我在project-root/githooks
文件夹中的commit-msg 钩子:
#!/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:
我试图将脚本添加到 package.json:
"scripts": {
... : ...,
"commitmsg": "sh hooks/commit-msg",
... : ...
}
The hook does not work. All messages pass. If put in .git/hooks it works normally.
钩子不起作用。所有消息都通过。如果放入 .git/hooks 它可以正常工作。
Here's a screenshot of a test project with the package.json, the commit-msg hook and the error it gives out.
这是一个带有 package.json、commit-msg 钩子和它给出的错误的测试项目的屏幕截图。
The same hook, put in .git/hooks folder, works just fine.
放在 .git/hooks 文件夹中的相同钩子工作得很好。
采纳答案by VonC
See issue 81
First, check
首先,检查
npm config get ignore-scripts # should be false
Then in a git repo:
然后在 git 仓库中:
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)
然后,您可以向 npm ( package.json
)添加钩子(这里是预提交和预推送),想法是这些钩子定义是该package.json
文件中的版本(您的 git repo 源的一部分)
You can also declare existing regular bash hooks (issue 92)
您还可以声明现有的常规 bash 钩子(问题 92)
{
"scripts": {
"precommit": "sh scripts/my-specific-hook.sh"
}
}
You can then use validate-commit-msg
to validate your commit message.
然后您可以使用它validate-commit-msg
来验证您的提交消息。
add
"commitmsg": "validate-commit-msg"
to your npm scripts inpackage.json
.
添加
"commitmsg": "validate-commit-msg"
到您的 npm 脚本中package.json
。
回答by Jerry Zheng
Like this:
像这样:
First, add validate script in your husky config:
首先,在您的 husky 配置中添加验证脚本:
// 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.
一切似乎都很好。