如何为git commit消息设置模式? [英] How do I set a pattern for git commit messages?

查看:113
本文介绍了如何为git commit消息设置模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如: Pair_Name | Story_Number | Commit_Message

解决方案

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



Git repos带有示例钩子,例如 git / hooks / commit-msg.sample 下的示例 commit-msg 钩子可以捕获重复的Signed-off-by行。

 #这个例子捕获重复的Signed-off-by行。 

test'=$(grep'^ Signed-off-by:'$ 1|
sort | uniq -c | sed -e'/ ^ [] * 1 [ ] / d')|| {
echo>& 2重复签名线外。
exit 1
}

要启用挂钩,请不要忘记让它成为可执行文件。








这里有一个虚构的例子,它只接受 london | 120 |东西...... 之类的提交信息:

 #!/ usr / bin / env ruby​​ 
message_file = ARGV [0]
message = File.read(message_file)

#$ regex = / \ [ref:(\ d +)\] /

PAIRS = [london,paris,moscow]#只允许这些名称
STORIES =\d {2,4}#故事必须是2,3或4位数字
MESSAGE =。{5,}#消息必须至少5个字符长

$ regex =((#{PAIRS.join('|')})\ |#{STORIES} \ |#{MESSAGE})

如果!$ regex.match(message)
puts[POLICY]你的消息格式不正确
exit 1
end

在美国ge:
$ b

  $ git ci -mberlin | 120
[POLICY]您的邮件格式不正确
$ git ci -mlondon | 120 | XX
[POLICY]您的邮件格式不正确
$ git ci -mlondon | 120 |好。
[master 853e622] london | 120 |看起来不错。
1文件已更改,1个插入(+)


I want to restrict the people whoever commits to have a specific commit message format, how do I do that?

For example: Pair_Name|Story_Number|Commit_Message

解决方案

There is a pre-commit-msg or commit-msg hook, that you could use:

Git repos come with sample hooks, e.g. the sample commit-msg hook under git/hooks/commit-msg.sample catches duplicate Signed-off-by lines.

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

To enable a hook, don't forget to make it executable.


 

Here's some fictional example, which would only accept commit messages of the london|120|something ... and the like:

#!/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

In usage:

$ 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 commit消息设置模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆