Skip to content

Instantly share code, notes, and snippets.

@jgillman
Forked from alexbevi/pre-commit.sh
Last active August 29, 2015 14:01
Show Gist options
  • Save jgillman/aaa3c0639cac4c7cdea5 to your computer and use it in GitHub Desktop.
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
#!/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