Created
September 1, 2015 15:29
-
-
Save rorymcdaniel/83b1185517bb328cc23e to your computer and use it in GitHub Desktop.
A git precommit hook that looks for things like console.log, var_dump, etc and prevents the commit if found
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
#!/usr/bin/env bash | |
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