2014-11-03 72 views
0

我寫了一個在Windows上運行的SVN的預先提交鉤子腳本(.bat)。我的SVN鉤子腳本有什麼問題----- pre-commit.bat?

的主要目的是:

  1. 檢查反轉日誌長度;
  2. 防止正常用戶刪除[repo]/trunk/文件夾;
  3. 防止正常用戶刪除[repo]/trunk/xxx/文件夾;

但SVN總是說:

command syntax not correct (exitcode 255) 

的代碼是在這裏:

@echo off 
:: Stops commits that have empty log messages. 
@echo off 

setlocal 

set REPOS=%1 
set TXN=%2 

Rem Check log length. 
svnlook log -t "%TXN%" "%REPOS%" | findstr ".........." > nul 
if %errorlevel% gtr 0 goto NoLog 

@echo off 
set Drop=No 

Rem Check Delete operation on trunk 
svnlook changed -t "%TXN%" "%Repos%" | findstr "^D[ ]*trunk//$" 
if %ERRORLEVEL% == 0 (set Drop=Yes) 

Rem Check Delete operation on subdirectory of Trunk 
svnlook changed -t "%TXN%" "%Repos%" | findstr "^D[ ]*trunk/[.]*//$" 
if %ERRORLEVEL% == 0 (set Drop=Yes) 

if Drop == Yes 
(goto DropTrunk) 
else 
(exit 0) 

:NoLog 
echo You must inpu reversion log, and not less than 10 characters! 1>&2 
exit 1 

:DropTrunk 
echo Only admin can delete the trunk directory and its subdirectory! 1>&2 
exit 1 
+2

'如果Drop == Yes'永遠不會是真的。也許你的意思是'%Drop%'? – 2014-11-03 02:06:34

回答

1

也許你只需要告訴你的鉤子腳本到哪裏尋找svnlook命令。來自SVN的書:

默認情況下,Subversion執行一個空的環境的鉤子腳本 - 也就是說,根本沒有設置環境變量,甚至沒有$ PATH(或在Windows下的%PATH%)。正因爲如此,許多管理員在他們的鉤子程序手動運行時都很困惑,但在被Subversion調用時卻不起作用。過去,管理員通過手動設置腳本本身需要的所有環境變量來解決此問題。

來源:http://svnbook.red-bean.com/en/1.8/svn.reposadmin.create.html#svn.reposadmin.hooks.configuration

1

錯誤的核心問題是if Drop == Yes查詢的語法。命令解釋程序不知道在下一行繼續行if Drop == Yes,除非出現左括號。

所以語句必須閱讀:

if Drop == Yes (
    goto DropTrunk 
) else (
    exit 0 
) 

或爲單行:

if Drop == Yes (goto DropTrunk) else (exit 0) 

類型if /?在命令提示符窗口中看到正確的if/else語法。

但還有一個問題:if Drop == Yes比較文字字符串DropYes,這當然不會是真的。爲了比較變量的值Drop你需要把它改成這樣:

if %Drop% == Yes (
    goto DropTrunk 
) else (
    exit 0 
) 

或:

if %Drop% == Yes (goto DropTrunk) else (exit 0) 

對於這樣的比較字符串,我會把周圍的字符串引號避免使用某些特殊字符或帶空串的麻煩,並且我還會使用/I開關進行不區分大小寫的比較:

if /I "%Drop%"=="Yes" (
    goto DropTrunk 
) else (
    exit 0 
) 

或:

if /I "%Drop%"=="Yes" (goto DropTrunk) else (exit 0) 

無論如何,我會做整個方法不同,如下所示:

@echo off 
set "Drop=" 

Rem Check Delete operation on trunk 
svnlook changed -t "%TXN%" "%Repos%" | findstr "^D[ ]*trunk//$" > nul 
if %ErrorLevel% EQU 0 (set "Drop=Yes") 

Rem Check Delete operation on subdirectory of Trunk 
svnlook changed -t "%TXN%" "%Repos%" | findstr "^D[ ]*trunk/[.]*//$" > nul 
if %ErrorLevel% EQU 0 (set "Drop=Yes") 

if defined Drop (
    goto DropTrunk 
) else (
    exit 0 
) 

正如你所看到的,我避免了只檢查是否字符串比較或者不定義變量Drop,我最初將其清除,而不是將其設置爲No

此外,我將ErrorLevel檢查的比較運算符從==(字符串比較)更改爲EQU(數值比較)。