SVN post / pre commit hook检查windows上的php语法

时间:2014-05-12 10:15:33

标签: php svn ubersvn

我最近将SVN服务器迁移到了Windows服务器。这一切都非常顺利 - 真是太好了 - 所以事实证明了。

我有一个预提交挂钩,它对提交的任何PHP文件运行语法检查,如果检查失败,则拒绝提交带有合适的错误消息 - 我将在下面复制。显然这不适用于Windows,我无法找到替代方案。有没有人有什么?

我真的不知道从哪里开始将以下内容转换为在Windows系统上运行,特别是考虑到它依赖的* nix工具的数量:-S

我已经阅读过使用像Codesniffer这样的事情进行PHP检查的预提交钩子 - 这是我最好/最简单的路径吗?

#!/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 个答案:

答案 0 :(得分:0)

归功于我的同事,我将这个小问题传递给了我,以及未来寻求者的下方,是一个预提交钩子,它将对Windows托管的SVN服务器进行PHP语法检查(也检查用户是否输入提交消息)。希望其他人会觉得这很有用:)

@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