Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Last active December 10, 2015 21:08
Show Gist options
  • Save marek-saji/4492381 to your computer and use it in GitHub Desktop.
Save marek-saji/4492381 to your computer and use it in GitHub Desktop.
git pre-commit hook checking syntax and for illegal words in PHP files.
#!/bin/bash
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# check PHP files for syntax errors
ERROR=""
git diff --cached --name-only $against | while read FILE
do
if [ "${FILE}" != "${FILE%.php}" ]
then
if ( php -l "${FILE}" 2>&1 > /dev/null )
then
ERROR=".$ERROR"
fi
fi
done
if [ "$ERROR" != "" ]
then
echo "Syntax errors detected, will not commit."
exit 1
fi
# check for illegal words
ERROR=""
#git diff --cached --name-only $against | while read FILE
# Will cause problems with names containing spaces, but the above
# have problems with modyfing $ERROR…
for FILE in $( git diff --cached --name-only $against )
do
if [ "${FILE}" != "${FILE%.php}" ]
then
if ( git diff $against -- "$FILE" | grep '^\+' | grep -P 'TODO|(Zend_Registry::get\(.logger.\)->log|var_dump|die)\(' )
then
ERROR=".$ERROR"
echo "$FILE has illegal words (see above)."
fi
fi
done
if [ "$ERROR" != "" ]
then
echo "Illegal words detected, will not commit."
exit 1
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment