how to format commit message for some people in gi

2019-02-21 03:11发布

问题:

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).