9

我想限制提交的人使用特定的提交消息格式,我该怎么做?

例如:Pair_Name|Story_Number|Commit_Message

4

2 回答 2

11

有一个pre-commit-msgorcommit-msg钩子,你可以使用:

Git repos 带有示例钩子,例如,commit-msg下面的示例钩子会git/hooks/commit-msg.sample捕获重复的 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
}

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


 

这是一些虚构的示例,它只接受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(+)
于 2013-01-04T05:53:27.270 回答
1

注意:这种限制也是gitolite的一部分(一个授权层,允许在推送到 repo 时进行各种检查)

您可以在“ git gitolite (v3) pre-receivehook for all commit messages ”中看到一个示例。

gitolite 的想法是,您可以轻松地为特定的用户组在特定的 repos 上部署该钩子。

于 2013-01-04T07:04:34.687 回答