Last active
July 1, 2019 02:29
-
-
Save datchley/6234265 to your computer and use it in GitHub Desktop.
Git pre-commit hook to make sure I don't leave console/debug statements in my code.
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 | |
# | |
# @file pre-commit | |
# @author David Atchley, <david.atchley@answers.com> | |
# @date Wed Dec 18 14:02:39 CST 2013 | |
# | |
# Pre Commit checks for various syntax and cleaning prio | |
# to committing. Exits with non-zero to stop commit | |
# | |
#---------------------------------------------------------------------- | |
# Handle any options via environment | |
if [ "$GIT_HOOK_PRECOMMIT_IGNORE" = "yes" ]; then | |
exit 0; | |
fi | |
# pluralize a word based on a count | |
s() { | |
local word=$1 | |
local count=$2 | |
if [ $count -eq 1 ]; then | |
echo $word | |
else | |
if echo "$word" | egrep "([sxz]|[cs]h)$" 2>&1 >/dev/null ; then | |
echo $word"es"; | |
elif echo "$word" | egrep "y$" 2>&1 >/dev/null ; then | |
echo $word"ies"; | |
else | |
echo $word"s" | |
fi | |
fi | |
} | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
# Redirect output to stderr. | |
exec 1>&2 | |
# track number of pre-commit errors found | |
errors=0 | |
echo "> Pre Commit Hook running" | |
echo "> You can by-pass this hook by running: export GIT_HOOK_PRECOMMIT_IGNORE=yes" | |
for file in `git diff-index --name-status $against -- | cut -c3-` | |
do | |
# Javascript pre-commit checks | |
if echo "$file" | egrep '\.(p?html|js)$' 2>&1 >/dev/null | |
then | |
# Make sure no 'console.*' calls are in any HTML/JS files | |
if egrep "(debugger;|console\.)" $file | egrep -v "(\/\/|\/\*|#).*(debugger;|console\.)" 2>&1 >/dev/null | |
then | |
echo "==> warning: $file contains 'console' or 'debugger' calls!!"; | |
errors=$((errors+1)) | |
fi | |
fi | |
# PHP pre-commit checks | |
if echo "$file" | egrep '\.php$' 2>&1 >/dev/null | |
then | |
# Make sure no 'console.*' calls are in any HTML/JS files | |
if egrep "util::debug" $file | egrep -v "(\/\/|\/\*|#).*util::debug" 2>&1 >/dev/null | |
then | |
echo "==> warning: $file contains 'util::debug' calls!!"; | |
errors=$((errors+1)) | |
fi | |
fi | |
done | |
if [ $errors -gt 0 ]; then | |
echo "=== fail. $errors "$(s 'error' $errors)" found" | |
exit 1 | |
else | |
echo "=== ok." | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment