我正在使用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