SVN post/pre commit hook to check php syntax on windows

917 Views Asked by At

I have recently migrated my SVN server to a windows server. It all went very smoothly - it was too good to be true - so it turned out.

I have/had a pre-commit hook which ran a syntax check on any PHP file committed and rejected the commit with a suitable error message if it failed the check - I will copy this below. Obviously this does not work on Windows, and I have not been able to find an alternative which does. Does anyone out there have anything?

I wouldn't really know where to start converting the below to run on a Windows system, expecially given the number of *nix tools it is reliant on :-S

I have read about pre-commit hooks which use things like Codesniffer to do the PHP checks - is something like that my best/simplest route?

#!/bin/bash
REPOS="$1"
TXN="$2"
PHP="/usr/bin/php"
SVNLOOK=/var/www/UberSVN/ubersvn/bin/svnlook

CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | awk '{print $2}'`
ERRORSA=0
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
    ERRORSA=1
    echo "---------------------------------------------------------------------------------" 1>&2
    echo "${FILE}: $MESSAGE" | sed "s| -| $FILE|g" 1>&2

   fi
  fi
 done
if [ $ERRORSA == 1 ]
then
    echo "---------------------------------------------------------------------------------" 1>&2
    echo "Please correct the errors and try commit again. $ERRORSA" 1>&2
exit 1
fi
exit 0
1

There are 1 best solutions below

0
On

Credit goes to my colleague to whom I passed this little problem on to, and below for any future seekers, is a pre-commit hook which will do a PHP syntax check on Windows hosted SVN servers (also checks the user has entered a commit message). Hopefully someone else will find this useful :)

@echo off  
 :: Stops commits that don't include a log message of at least 6 characters.        


@echo off  

 setlocal enableDelayedExpansion

 rem Subversion sends through the repository path and transaction id  
 set REPOS=%1  
 set TXN=%2  


 svnlook log %REPOS% -t %TXN% | findstr ...... > nul  
 if %errorlevel% gtr 0 (goto err) else (goto cont)

 :err  
 echo --------------------------------------------------------------------------- 1>&2   
 echo Your commit has been blocked because it didn't include a log message. 1>&2  
 echo Do the commit again, this time with a log message that describes your changes. 1>&2
 echo --------------------------------------------------------------------------- 1>&2  
 exit 1  

 :cont

svnlook changed %REPOS% -t %TXN% |findstr /I /R "\.php$ \.phtml$" >lint.txt
for /F "tokens=2* delims= " %%i in (lint.txt) do (
    set fname=%%i %%j
    for /l %%a in (1,1,31) do if "!fname:~-1!"==" " set fname=!fname:~0,-1!
    svnlook cat %REPOS% -t %TXN% "!fname!" | D:\PHP\php -l | findstr /I /B /V "No syntax errors" 1>&2
    if !errorlevel! neq 1 (
        echo in "%%i %%j" 1>&2
        echo. 1>&2
        echo --------------------------------------------------------------------------- 1>&2
        exit 1
    )
)
del lint.txt