-
-
Save jgillman/aaa3c0639cac4c7cdea5 to your computer and use it in GitHub Desktop.
Git pre-commit hook to check all staged Ruby (*.rb/haml/js/coffee) files for debug statements
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
#!/bin/sh | |
# | |
# Git pre-commit hook to check all staged Ruby (*.rb/haml/coffee) files | |
# for Pry binding references | |
# | |
# Installation | |
# | |
# cd .git/hooks | |
# ln -s relative/path/to/pre-commit.sh | |
# | |
# Based on | |
# | |
# http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/ | |
# http://mark-story.com/posts/view/using-git-commit-hooks-to-prevent-stupid-mistakes | |
# https://gist.github.com/3266940 | |
readonly FILES_PATTERN='\.(rb|haml|coffee|js)(\..+)?$' | |
readonly FORBIDDEN_PATTERN='binding.pry\|debugger' | |
has_files_to_check() { | |
git diff --cached --name-only \ | |
| grep --extended-regexp $FILES_PATTERN \ | |
&> /dev/null | |
} | |
has_bad_files() { | |
git diff --cached --name-only \ | |
| grep --extended-regexp $FILES_PATTERN \ | |
| xargs grep --regexp=$FORBIDDEN_PATTERN \ | |
&> /dev/null | |
} | |
print_offending_files() { | |
git diff --cached --name-only \ | |
| grep --extended-regexp $FILES_PATTERN \ | |
| GREP_COLOR='37;41' xargs grep --color --with-filename --line-number --regexp=$FORBIDDEN_PATTERN | |
} | |
print_rejected_reason() { | |
echo "\nCommit rejected. The above files have disallow statements." | |
echo "If you need to commit them, you may use: git commit --no-verify" | |
} | |
main() { | |
if $(has_files_to_check) | |
then | |
if $(has_bad_files) | |
then | |
print_offending_files | |
print_rejected_reason | |
exit 1 | |
else | |
# No offending files, continue | |
exit 0 | |
fi | |
else | |
# No files to check, continue | |
exit 0 | |
fi | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment