-
-
Save jswright61/68c1f40ad748b0b132c11a3a51f69f8b to your computer and use it in GitHub Desktop.
Git hook to avoid commit debug lines (binding.pry console.log debugger...)
This file contains 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 | |
# modified from https://gist.github.com/wacko/62560b45c1d191859d6b | |
# Added custom phrase, made greps case insensitive | |
# 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 | |
# Does not handle spaces (use dots (.) instead) | |
KEYWORDS = %w(binding.pry console.log debugger JSW.was.here) | |
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 | |
# search files for keywords | |
%x(git grep -q -E -i "#{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 -i #{keyword} #{file}) | |
if $?.exitstatus.zero? | |
line = %x(git grep -n -i #{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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use space on your keywords I think you just need to change the array syntax:
KEYWORDS = ["binding.pry", "console.log", "debugger" "JSW was here"]
and add quotes on the bash commands for the keywords:
%x(git grep -q -i '#{keyword}' #{file})
(line 27)line = %x(git grep -n -i '#{keyword}' #{file} | awk -F ":" '{print $2}').split.join(', ')
(line 30)