git - 如何为 git 提交消息设置模式?

标签 git

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

例如:Pair_Name|Story_Number|Commit_Message

最佳答案

有一个 pre-commit-msgcommit-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/

相关文章:

git - 致命的 : remote heroku already exists

linux - 从 Git 获取单个文件版本

git - 使用git describe使Git在分支上显示正确的标签

git - 可以在一台机器上使用 GitHub 和 GitLab 吗?

git - 仅发布某些 git 分支

git - 如何在具有更改历史记录的存储库上应用 git 补丁?

Java 感知 merge 命令

git - 我如何向非程序员解释源代码控制(特别是 Mercurial/Tortoise)?

ruby-on-rails-3 - 从 Rails 应用程序中获取当前的 Git 提交版本?

java - eclipse egit 工作目录 vs jenkins 和 git 方式