Created
August 12, 2014 14:54
-
-
Save jpetitcolas/ce00feaf19d46bfd5691 to your computer and use it in GitHub Desktop.
My standard Git pre-commit hook
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 | |
# | |
# Based on http://nrocco.github.io/2012/04/19/git-pre-commit-hook-for-PHP.html post | |
# | |
# Do not forget to: chmod +x .git/hooks/pre-commit | |
BAD_PHP_WORDS='var_dump|die|todo' | |
BAD_TWIG_WORDS='{{ dump(.*) }}' | |
EXITCODE=0 | |
FILES=`git diff --cached --diff-filter=ACMRTUXB --name-only HEAD --` | |
for FILE in $FILES ; do | |
if [ "${FILE##*.}" = "php" ]; then | |
# Run all php files through php -l and grep for `illegal` words | |
/usr/bin/php -l "$FILE" > /dev/null | |
if [ $? -gt 0 ]; then | |
EXITCODE=1 | |
fi | |
/bin/grep -H -i -n -E "${BAD_PHP_WORDS}" $FILE | |
if [ $? -eq 0 ]; then | |
EXITCODE=1 | |
fi | |
fi | |
if [ "${FILE##*.}" = "twig" ]; then | |
/bin/grep -H -i -n -E "${BAD_TWIG_WORDS}" $FILE | |
if [ $? -eq 0 ]; then | |
EXITCODE=1 | |
fi | |
fi | |
done | |
if [ $EXITCODE -gt 0 ]; then | |
echo | |
echo 'Fix the above erros or use:' | |
echo ' git commit --no-validate' | |
echo | |
fi | |
exit $EXITCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment