Ian P. Christian's Personal Blog Random witterings from pookey

14Jun/072

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
Comments (2) Trackbacks (0)
  1. Hey Ian, just wanted to thank you for the code you wrote. I’ve used it as the foundation for the pre-commit that I needed.

    http://www.daevid.com/content/examples/snippets.php


Leave a comment


No trackbacks yet.