Skip to content

Instantly share code, notes, and snippets.

@lenlorijn
Last active August 29, 2015 14:23
Show Gist options
  • Save lenlorijn/58c55417f89b886b0496 to your computer and use it in GitHub Desktop.
Save lenlorijn/58c55417f89b886b0496 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Based on http://nrocco.github.io/2012/04/19/git-pre-commit-hook-for-PHP.html post
# and https://gist.github.com/jpetitcolas/ce00feaf19d46bfd5691
#
# Do not forget to: chmod +x .git/hooks/pre-commit
BAD_PHP_WORDS='var_dump|die|exit|ini_set|extract|__halt_compiler|eval(|print_r'
BAD_JS_WORDS='console.log'
BAD_TWIG_WORDS='{{ dump(.*) }}'
EXITCODE=0
FILES=`git diff --cached --diff-filter=ACMRTUXB --name-only HEAD --`
for FILE in $FILES ; do
if [ "${FILE:9:4}" = "core" ]; then
echo "CORE FILE EDITED!"
echo $FILE
EXITCODE=1
fi
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##*.}" = "phtml" ]; 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
/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
if [ "${FILE##*.}" = "js" ]; then
/bin/grep -H -i -n -E "${BAD_JS_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