Last active
August 29, 2015 14:04
-
-
Save meagar/280bf86f04f09df0f202 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# This is a Git pre-commit hook. | |
# | |
# Reject commits that contain any of the following strings: | |
# # NO COMMIT | // NO COMMIT | #NOCOMMIT | //NOCOMMIT | etc | |
# debugger | |
# binding.pry | |
# console.log | |
# | |
# If you attempt to commit any of these strings, you will get a pretty error telling you | |
# where the offence occured, and that you can use `git commit -n` to skip this check, in | |
# the case that you want to forcibly add (for example) a console.log | |
# | |
# INSTALL: | |
# - Save this file as <project_path>/.git/hooks/pre-commit | |
# - Make sure you have Ruby installed | |
# | |
# USAGE: | |
# Forget this script exists until you accidentally stage a "bidning.pry" and are | |
# prevented from committing your changes by it | |
# | |
TESTS = [ | |
'(#|//) ?NO ?COMMIT', | |
'\bdebugger\b', | |
'\bbinding.pry\b', | |
'\bconsole\.log\b', | |
] | |
num_violations = 0 | |
lines = `git diff --cached`.split("\n") | |
files = [] | |
bad_files = [] | |
file = "" | |
file_outputted = false | |
lines.each do |line| | |
if line[/^(\+\+\+) b\//] | |
file = line | |
files << file | |
file['+++ b/'] = '' | |
file_outputted = false | |
next | |
end | |
TESTS.each do |test| | |
if match = line[/^\+.*#{test}/] | |
bad_files << file | |
unless file_outputted | |
puts "\n#{file}" | |
file_outputted = true | |
end | |
puts "\t#{line.gsub(/#{test}/, "\e[0;37;41m\\0\e[0m")}" | |
num_violations += 1 | |
end | |
end | |
end | |
bad_files.uniq! | |
if num_violations > 0 | |
puts "\n#{num_violations} violations found in #{bad_files.length} of #{files.length} files" | |
puts | |
puts "Use \e[1;37m git commit -n \e[0m to bypass this pre-commit hook" | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install manual?