-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·57 lines (49 loc) · 1.55 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
#!/usr/bin/env ruby
# Validates that you don't commit forbidden keywords to the repo
# You can skip this checking with 'git commit --no-verify'
exit 0 if ARGV.include?('--no-verify')
# Update this list with your own forbidden keywords
KEYWORDS = [
'binding.pry',
'debugger',
'byebug',
'And pause',
]
def red(text) "\033[31m#{text}\033[0m"; end
def yellow(text) "\033[33m#{text}\033[0m"; end
# list all the files staged for commit
files_changed = %x(git diff --cached --name-only --).split
#FORBIDDEN_BRANCHES = ["staging", "master"]
FORBIDDEN_BRANCHES = []
def current_branch()
branches = `git branch --no-color`.split(/\n/)
current = branches.select{ |b| b =~ /\s*\*/ }.first
current.gsub(/[\*\s]/, "")
end
branch = current_branch
if (FORBIDDEN_BRANCHES.include?(branch))
puts
puts " STOP THE PRESS!"
puts " You are trying to commit on the *#{branch}* branch."
puts " Surely you don't mean that?"
puts
puts " If you really do, force the commit by adding --no-verify to the command."
puts
exit 1
end
# search files for keywords
%x(git grep -q -E "#{KEYWORDS.join('|')}" #{files_changed.join(' ')})
if $?.exitstatus.zero?
puts "# Check following lines:"
files_changed.each do |file|
KEYWORDS.each do |keyword|
%x(git grep -q "#{keyword}" #{file})
if $?.exitstatus.zero?
line = %x(git grep -n "#{keyword}" #{file} | awk -F ":" '{print $2}').split.join(', ')
puts "#\t#{red(file)}:#{line} contains #{yellow keyword}."
end
end
end
puts "Use 'git commit --no-verify' to skip this validation"
exit 1
end