【问题标题】:How do I set a pattern for git commit messages?如何为 git 提交消息设置模式?
【发布时间】:2013-01-04 05:50:08
【问题描述】:

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

例如:Pair_Name|Story_Number|Commit_Message

【问题讨论】:

    标签: git


    【解决方案1】:

    有一个 pre-commit-msgcommit-msg 钩子,您可以使用:

    Git 存储库带有示例钩子,例如git/hooks/commit-msg.sample 下的示例 commit-msg 挂钩捕获重复的 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(+)
    

    【讨论】:

      【解决方案2】:

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

      您可以在“git gitolite (v3) pre-receive hook for all commit messages”查看一个示例。

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

      【讨论】:

        猜你喜欢
        • 2022-12-06
        • 1970-01-01
        • 2019-08-20
        • 2016-02-01
        • 2011-01-18
        • 1970-01-01
        • 2019-02-14
        • 2014-11-13
        • 2015-10-30
        相关资源
        最近更新 更多