3 # Copyright (C) The Arvados Authors. All rights reserved.
5 # SPDX-License-Identifier: AGPL-3.0
7 # This script can be installed as a git update hook.
9 # It can also be installed as a gitolite 'hooklet' in the
10 # hooks/common/update.secondary.d/ directory.
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.
21 all_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
22 all_revs.each do |rev|
25 puts "Revision #{b} is blacklisted, you must remove it from your branch (possibly using git rebase) before you can push."
32 blacklist ['26d74dc0524c87c5dcc0c76040ce413a4848b57a']
34 # Only enforce policy on the master branch
35 exit 0 if $refname != 'refs/heads/master'
37 puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
39 $regex = /\[ref: (\d+)\]/
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
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")
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"
68 puts "\n******************************************************************\n"
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"
80 puts "\n******************************************************************\n"
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"
91 puts "\n******************************************************************\n"
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"
106 puts "\n******************************************************************\n"
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"
121 puts "\n******************************************************************\n"