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

前端 未结 1 431
故里飘歌
故里飘歌 2021-01-16 16:51

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 wri

相关标签:
1条回答
  • 2021-01-16 17:45

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

    0 讨论(0)
提交回复
热议问题