4038: add "fixes #" to acceptable merge commit messages
[arvados.git] / git / hooks / coding-standards.sh
1 #!/usr/bin/env ruby
2
3 # This script can be installed as a git update hook.
4
5 # It can also be installed as a gitolite 'hooklet' in the
6 # hooks/common/update.secondary.d/ directory.
7
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.
10
11 $refname = ARGV[0]
12 $oldrev  = ARGV[1]
13 $newrev  = ARGV[2]
14 $user    = ENV['USER']
15
16 # Only enforce policy on the master branch
17 exit 0 if $refname != 'refs/heads/master'
18
19 puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
20
21 $regex = /\[ref: (\d+)\]/
22
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
28
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")
34   broken = false
35   no_ff = false
36
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"
49       puts message
50       puts "\n******************************************************************\n"
51       puts "\n\n"
52       broken = true
53       no_ff = true
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"
61       puts message
62       puts "\n******************************************************************\n"
63       puts "\n\n"
64       broken = true
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"
72       puts message
73       puts "\n******************************************************************\n"
74       puts "\n\n"
75       broken = true
76     end
77   end
78
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"
87       puts message
88       puts "\n******************************************************************\n"
89       puts "\n\n"
90       broken = true
91     end
92
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"
102       puts message
103       puts "\n******************************************************************\n"
104       puts "\n\n"
105       broken = true
106     end
107   end
108
109   if broken
110     exit 1
111   end
112 end
113
114 check_message_format