Coding standards hook, initial commit.
[arvados-dev.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 -- 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'
19
20 puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
21
22 $regex = /\[ref: (\d+)\]/
23
24 $broken_commit_message = /Please enter a commit message to explain why this merge is necessary/
25
26 $merge_commit = /Merge branch/
27 $merge_master_commit = /Merge branch 'master' (of|into)/
28 $refs_or_closes_found = /(refs #|closes #)/i
29
30 # If the next command has output, this is a non-merge commit.
31 #`git rev-list --max-parents=1 --first-parent #{$oldrev}..#{$newrev}`
32
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"
43                         puts message
44                         puts "\n******************************************************************\n"
45                         puts "\n\n"
46       exit 1
47     end
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"
55                         puts message
56                         puts "\n******************************************************************\n"
57                         puts "\n\n"
58       exit 1
59                 end
60   end
61 end
62
63 check_message_format
64