Subversion pre-commit hook for PHP

mars 8th, 2010
by Axel

If you are using Subversion for your development projects, you may know about hooks: these are scripts that are executed on the server at different times. For instance you may want to check that a commit message is not empty before allowing the commit. In this case, this is a pre-commit hook.

In my case, I decided to add a constraint: check whether my PHP codes contain errors or not. Here’s the « pre-commit » script:

#!/bin/bash

REPOS="$1"
TXN="$2"

SVNLOOK=`which svnlook`
PHP=`which php`

$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null
if [ $? -ne 0 ] ; then
  echo "ERROR: any commit must have a message">/dev/stderr
  exit 1
fi

for FILE in `$SVNLOOK changed -t "$TXN" "$REPOS" | cut -b 5-`
do
   if [ "${FILE: -4}" == '.php' ]; then

       $SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | $PHP -l

       if [ $? != 0 ]; then
           echo "ERROR: PHP errors detected in $FILE" > /dev/stderr
           exit 1
       fi
   fi
done

exit 0

Thanks Gwenael for the PHP check portion adapted to my needs.

PS: this script requires « php-cli »

Tags: , , ,
Posted in Development | Comments (0)

No comments yet

Leave a Reply