Skip to content

Instantly share code, notes, and snippets.

@pironim
Created October 1, 2014 13:37
Show Gist options
  • Save pironim/a4d43e0aba7a780dd02b to your computer and use it in GitHub Desktop.
Save pironim/a4d43e0aba7a780dd02b to your computer and use it in GitHub Desktop.
git pre-commit hook with debugger
# details here http://blog.kaygee79.com/git-pre-commit-hook-to-find-common-debug-functions-such-as-console-log-var_dump-and-print_r/
TEXT_DEFAULT="\\033[0;39m"
TEXT_INFO="\\033[1;32m"
TEXT_ERROR="\\033[1;31m"
TEXT_YELLOW="\\033[1;33m"
echo -e "$TEXT_YELLOW""[ >>> PRE-COMMIT START ]""$TEXT_DEFAULT"
FILES_PATTERN='(\..+)?$'
FORBIDDEN="console\.(debug|info|log|warn)\([^)]*\)|var_dump\([^)]*\)|print_r\([^)]*\)|<<<<<<<|=======|>>>>>>>"
if git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
xargs grep -E --with-filename -n $FORBIDDEN | \
grep -v '//';
then
echo -e "$TEXT_ERROR[ >>> COMMIT REJECTED ]" &&
echo -e "$TEXT_ERROR""If you absolutely need to commit this use git commit --no-verify"
exit 1;
fi
echo -e "$TEXT_YELLOW""[ >>> PRE-COMMIT DONE ]""$TEXT_DEFAULT"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment