Automatically syntax checking of PHP files when checking to SVN

This precommit script will prevent people from checking in syntactically invalid PHP files to a subversion repositroy

#!/bin/bash
REPOS="$1"
TXN="$2"
PHP="/usr/lib/php5/bin/php"
SVNLOOK=/usr/bin/svnlook

CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | awk '{print $2}'`
for LINE in $CHANGED
  do
  FILE=`echo $LINE | egrep \.php$`
  if [ $? == 0 ]
  then
   MESSAGE=`$SVNLOOK cat -t "$TXN" "$REPOS" "${FILE}" | $PHP -l`
   if [ $? -ne 0 ]
   then
    echo " " 1>&2
    echo "---------------------------------------------------------------------------------" 1>&2
    echo "During automatic PHP syntax checking we found an error in the following PHP file: " 1>&2
    echo " " 1>&2
    echo "${FILE}: $MESSAGE" | sed "s| -| $FILE|g" 1>&2
    echo " " 1>&2
    echo "Please correct the error and try commit again." 1>&2
    echo " " 1>&2
    echo "You can check for syntax error on your computer by running command: php -l ${FILE}" 1>&2
    echo " " 1>&2
    echo "Best regards," 1>&2
    echo "Doctrine Dev Team" 1>&2
    echo "---------------------------------------------------------------------------------" 1>&2
    exit 1
   fi
  fi
 done

7 thoughts on “Automatically syntax checking of PHP files when checking to SVN”

  1. Thank you! You Rock You Rule! 🙂 I got to your page after I had an accidently php file on a live server that had a parser syntax fault on it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.