3 # This script can be installed as a git update hook.
5 # It can also be installed as a gitolite 'hooklet' in the
6 # hooks/common/update.secondary.d/ directory.
8 # NOTE: this script runs under the same assumptions as the 'update' hook, so
9 # the starting directory must be maintained and arguments must be passed on.
16 # Only enforce policy on the master branch -- on second thought, do it on all
17 # branches so that people don't get surprised at merge-to-master time.
18 #exit 0 if $refname != 'refs/heads/master'
20 puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
22 $regex = /\[ref: (\d+)\]/
24 $broken_commit_message = /Please enter a commit message to explain why this merge is necessary/
26 $merge_commit = /Merge branch/
27 $merge_master_commit = /Merge branch 'master' (of|into)/
28 $refs_or_closes_found = /(refs #|closes #)/i
30 # If the next command has output, this is a non-merge commit.
31 #`git rev-list --max-parents=1 --first-parent #{$oldrev}..#{$newrev}`
33 # enforced custom commit message format
34 def check_message_format
35 missed_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
36 missed_revs.each do |rev|
37 message = `git cat-file commit #{rev} | sed '1,/^$/d'`
38 if $broken_commit_message.match(message)
39 puts "\n[POLICY] Please avoid broken commit messages, rejected\n\n"
40 puts "\n******************************************************************\n"
41 puts "\nOffending commit: #{rev}\n\n"
42 puts "\nOffending commit message:\n\n"
44 puts "\n******************************************************************\n"
48 if $merge_commit.match(message) and
49 not $merge_master_commit.match(message) and
50 not $refs_or_closes_found.match(message)
51 puts "\n[POLICY] Please make sure to refer to an issue in all branch merge commits, rejected\n\n"
52 puts "\n******************************************************************\n"
53 puts "\nOffending commit: #{rev}\n\n"
54 puts "\nOffending commit message:\n\n"
56 puts "\n******************************************************************\n"