Extract SVN revision number, revision range, branch name from a given directory
This posts presents a set of commands to determine the SVN revision number, revision range and branchname of a current directory.
The info can then be used for automatic software/document versioning.
So far the code is given for DOS/Windows. It can easily be adapted for linux.
The latest version of the code can be found on the following Github repository.
The latest version of the code can be found on the following Github repository.
Getting the branch name
The following is the content of a batch file called '_GetBranchName.bat'.
rem Script: _GetBranchName.bat
rem This scripts echoes the branch name of a folder within a repository
rem A directory can be given as argument, otherwise, the directory FROM WHICH the command is run is used.
@echo off
rem --- Extracting branch name
for /f "tokens=*" %%i in ('svn info %1 ^| find "Relative URL"') do set SBRANCHES=%%i
set BRANCH=%SBRANCHES:~25%
rem --- Check if trunk (not pretty)
for /f "tokens=*" %%i in ('svn info %1 ^| find "Relative URL" ^| find "trunk"') do set STRUNK=%%i
if NOT "%STRUNK%"=="" (
set BRANCH=trunk
)
Getting the revision range
The revision range is relevant to determine the state of the svn repository (in particular if within a branch) The following is the content of a batch file called '_GetRevisionRange.bat'.
rem Script: _GetRevisionRange.bat
rem This scripts echo the Revision range of a folder within a repository
rem A directory can be given as argument, otherwise, the directory FROM WHICH the command is run is used.
@echo off
rem --- Getting Svn version range
FOR /F %%i IN ('svnversion -n %1') DO SET "REVISION_RANGE=%%i"
echo %REVISION_RANGE%
Getting the revision number
The following is the content of a batch file called '_GetRevisionNumber.bat'. It requires to have the batch script 'max.bat' next to it (code given below)
rem Script: _GetRevisionNumber.bat
rem This scripts echoes the Revision number of a folder within a repository
rem A directory can be given as argument, otherwise, the directory FROM WHICH the command is run is used.
@echo off
rem The path of the current script (_GetRevisonNumber.bat)
set SCRIPT_DIR=%~dp0
set MAX_PRG=%SCRIPT_DIR%max
rem --- REVISION_NUMBER
rem Extracting Last changed revision for all files in directory and appending to a list
set LAST_REV_LIST=
for /f "tokens=4" %%i in ('svn info -R %1 ^| find "Last Changed Rev"') do (
call set "LAST_REV_LIST=%%LAST_REV_LIST%% %%i"
)
rem Getting Maximum revision number in directory
for /f "delims=" %%i in ('%MAX_PRG% "%LAST_REV_LIST%"') do set "REVISION_NUMBER=%%i"
echo %REVISION_NUMBER%
rem Sript: max.bat
@echo off
setlocal enabledelayedexpansion
set a=.%~1
if "%a%" equ "." set /p a="Input stream: "
call :max res %a%
echo %res%
endlocal
goto :eof
:max
set %1=%2
:loop
shift /2
if "%2" equ "" goto :eof
if %2 gtr !%1! set res=%2
goto loop
Summary
A summary scripts using the scripts above is given below. The batch script accepts a directory as argument.
rem Script: _MakeVersion.bat
@echo off
rem --- Parsing arguments
IF "%1"=="" (
set WORK_DIR=%cd%
) else (
set WORK_DIR=%1
)
rem --- Handling directories
set SCRIPT_PATH=%~dp0
set ORIGIN_DIR=%cd%
cd %WORK_DIR%
rem --- Getting Svn Info
call %SCRIPT_PATH%_GetRevisionNumber.bat 1>NUL
call %SCRIPT_PATH%_GetRevisionRange.bat 1>NUL
call %SCRIPT_PATH%_GetBranchName.bat 1>NUL
echo Directoty : %WORK_DIR%
echo Branch : %BRANCH%
echo Last Changed : %REVISION_NUMBER%
echo Revision range: %REVISION_RANGE%
cd %ORIGIN_DIR%