-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·87 lines (70 loc) · 2.94 KB
/
pre-commit
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'English'
# NOTE: you may need change file permissions
# chmod +x hooks/pre-commit
exit 0 if ARGV.include?('--no-verify')
warning_keywords = %w[console.log]
keywords = %w[binding.pry console.dir byebug debugger]
files_changed = `git diff-index --name-only HEAD --`.split
# puts '----------------------------------------------------------------------'
# puts remove files changed from the pre-commit checking if they are one of the following files
# puts '----------------------------------------------------------------------'
# files_changed = files_changed - ['hooks/pre-commit']
# files_changed = files_changed - ['hooks/update-version']
# byebug may need to be in these files
files_changed -= ['Gemfile']
files_changed -= ['Gemfile.lock']
files_changed -= ['.gitignore']
files_changed -= ['README.md']
files_changed = files_changed.reject { |f| f.downcase.end_with?('.json') }
files_changed = files_changed.reject { |f| f.downcase.end_with?('.yml') }
# ignore files from specific folders
file_groups = files_changed.select do |item|
item.start_with?('hooks') # ||
# item.start_with?('lib/generators')
end
files_changed -= file_groups
# remove files that are changed because they are deleted
files_changed = files_changed.select { |filename| File.file?(filename) }
# puts '----------------------------------------------------------------------'
# puts 'Files Changed'
# puts '----------------------------------------------------------------------'
# puts files_changed
# puts '----------------------------------------------------------------------'
unless files_changed.empty?
# puts "#{keywords.join('|')}"
# puts "#{files_changed.join(' ')}"
`git grep -q -E "#{warning_keywords.join('|')}" #{files_changed.join(' ')}`
if $CHILD_STATUS.exitstatus.zero?
puts '' # Check following lines:''
puts $CHILD_STATUS.exitstatus
files_changed.each do |file|
warning_keywords.each do |keyword|
# puts "#{keyword} ::: #{file}"
`git grep -q -E #{keyword} #{file}`
if $CHILD_STATUS.exitstatus.zero?
line = `git grep -n #{keyword} #{file} | awk -F ":" '{print $2}'`.split.join(', ')
puts "WARNING:\t\033[31m#{file}\033[0m contains #{keyword} at line \033[33m#{line}\033[0m."
end
end
end
end
`git grep -q -E "#{keywords.join('|')}" #{files_changed.join(' ')}`
if $CHILD_STATUS.exitstatus.zero?
puts '' # Check following lines:''
puts $CHILD_STATUS.exitstatus
files_changed.each do |file|
keywords.each do |keyword|
# puts "#{keyword} ::: #{file}"
`git grep -q -E #{keyword} #{file}`
if $CHILD_STATUS.exitstatus.zero?
line = `git grep -n #{keyword} #{file} | awk -F ":" '{print $2}'`.split.join(', ')
puts "ERROR :\t\033[31m#{file}\033[0m contains #{keyword} at line \033[33m#{line}\033[0m."
end
end
end
puts '# Force commit with --no-verify'
exit 1
end
end