我想限制提交的人使用特定的提交消息格式,我该怎么做?
例如:Pair_Name|Story_Number|Commit_Message
最佳答案
有一个 pre-commit-msg
或 commit-msg
Hook ,您可以使用:
Git 存储库带有示例 Hook ,例如git/hooks/commit-msg.sample
下的示例 commit-msg
钩子(Hook)捕获重复的 Signed-off-by 行。
# This example catches duplicate Signed-off-by lines.
test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
要启用一个钩子(Hook),不要忘记让它可执行。
这是一些虚构的例子,它只接受 london|120|something ...
之类的提交消息:
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
# $regex = /\[ref: (\d+)\]/
PAIRS = ["london", "paris", "moscow"] # only these names allowed
STORIES = "\d{2,4}" # story must be a 2, 3 or 4 digit number
MESSAGE = ".{5,}" # message must be at least 5 chars long
$regex = "( (#{PAIRS.join('|')})\|#{STORIES}\|#{MESSAGE} )"
if !$regex.match(message)
puts "[POLICY] Your message is not formatted correctly"
exit 1
end
在使用中:
$ git ci -m "berlin|120"
[POLICY] Your message is not formatted correctly
$ git ci -m "london|120|XX"
[POLICY] Your message is not formatted correctly
$ git ci -m "london|120|Looks good."
[master 853e622] london|120|Looks good.
1 file changed, 1 insertion(+)
关于git - 如何为 git 提交消息设置模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151775/