我正在使用node.js项目,我需要使用各种应用程序,如github,sourcetree等来检出git源代码。是否可以定制验证git commit消息并在提交更改时在所有应用程序中显示错误消息?如何验证并显示git commit消息的错误?
我知道在git中有一个git hook'commit-msg',但我不知道如何使用它。
我正在使用node.js项目,我需要使用各种应用程序,如github,sourcetree等来检出git源代码。是否可以定制验证git commit消息并在提交更改时在所有应用程序中显示错误消息?如何验证并显示git commit消息的错误?
我知道在git中有一个git hook'commit-msg',但我不知道如何使用它。
正如你所说你在一个Nodejs项目,我假设你有一个package.json
。我建议你看看ghooks,validate-commit-msg,commitizen和conventional-changelog。
我知道有很多的链接在这里,但所有这些一起工作的here's an example以下AngularJS' commit convention:
package.json
:
"devDependencies": {
…
"commitizen": "2.8.1",
"cz-conventional-changelog": "1.1.6",
"ghooks": "1.3.2",
"validate-commit-msg": "2.6.1",
…
}
…
"config": {
"ghooks": {
"commit-msg": "validate-commit-msg"
},
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
}
…
下面是一个示例。
#!/bin/sh
path=$1
echo path is $path
a=$(cat $path)
echo commit message is
echo $a
if [[ "$a" =~ "hello world" ]];then
echo commit format test passed
exit 0
else
echo commit format test failed
exit 1
fi
将它保存为一个名为commit-msg
并使其可执行文件并把它放到git的/钩子文件/。
此示例检查提交消息是否有子字符串“hello world”。如果是这样,承诺就会成功。如果没有,提交将失败。
下面是一个Python版本
#!/usr/bin/python
import sys
path = sys.argv[1]
print "path is " + path
with open(path) as f:
lines = f.read()
print "commit message is"
print lines
if "hello world" in lines:
print "format test passed"
exit(0)
else:
print "format test failed"
exit(1)
你可以提高这个钩子用你的逻辑。你可以检查.git/hooks /是否有commit-msg.sample
。如果是这样,你可以把它作为参考。你可以只需cp .git/hooks/commit-msg.smaple .git/hooks/commit-msg
然后编辑它。如果你想在每个repo中部署这个钩子,你可以将这个钩子复制到/ usr/share/git-core/templates/hooks中,如果你使用的是Ubuntu。我不知道其他系统中的默认模板路径是什么。您可能需要检查。这样做后,当你git clone
,这个钩子将被复制到.git/hook /中。至于已有的回购,您可以运行git init
来复制挂钩。
还有一件事,如果你不想勾跑,你可以添加选项--no-verify
或只是-n
时git commit
,这也绕过钩子pre-commit
如果它存在。
你能在你想要的详细说明发生提交时发生? –
我需要验证提交消息并在提交消息不满足所需格式时显示错误。 –
'commit-msg'取一个参数,它是包含提交消息的文件的路径。所以你可以解析文件的内容并检查它是否被格式化。 – ElpieKay