Skip to content

Instantly share code, notes, and snippets.

@Wind4
Created April 15, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wind4/a52e5f74b78643229c00 to your computer and use it in GitHub Desktop.
Save Wind4/a52e5f74b78643229c00 to your computer and use it in GitHub Desktop.
SVNHooks

SVNHook

pre-commit.cmd: Verify that the files being committed have been locked by the committing user. pre-lock.cmd: Prevent stealing locks.

@echo off
setlocal enabledelayedexpansion
:: *******************************************************
:: Get values from action
:: *******************************************************
set REPOS=%1
set TXN=%2
:: *******************************************************
:: If the file doesn't have the correct amount of
:: arguments then go to the end
:: *******************************************************
if DEFINED TXN (
echo.
) else (
echo.
echo This file must be run by the repository!
echo.
pause
goto ERROR_EXIT
)
:: *******************************************************
:: Get the User who is committing
:: *******************************************************
for /f "tokens=*" %%a in ('svnlook author %REPOS% --transaction %TXN%') do set COMMIT_USER=%%a
:: *******************************************************
:: Lock check - Verify that the files being committed
:: have been locked by the committing user.
:: *******************************************************
for /f "tokens=1,*" %%b in ('svnlook changed %REPOS% --transaction %TXN%') do (
set needlock=
:: File contents changed
if "%%b" == "U" set needlock=*
:: File contents and properties changed
if "%%b" == "UU" set needlock=*
if DEFINED needlock (
set locked=
for /f "tokens=1,*" %%f in ('svnlook lock %REPOS% %%c') do (
if "Owner:" == "%%f" (
if "%COMMIT_USER%" == "%%g" (
set locked=*
)
)
)
if DEFINED locked (
echo.
) else (
goto LOCK_ERROR
)
)
)
:: passed
::exit 2
goto NORMAL_EXIT
:: *******************************************************
:: If there's an error display error dialogue
:: *******************************************************
:LOCK_ERROR
echo. 1>&2
echo. 1>&2
echo Please follow these guidlines: 1>&2
echo - Files being updated should be locked. 1>&2
echo - Files being deleted should NOT be locked. 1>&2
echo - Files being added should NOT be locked. 1>&2
echo. 1>&2
echo Please lock / unlock the files according to the guidlines above 1>&2
echo and then try committing again. 1>&2
echo. 1>&2
echo NOTE: DO NOT LOCK FILES THAT ARE BEING ADDED OR DELETED. 1>&2
echo. 1>&2
echo. 1>&2
goto ERROR_EXIT
:: *******************************************************
:: Check doesn't pass, do not allow commit!
:: *******************************************************
:ERROR_EXIT
exit /b 1
:: *******************************************************
:: All checks passed, so allow the commit.
:: *******************************************************
:NORMAL_EXIT
exit 0
@echo off
REM [1] REPOS-PATH (the path to this repository)
REM [2] PATH (the path in the repository about to be locked)
REM [3] USER (the user creating the lock)
REM [4] COMMENT (the comment of the lock)
REM [5] STEAL-LOCK (1 if the user is trying to steal the lock, else 0)
setlocal
::svn对代码资源库路径与文件路径里的右小括号敏感,需要对其转义
::代码资源库路径
set repos=%1
set "repos=%repos:)=^)%"
::当前文件路径
set repPath=%2
set "repPath=%repPath:)=^)%"
set userName=%3
set isSteal=%5
rem NO_STEALING
::如果没有被锁定,则直接跳走结束处理
if /I '1'=='%isSteal%' goto NO_STEALING
REM echo repos = %repos% >>d:\log.txt
REM echo repPath = %repPath% >>d:\log.txt
REM echo userName = %userName% >>d:\log.txt
rem if the path has been locked, find the Owner.
::这里是处理重点
::通过svnlook lock %repos% %repPath%,命令获取锁信息,例如:
:: UUID Token: opaquelocktoken:1707b1a0-8dd1-a94e-87d2-6569a115cd5c
:: Owner: ljz
:: Created: 2011-08-08 21:05:31 +0800 (周一, 08 八月 2011)
:: Expires:
:: Comment (1 line):
::通过findstr /r /n ".",将所有行的前面加上行号,前返回所有行,例如:
:: 1:UUID Token: opaquelocktoken:1707b1a0-8dd1-a94e-87d2-6569a115cd5c
:: 2:Owner: ljz
:: 3:Created: 2011-08-08 21:05:31 +0800 (周一, 08 八月 2011)
:: 4:Expires:
:: 5:Comment (1 line):
::通过tokens=1,2,3 delims=: ,以:号与空格作为分隔符,将上述每一行分隔,并将前三段分别装入变量%%i,%%j,%%k
::通过if %%i == 2 set LockedName=%%k,把第二行分隔后的第三段装入变量LockedName,在这里,就是ljz
for /f "tokens=1,2,3 delims=: " %%i in ('svnlook lock %repos% %repPath% ^|findstr /r /n "."') do (
if %%i == 2 set LockedName=%%k
)
rem If we get no result from svnlook, there's no lock, allow the lock to happen.
::如果没有获取到锁定信息,则直接跳走结束处理
if not defined LockedName goto OK_EXIT
rem If the person locking matches the lock's owner, allow the lock to happen.
rem But this one won't effect, the SVN don't care if the person matchs, they just don't allow relock.
REM echo userName = %userName% >>d:\log.txt
REM echo LockedName = %LockedName% >>d:\log.txt
::如果锁定人与当前用户同名,则直接跳走结束处理
if /I '%LockedName%'=='%userName%' goto OK_EXIT
rem Otherwise, we've got an owner mismatch, so return failure:
:WRONG_PERSON
echo the path has been locked by %LockedName%, Pls contact %LockedName% to unlock it.>&2
goto ERROR_EXIT
:NO_STEALING
echo Stealing lock is not allowed at this server.>&2
:ERROR_EXIT
endlocal
exit 1
:OK_EXIT
endlocal
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment