Created
March 7, 2013 21:49
-
-
Save ideasasylum/5112128 to your computer and use it in GitHub Desktop.
A pre-commit Git hook for preventing commits containing focus: true, debugger, binding.pry etc Based on a blog post I wrote http://jamie.ideasasylum.com/2013/02/preventing-the-stupid-mistakes-like-committing-focustrue/ (which in turn was based on someone else's script). This version allows you to easily setup new rules at the head of the script
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 | |
spec_hits = [] | |
checks = { | |
'_spec\.rb$' => ['focus:[:space:]*true'], | |
'\.rb$' => ['binding\.pry', 'debugger'] | |
} | |
# 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") | |
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 spec_hits array | |
results.each{ |r| | |
spec_hits.push "#{filename}:" + `grep -n '#{r}' #{filename}`.sub(/:\s+/, ' ').chomp | |
} | |
end | |
end | |
end | |
end | |
end | |
if spec_hits.any? | |
puts "\e[33m>>> Please remove the following problems from these files before committing\e[0m" | |
puts spec_hits.join("\n") | |
end | |
exit 1 if spec_hits.any? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment