Skip to content

Instantly share code, notes, and snippets.

@bryanstedman
Created April 25, 2016 18:41
Show Gist options
  • Save bryanstedman/41617962c6229f56d4d3afb86da2ce7b to your computer and use it in GitHub Desktop.
Save bryanstedman/41617962c6229f56d4d3afb86da2ce7b to your computer and use it in GitHub Desktop.
Pre-Commit Hook Checking for Debug code
#!/bin/sh
#
# Pre commit hook to look for missed debug code
DEBUG_REGEX="console\.(debug|info|log|warn)|%pre=|\.to_yaml"
# Add extensions to check here
EXTENSIONS_REGEX="(.html$|.haml$|.erb$|.js$)"
ERRORS_BUFFER=""
TEXT_DEFAULT="\\033[0;39m"
TEXT_INFO="\\033[1;32m"
TEXT_ERROR="\\033[1;31m"
TEXT_UNDERLINE="\\0033[4m"
TEXT_BOLD="\\0033[1m"
FILES=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
echo "\\033[1;33m""Debug checker - pre-commit hook" "$TEXT_DEFAULT"
echo
for FILE in $FILES; do
if [[ $FILE =~ $EXTENSIONS_REGEX ]]; then
echo "$TEXT_INFO""Checking file: $FILE" "$TEXT_DEFAULT"
ERRORS=$(grep -sEnH $DEBUG_REGEX $FILE)
if [ "$ERRORS" != "" ]; then
ERRORS_BUFFER="$ERRORS"
ERRORS="$TEXT_ERROR Errors found in $TEXT_BOLD$FILE$TEXT_DEFAULT\n $ERRORS\n"
echo "$ERRORS"
else
echo "$TEXT_INFO No errors found in $TEXT_BOLD$FILE$TEXT_DEFAULT\n"
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo "$TEXT_ERROR" "There were errors or warnings, commit aborted." "$TEXT_DEFAULT"
echo "$TEXT_INFO" "If you are sure you want to commit those files, use --no-verify option" "$TEXT_DEFAULT"
exit 1
else
echo "$TEXT_INFO" "All files are clean." "$TEXT_DEFAULT"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment