how to format commit message for some people in git hook?

时间秒杀一切 提交于 2019-12-19 11:56:22

问题


people should add bug ID in commit message in some format,such as [BUG33] [review leader=...] ... and not every committer must have to follow this formula,i mean scm can write free in commit message. i have searched that commit-msg hook may help to implement it. any one can git me some similar hook examples


回答1:


The book progit has an excellent example of these, both server-side and client-side. You can find a few examples here. One example taken from that link and adapted to your commit message would be:

#!/usr/bin/env ruby

$refname = ARGV[0]
$oldrev  = ARGV[1]
$newrev  = ARGV[2]
$user    = ENV['USER']

puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
$regex = /\[BUG: (\d+)\]/

# enforced custom commit message format
def check_message_format
  missed_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
  missed_revs.each do |rev|
    message = `git cat-file commit #{rev} | sed '1,/^$/d'`
    if !$regex.match(message)
      puts "[POLICY] Your message is not formatted correctly"
      exit 1
    end
  end
end
check_message_format

This should reject any commit whose message isn't formatted with the string "BUG:" followed by a number (presumably from your issue tracking system).



来源:https://stackoverflow.com/questions/11312760/how-to-format-commit-message-for-some-people-in-git-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!