Last active
December 4, 2021 10:33
-
-
Save freshtonic/8531434 to your computer and use it in GitHub Desktop.
Git commit hook to abort commits when I accidentally commit dumb shit.
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 | |
# 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