Skip to content

Instantly share code, notes, and snippets.

@mbobin
Created July 5, 2016 14:38
Show Gist options
  • Select an option

  • Save mbobin/7bd00aaffe5085830127a56ef7ec1926 to your computer and use it in GitHub Desktop.

Select an option

Save mbobin/7bd00aaffe5085830127a56ef7ec1926 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
hits = []
checks = {
#'_spec\.rb$' => ['focus:[:space:]*true'],
'\.rb$' => ['binding\.pry', 'debugger', 'byebug', 'console\.log'],
'\.erb$' => ['binding\.pry', 'debugger', 'byebug', 'console\.log'],
'\.js$' => ['binding\.pry', 'debugger', 'byebug', 'console\.log'],
'\.coffee$' => ['binding\.pry', 'debugger', 'byebug', 'console\.log'],
'\.jsx$' => ['binding\.pry', 'debugger', 'byebug', 'console\.log'],
'\.rake$' => ['binding\.pry', 'debugger', 'byebug'],
'\.yml$' => ['binding\.pry', 'debugger', 'byebug']
}
blacklisted_exts = [ '\.log$', '\.rdb$' ]
# Find the names of all the filenames that have been (A)dded (C)opied or (M)odified
filenames = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
# Ensure there are no special files committed
filenames.each do |filename|
blacklisted_exts.each do |ext|
hits.push filename if filename.match(ext)
end
end
filenames.each do |filename|
# Perform special checks for _spec filenames (rspec tests)
checks.each do |filename_pattern, patterns|
if filename.match filename_pattern
patterns.each do |contents_pattern|
results = `git diff --cached #{filename} | grep "^\+[^+]" | grep "#{contents_pattern}"`.split("\n").map { |r| r.sub(/^\+[\s\t]*/, '') }
if $? == 0
# Add the relevant change with line number to the hits array
results.each{ |r|
hits.push "#{filename}:" + `grep -n '#{r}' #{filename}`.sub(/:\s+/, ' ').chomp
}
end
end
end
end
end
if hits.any?
puts "\e[33m>>> Please remove the following problems from these files before committing\e[0m"
puts hits.join("\n")
end
exit 1 if hits.any?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment