-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommit-hooks.rb
executable file
·63 lines (57 loc) · 1.99 KB
/
commit-hooks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/ruby
#
# Checks for known blacklisted file patterns before allowing a merge.
# Author: rbuckheit
#
SEARCH_PATH = File.expand_path(File.dirname(__FILE__), 'js')
COMMIT_HOOKS = [
{
:name => 'remove console.logs',
:file_pattern => '*.coffee',
:pattern => 'console',
:exception => /window.console &&/,
:explanation => 'remove console.log statements before commit'
},
{
:name => 'no raw javascript files',
:file_pattern => '*.js',
:pattern => '',
:exception => /test-main.js|karma.*.conf.js|KryptnosticClient.js|kryptnostic-umd-exports.js|.*Worker.js/,
:explanation => 'raw js files are not allowed, please use coffeescript'
},
{
:name => 'no TODO comments',
:file_pattern => '*.coffee',
:pattern => 'TODO',
:exception => /authorized/,
:explanation => 'fix TODO comments before committing'
},
{
:name => 'no FIXME comments',
:file_pattern => '*.coffee',
:pattern => 'FIXME',
:exception => /authorized/,
:explanation => 'fix FIXME comments before committing'
},
{
:name => 'no jquery ajax',
:file_pattern => '*.coffee',
:pattern => 'jquery.ajax',
:exception => /authorized/,
:explanation => 'please use axios for ajax calls'
}
]
COMMIT_HOOKS.each do |hook|
command = "grep -r -I '#{hook[:pattern]}' #{SEARCH_PATH} --include='#{hook[:file_pattern]}'"
violations = `#{command}`.split("\n").map(&:chomp).reject{|line| line.match(hook[:exception])}
violations.each do |violation|
$stderr.puts "FAILED pattern '#{hook[:name]}'"
$stderr.puts " " + violation + "\n\n"
$stderr.puts "To fix, you can use an exception pattern: '#{hook[:exception].inspect}' or delete the line."
end
if not violations.empty?
raise 'blacklisted patterns found while checking source. please fix and re-commit'
else
puts "commit hook '#{hook[:name]}' passed"
end
end