3

我尝试为我的 svn 存储库准备一个提交后挂钩。因此,我需要使用命令获得的最后一次提交的日志消息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.。for 循环是我认为的一个片段,它会将 svnlook 输出读入%var%.

我的方法是在一个变量中获取日志消息,我将其作为参数传递给另一个 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.但是换行符消失了,但我需要进一步处理。

4

3 回答 3

1

由于您还需要换行符,您可以在连接行时添加它们。

@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!
于 2012-04-30T11:04:04.217 回答
1

如果我正确理解您的问题,您需要将 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!
于 2012-04-30T10:09:59.550 回答
0

没有分隔符,有些词会丢失

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

如果您将 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 e.g. 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
于 2012-05-04T21:24:25.203 回答