Skip to content

Instantly share code, notes, and snippets.

@jpetitcolas
Created August 12, 2014 14:54
Show Gist options
  • Save jpetitcolas/ce00feaf19d46bfd5691 to your computer and use it in GitHub Desktop.
Save jpetitcolas/ce00feaf19d46bfd5691 to your computer and use it in GitHub Desktop.
My standard Git pre-commit hook
#!/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