Getting log message from svnlook via windows batch

5.8k views Asked by At

I try to prepare a post-commit hook for my svn repository. Therefore i need the log message from the last commit which I get with the command svnlook log -r %REV% %REPOS%. Filling the snippet with the appropriate params I get the following multline log message:

This
is
my
transaction.

This works well so far. Now I put this in a .bat file:

@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%

When I execute the script only the last line transaction. is echoed. The for-loop is a snippet from which I thought, it would read the svnlook output into %var%.

My approach is to get the log message in a variable, which I pass to another exe-file as parameter. But it won't work. I don't know how to properly use the loop.

The log message should given to another exe-file as an parameter.

I modified the script to the following (@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!

The output is now This is my transaction. But the linebreak are gone but I need the for further processing.

3

There are 3 answers

3
jeb On BEST ANSWER

As you want also the linebreaks, you can add them when concatenating the lines.

@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!
4
PA. On

If I understand your question correctly, you need to concatenate the output of the svnlook command into a single variable (something like VAR = VAR & %%i)

In BAT you access the contents of a variable by writing the variable wrapped with % signs. And you concatenate by just sticking them together. SET X=%A%. SET Y=%A%%B%. So in your case you should change the SET assignment to something like SET VAR=%VAR% %%i.

However, this would not work. As the assignment is inside a FOR loop, it needs to be reevaluated every iteration. You need to Enable Delayed Expansion. Read HELP SET for more information.

Something similar to this,

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

without delims, some words will be missing

here is my pre-commit script, that i just finished writing

if you set debug to 1, it will start failing all the time, and you ll get to see messages with some var output

@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