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
17 exit 0 if $refname != 'refs/heads/master'
19 puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
21 $regex = /\[ref: (\d+)\]/
23 $broken_commit_message = /Please enter a commit message to explain why this merge is necessary/
24 $wrong_way_merge_master = /Merge( remote-tracking)? branch '([^\/]+\/)?master' into/
25 $merge_master = /Merge branch '[^']+'((?! into)| into master)/
26 $pull_merge = /Merge branch 'master' of /
27 $refs_or_closes_or_no_issue = /(refs #|closes #|fixes #|no issue #)/i
29 # enforced custom commit message format
30 def check_message_format
31 all_revs = `git rev-list --first-parent #{$oldrev}..#{$newrev}`.split("\n")
32 merge_revs = `git rev-list --first-parent --min-parents=2 #{$oldrev}..#{$newrev}`.split("\n")
33 # single_revs = `git rev-list --first-parent --max-parents=1 #{$oldrev}..#{$newrev}`.split("\n")
37 merge_revs.each do |rev|
38 message = `git cat-file commit #{rev} | sed '1,/^$/d'`
39 if $wrong_way_merge_master.match(message)
40 puts "\n[POLICY] Only non-fast-forward merges into master are allowed. Please"
41 puts "reset your master branch:"
42 puts " git reset --hard origin/master"
43 puts "and then merge your branch with the --no-ff option:"
44 puts " git merge your-branch --no-ff\n"
45 puts "Remember to add a reference to an issue number in the merge commit!\n"
46 puts "\n******************************************************************\n"
47 puts "\nOffending commit: #{rev}\n"
48 puts "\nOffending commit message:\n"
50 puts "\n******************************************************************\n"
54 elsif $pull_merge.match(message)
55 puts "\n[POLICY] This appears to be a git pull merge of remote master into local"
56 puts "master. In order to maintain a linear first-parent history of master,"
57 puts "please reset your branch and remerge or rebase using the latest master.\n"
58 puts "\n******************************************************************\n"
59 puts "\nOffending commit: #{rev}\n"
60 puts "\nOffending commit message:\n\n"
62 puts "\n******************************************************************\n"
65 elsif not $merge_master.match(message) and not
66 puts "\n[POLICY] This does not appear to be a merge of a feature"
67 puts "branch into master. Merges must follow the format"
68 puts "\"Merge branch 'feature-branch'\".\n"
69 puts "\n******************************************************************\n"
70 puts "\nOffending commit: #{rev}\n"
71 puts "\nOffending commit message:\n\n"
73 puts "\n******************************************************************\n"
79 all_revs.each do |rev|
80 message = `git cat-file commit #{rev} | sed '1,/^$/d'`
81 if $broken_commit_message.match(message)
82 puts "\n[POLICY] Rejected broken commit message for including boilerplate"
83 puts "instruction text.\n"
84 puts "\n******************************************************************\n"
85 puts "\nOffending commit: #{rev}\n"
86 puts "\nOffending commit message:\n\n"
88 puts "\n******************************************************************\n"
93 # Do not test when the commit is a no_ff merge (which will be rejected), because
94 # this test will complain about *every* commit in the merge otherwise, obscuring
95 # the real reason for the rejection (the no_ff merge)
96 if not no_ff and not $refs_or_closes_or_no_issue.match(message)
97 puts "\n[POLICY] All commits to master must include an issue using \"refs #\" or"
98 puts "\"closes #\", or specify \"no issue #\"\n"
99 puts "\n******************************************************************\n"
100 puts "\nOffending commit: #{rev}\n"
101 puts "\nOffending commit message:\n\n"
103 puts "\n******************************************************************\n"