如何为git commit消息设置模式?

时间:2013-01-04 05:50:08

标签: git

我想限制那些提交具有特定提交消息格式的人,我该怎么做?

例如:Pair_Name|Story_Number|Commit_Message

2 个答案:

答案 0 :(得分:11)

您可以使用pre-commit-msgcommit-msg挂钩:

Git repos带有样品钩,例如commit-msg下的示例git/hooks/commit-msg.sample挂钩会捕获重复的签名离线。

# 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
}

要启用挂钩,请不要忘记使其可执行。


这是一些虚构的例子,它只接受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(+)

答案 1 :(得分:1)

注意:此类限制也是gitolite的一部分(authorization layer允许在推送到回购时进行所有类型的检查)

您可以在" git gitolite (v3) pre-receive hook for all commit messages"。

中看到一个示例

使用gitolite的想法是,您可以轻松地将特定存储库中的挂钩部署到特定的用户组。

相关问题