Skip to content

Instantly share code, notes, and snippets.

@tigerclaw-az
Last active January 4, 2016 03:29
Show Gist options
  • Save tigerclaw-az/8562495 to your computer and use it in GitHub Desktop.
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
#!/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