通过Windows批处理从svn​​look获取日志消息

我尝试为我的svn仓库准备一个post-commit钩子。 因此,我需要使用命令svnlook log -r %REV% %REPOS%得到的最后一次提交的日志消息。 用适当的参数填充片段我得到以下多行日志消息:

 This is my transaction. 

迄今为止,这工作得很好。 现在我把它放在一个.bat文件中:

 @ECHO OFF REM just for testing purpose... SET REPOS=C:\repo SET REV=40 FOR /F %%i in ('svnlook log -r %REV% %REPOS%') do SET VAR=%%i ECHO %VAR% 

当我只执行最后一行transaction.的脚本transaction. 是呼应。 for循环是我认为的一个片段,它会将svnlook输出读入%var%

我的方法是获取日志消息在一个variables,我传递给另一个exe文件作为参数。 但它不会工作。 我不知道如何正确使用循环。

日志消息应该给另一个exe文件作为参数。

我将脚本修改为以下(@thx PA。)

 @ECHO OFF setlocal enabledelayedexpansion SET REPOS=C:\repo SET REV=40 SET MSG= FOR /F %%i in ('svnlook log -r %REV% %REPOS%') do SET VAR=!VAR! %%i ECHO !VAR! 

输出是现在This is my transaction. 但换行不见了,但我需要进一步处理。

正如你想要的linebreaks,你可以添加他们时,连接线。

 @ECHO OFF setlocal enabledelayedexpansion set LF=^ rem ** The two empty lines are NECESSARY SET REPOS=C:\Users\CH.ROSESOFT\Downloads\t3\repo SET REV=40 SET MSG= FOR /F %%i in ('svnlook log -r %REV% %REPOS%') do ( SET "VAR=!VAR!!LF!%%i" SET "PAR=!PAR!^^!LF!!LF!%%i" ) ECHO !VAR! myProgram.exe !par! 

如果我正确理解你的问题,你需要将svnlook命令的输出连接成一个变量(类似于VAR = VAR&%% i)

在BAT中,通过写入包含%符号的变量来访问变量的内容。 然后你把它们粘在一起 SET X=%A%SET Y=%A%%B% 。 所以在你的情况下,你应该改变SET赋值为SET VAR=%VAR% %%i

但是,这是行不通的。 由于赋值在FOR循环中,所以每次迭代都需要重新进行赋值。 您需要启用延迟扩展 。 阅读HELP SET了解更多信息。

类似的东西,

 @ECHO OFF setlocal enabledelayedexpansion ... SET VAR= FOR /F %%i in ('solook log -r %REV% %REPOS%') do SET VAR=!VAR! %%i ECHO !VAR! 

没有界限,有些话将会丢失

这是我的预先提交脚本,我刚写完

如果将debug设置为1,则它将始终开始失败,并且您将看到具有某个var输出的消息

 @echo off set DEBUG=0 :::::::::::::::::::::::::: Dont touch this part ::::::::::::: setlocal enabledelayedexpansion set space= set LF=^ rem ** The two empty lines are NECESSARY :::::::::::::::::::::::::: Dont touch above ::::::::::::: set REPOS=%1 set TXN=%2 ::: Get the author :::::::::::::::::::::::::::::::::: set author= For /F %%I in ('svnlook author %REPOS% -t %TXN%') Do Set author=!author!%%I ::: Get the message :::::::::::::::::::::::::::::::::: set message= For /F "delims=" %%I in ('svnlook log %1 -t %2') Do Set message=!message!%%I%space% :: Make sure that author is not blank or guest - if readonly accounts are enabled and something goes wrong echo %author% | FindStr [a-zA-Z0-9] >nul IF %ERRORLEVEL% NEQ 0 GOTO AUTHOR_NOT_OK echo %author% | FindStr \C:guest >nul IF %ERRORLEVEL% EQU 0 GOTO AUTHOR_NOT_OK :: %ERRORLEVEL% = 1 at this point means its no error! :: Make sure that the log message contains some text. echo "%message%" | FindStr [a-zA-Z0-9] >nul IF %ERRORLEVEL% NEQ 0 GOTO COMMENT_NOT_OK :: Make sure that the log message contains ACR echo "%message%" | FindStr /I /C:acr >nul IF %ERRORLEVEL% NEQ 0 GOTO COMMENT_NOT_OK GOTO OK :COMMENT_NOT_OK echo COMMENT_NOT_OK 1>&2 echo "D:\SYSAPPS\HATS\ci\repositories\repo\java\hooks\pre-commit.bat 1>&2 echo Your commit has been blocked because you didn't provide adequate log message 1>&2 echo Please write a log message describing the purpose of your changes and 1>&2 echo then try committing again. -- Thank you 1>&2 echo eg 1>&2 echo ACR: 12345 1>&2 echo fixed blah blah blah 1>&2 if %DEBUG% EQU 1 GOTO EXITDEBUG exit 1 :AUTHOR_NOT_OK echo AUTHOR_NOT_OK 1>&2 :OK if %DEBUG% EQU 1 GOTO EXITDEBUG exit 0 :EXITDEBUG echo == EXITDEBUG == 1>&2 echo ---- %AUTHOR% %message% ---- 1>&2 echo txn %TXN% repos %REPOS% 1>&2 echo changedpath 1>&2 svnlook changed %REPOS% -t %TXN% 1>&2 echo ERRORLEVEL %ERRORLEVEL% 1>&2 echo "D:\SYSAPPS\HATS\ci\repositories\repo\java\hooks\pre-commit.bat" 1>&2 exit 1