Merge branch '11332-arv-cwl-integration-testing'
[arvados-dev.git] / git / hooks / coding-standards.sh
1 #!/usr/bin/env ruby
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: AGPL-3.0
6
7 # This script can be installed as a git update hook.
8
9 # It can also be installed as a gitolite 'hooklet' in the
10 # hooks/common/update.secondary.d/ directory.
11
12 # NOTE: this script runs under the same assumptions as the 'update' hook, so
13 # the starting directory must be maintained and arguments must be passed on.
14
15 $refname = ARGV[0]
16 $oldrev  = ARGV[1]
17 $newrev  = ARGV[2]
18 $user    = ENV['USER']
19
20 def blacklist bl
21   all_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
22   all_revs.each do |rev|
23     bl.each do |b|
24       if rev == b
25         puts "Revision #{b} is blacklisted, you must remove it from your branch (possibly using git rebase) before you can push."
26         exit 1
27       end
28     end
29   end
30 end
31
32 blacklist ['26d74dc0524c87c5dcc0c76040ce413a4848b57a']
33
34 # Only enforce policy on the master branch
35 exit 0 if $refname != 'refs/heads/master'
36
37 puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
38
39 $regex = /\[ref: (\d+)\]/
40
41 $broken_commit_message = /Please enter a commit message to explain why this merge is necessary/
42 $wrong_way_merge_master = /Merge( remote-tracking)? branch '([^\/]+\/)?master' into/
43 $merge_master = /Merge branch '[^']+'((?! into)| into master)/
44 $pull_merge = /Merge branch 'master' of /
45 $refs_or_closes_or_no_issue = /(refs #|closes #|fixes #|no issue #)/i
46
47 # enforced custom commit message format
48 def check_message_format
49   all_revs    = `git rev-list --first-parent #{$oldrev}..#{$newrev}`.split("\n")
50   merge_revs  = `git rev-list --first-parent --min-parents=2 #{$oldrev}..#{$newrev}`.split("\n")
51   # single_revs = `git rev-list --first-parent --max-parents=1 #{$oldrev}..#{$newrev}`.split("\n")
52   broken = false
53   no_ff = false
54
55   merge_revs.each do |rev|
56     message = `git cat-file commit #{rev} | sed '1,/^$/d'`
57     if $wrong_way_merge_master.match(message)
58       puts "\n[POLICY] Only non-fast-forward merges into master are allowed. Please"
59       puts "reset your master branch:"
60       puts "  git reset --hard origin/master"
61       puts "and then merge your branch with the --no-ff option:"
62       puts "  git merge your-branch --no-ff\n"
63       puts "Remember to add a reference to an issue number in the merge commit!\n"
64       puts "\n******************************************************************\n"
65       puts "\nOffending commit: #{rev}\n"
66       puts "\nOffending commit message:\n"
67       puts message
68       puts "\n******************************************************************\n"
69       puts "\n\n"
70       broken = true
71       no_ff = true
72     elsif $pull_merge.match(message)
73       puts "\n[POLICY] This appears to be a git pull merge of remote master into local"
74       puts "master.  In order to maintain a linear first-parent history of master,"
75       puts "please reset your branch and remerge or rebase using the latest master.\n"
76       puts "\n******************************************************************\n"
77       puts "\nOffending commit: #{rev}\n"
78       puts "\nOffending commit message:\n\n"
79       puts message
80       puts "\n******************************************************************\n"
81       puts "\n\n"
82       broken = true
83     elsif not $merge_master.match(message) and not
84       puts "\n[POLICY] This does not appear to be a merge of a feature"
85       puts "branch into master.  Merges must follow the format"
86       puts "\"Merge branch 'feature-branch'\".\n"
87       puts "\n******************************************************************\n"
88       puts "\nOffending commit: #{rev}\n"
89       puts "\nOffending commit message:\n\n"
90       puts message
91       puts "\n******************************************************************\n"
92       puts "\n\n"
93       broken = true
94     end
95   end
96
97   all_revs.each do |rev|
98     message = `git cat-file commit #{rev} | sed '1,/^$/d'`
99     if $broken_commit_message.match(message)
100       puts "\n[POLICY] Rejected broken commit message for including boilerplate"
101       puts "instruction text.\n"
102       puts "\n******************************************************************\n"
103       puts "\nOffending commit: #{rev}\n"
104       puts "\nOffending commit message:\n\n"
105       puts message
106       puts "\n******************************************************************\n"
107       puts "\n\n"
108       broken = true
109     end
110
111     # Do not test when the commit is a no_ff merge (which will be rejected), because
112     # this test will complain about *every* commit in the merge otherwise, obscuring
113     # the real reason for the rejection (the no_ff merge)
114     if not no_ff and not $refs_or_closes_or_no_issue.match(message)
115       puts "\n[POLICY] All commits to master must include an issue using \"refs #\" or"
116       puts "\"closes #\", or specify \"no issue #\"\n"
117       puts "\n******************************************************************\n"
118       puts "\nOffending commit: #{rev}\n"
119       puts "\nOffending commit message:\n\n"
120       puts message
121       puts "\n******************************************************************\n"
122       puts "\n\n"
123       broken = true
124     end
125   end
126
127   if broken
128     exit 1
129   end
130 end
131
132 check_message_format