Skip to content

Instantly share code, notes, and snippets.

@yalab
Created May 11, 2012 05:55
Show Gist options
  • Save yalab/2657810 to your computer and use it in GitHub Desktop.
Save yalab/2657810 to your computer and use it in GitHub Desktop.
PHP Syntax check before pre-commit hook of git
#!/bin/sh
PHP="/usr/bin/php"
PWD=`pwd`
SYNTAXCHECK="/usr/bin/php -l"
GIT_ROOT=`pwd`/`git rev-parse --show-cdup`
MODIFIED_FILES=`git diff-index --cached HEAD | awk '{print $6}'`
ERRORS=""
for f in $MODIFIED_FILES;do
FNAME=${GIT_ROOT}${f}
if [ "php" != "${FNAME##*.}" ];then
continue
fi
RESULT=`$SYNTAXCHECK $FNAME`
if [ "$?" != "0" ];then
ERRORS="$ERRORS\n\n$RESULT"
fi
done
if [ -n "$ERRORS" ];then
echo "$ERRORS"
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment