Last active
January 4, 2016 03:29
-
-
Save tigerclaw-az/8562495 to your computer and use it in GitHub Desktop.
pre-commit hook for Git that looks for certain patterns and rejects the commit if found
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 -w | |
FORBIDDEN = [ | |
/(?<!\/\/\s)console\.log/, | |
/(?<!\/\/\s)console\.debug/, | |
/(?<!\/\/\s)echo print_r/, | |
/\bdo not commit\b/i | |
] | |
full_diff = `git diff --cached --` | |
full_diff.scan(%r{^\+\+\+ b/(.+)\n@@.*\n([\s\S]*?)(?:^diff|\z)}).each do |file, diff| | |
added = diff.split("\n").select { |x| x.start_with?("+") }.join("\n") | |
if FORBIDDEN.any? { |re| added.match(re) } | |
puts %{Please remove "#{$1 || $&}" statement from #{file} and retry commit} | |
exit 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment