Skip to content

Instantly share code, notes, and snippets.

@freshtonic
Last active December 4, 2021 10:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freshtonic/8531434 to your computer and use it in GitHub Desktop.
Save freshtonic/8531434 to your computer and use it in GitHub Desktop.
Git commit hook to abort commits when I accidentally commit dumb shit.
#!/usr/bin/env ruby
# Checks for things that I often commit accidentally and bails out of the
# commit. To skip this pre-commit hook use `git commit --no-verify`.
checks = [
/\bddescribe\b/,
/\biit\b/,
/\bxit\b/,
/binding.pry/,
/debugger/
]
files_modified = `git diff-index --diff-filter=ACMRTUXB --cached --name-only HEAD`.split("\n")
files_modified.each do |file_name|
content = `git show :"#{file_name}"` # read file from cache (i.e. what will be commited)
checks.each do |check|
if content =~ check
STDERR.puts "#{file_name} matches pre-commit check #{check}. Aborting commit"
exit 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment