Oct 23, 2008

How to Disable Steal Lock in Subversion

In Subversion, you can easily steal the lock that other user lock it. By using hook script, you can disallow user to do so. Just edit the pre-lock script as following

pre-lock.bat
The following is the content of pre-lock.bat which will work on Windows environment. Just copy the content and put it under the hook folder of your Subversion repository

@ECHO OFF
:: Set all parameters. Even though most are not used, in case you want to add
:: changes that allow, for example, editing of the author or addition of log messages.
set repository=%1
set rev_path=%2
set userName=%3

:: If a lock exists and is owned by a different person, don't allow it
:: to be stolen (e.g., with 'svn lock --force ...').

FOR /F "delims=: tokens=1*" %%a IN ('svnlook lock "%repository%" "%rev_path%"') DO if %%a==Owner (set LOCK_OWNER=%%b)

:: If we get no result from svnlook, there's no lock, allow the lock to
:: happen:
if "%LOCK_OWNER%"=="" (
exit /b 0
)

:: If the person locking matches the lock's owner, allow the lock to
:: happen:
if "%LOCK_OWNER%" = " %username%" (
exit /b 0
)

:: Otherwise, we've got an owner mismatch, so return failure:
echo "Error: %rev_path% already locked by %LOCK_OWNER%." >&2
exit /b 1

9 comments:

ashwani said...

It's a gr8 script.
It partly solved my problem.But thanks for the person who made this script.
Well,How can i edit this script to allow stealing/breaking for named administrator only.

Please help me
Thanks In Advance
Ashwani Singh
Senior Software Engineer

Hieu Le Trung said...

You can search for svn_owner name in the configuration file with following lines

FOR /F "delims== tokens=1*" %%a IN (%repository%\..\Permission\authz-svn.conf) DO if "%%a"=="svn_owner " ( set SVN_OWNER=%%b )

Or if you have the administrator name already, put it into the following if

if "%LOCK_OWNER%" = " %username%" (
exit /b 0
)

Hope this help

ashwani said...

Hi Hieu,Thanks for suggestion

AdminName = ashwanisingh

I have written the below script:-

@ECHO OFF
:: Set all parameters. Even though most are not used, in case you want to add
:: changes that allow, for example, editing of the author or addition of log messages.
set repository=%1
set rev_path=%2
set userName=%3

:: If a lock exists and is owned by a different person, don't allow it
:: to be stolen (e.g., with 'svn lock --force ...').

FOR /F "delims=: tokens=1*" %%a IN ('svnlook lock "%repository%" "%rev_path%"') DO if %%a==Owner (set LOCK_OWNER=%%b)

:: If we get no result from svnlook, there's no lock, allow the lock to
:: happen:
if "%LOCK_OWNER%"=="" (
exit /b 0
)

:: If the person locking matches the lock's owner, allow the lock to
:: happen:
if "%LOCK_OWNER%" = " %ashwanisingh%" (
exit /b 0
)


:: Otherwise, we've got an owner mismatch, so return failure:
echo "Error: %rev_path% already locked by %LOCK_OWNER%." >&2
exit /b 1



This is not working.
Means the administrator doesn't able to steal the lock by another user which should happen.

Hieu Le Trung said...

You can add following lines (there is space before ashwanisingh)
if "%LOCK_OWNER%" = " ashwanisingh" (
exit /b 0
)

After the line
if "%LOCK_OWNER%" = " %username%" (
exit /b 0
)

ashwani said...

Thanks Hieu for ur suggestions.But the problem is still there.The administrator(ashwanisingh) still not able to steal the lock of another user.it gives the error 'Lock Blocked by pre-lock hook(exit code 255)with output:=was unexpected at this time'



I have ritten the script as below mentioned:-


@ECHO OFF
:: Set all parameters. Even though most are not used, in case you want to add
:: changes that allow, for example, editing of the author or addition of log messages.
set repository=%1
set rev_path=%2
set userName=%3

:: If a lock exists and is owned by a different person, don't allow it
:: to be stolen (e.g., with 'svn lock --force ...').

FOR /F "delims=: tokens=1*" %%a IN ('svnlook lock "%repository%" "%rev_path%"') DO if %%a==Owner (set LOCK_OWNER=%%b)

:: If we get no result from svnlook, there's no lock, allow the lock to
:: happen:
if "%LOCK_OWNER%"=="" (
exit /b 0
)

:: If the person locking matches the lock's owner, allow the lock to
:: happen:

if "%LOCK_OWNER%" = "userName" (
exit /b 0
)
if "%LOCK_OWNER%" = " ashwanisingh" (
exit /b 0
)
:: Otherwise, we've got an owner mismatch, so return failure:
echo "Error: %rev_path% already locked by %LOCK_OWNER%." >&2
exit /b 1


Plz help me.

Hieu Le Trung said...

Oh I'm sorry, I've give a wrong one. It should be
if "%LOCK_OWNER%" = " %userName%" (
exit /b 0
)
if "%userName%" = "ashwanisingh" (
exit /b 0
)


The first if is to check for the lock owner is current user or not
The second if is to check for the user invoke the command is `admin` or not

ashwani said...

Hi Trung,

I have ritten the script described below:-
But still it is not working.

@ECHO OFF
:: Set all parameters. Even though most are not used, in case you want to add
:: changes that allow, for example, editing of the author or addition of log messages.
set repository=%1
set rev_path=%2
set userName=%3

:: If a lock exists and is owned by a different person, don't allow it
:: to be stolen (e.g., with 'svn lock --force ...').

FOR /F "delims=: tokens=1*" %%a IN ('svnlook lock "%repository%" "%rev_path%"') DO if %%a==Owner (set LOCK_OWNER=%%b)


:: If we get no result from svnlook, there's no lock, allow the lock to
:: happen:
if "%LOCK_OWNER%"=="" (
exit /b 0
)

:: If the person locking matches the lock's owner, allow the lock to
:: happen:

if "%LOCK_OWNER%" = " %userName%" (
exit /b 0
)


What will i do?
Plz help me
Thanks In Advance

Unknown said...

This is an old thread, but I just came across this link.

Here the solution for Ashwani's problem is to check if %username% matches %AdminUser% and let the lock be set.

ksamy said...

Use the following script to avoid steal lock.


@ECHO OFF
:: Set all parameters. Even though most are not used, in case you want to add
:: changes that allow, for example, editing of the author or addition of log messages.
set repository=%1
set rev_path=%2
set userName=%3
set steal=%5

:: If a lock exists and is owned by a different person, don't allow it
:: to be stolen (e.g., with 'svn lock --force ...').



FOR /F "delims=: tokens=1*" %%a IN ('svnlook lock "%repository%" "%rev_path%"') DO if %%a==Owner (set LOCK_OWNER=%%b)

:: If we get no result from svnlook, there's no lock, allow the lock to
:: happen:
if "%LOCK_OWNER%"=="" (
exit /b 0
)

:: Check wheather the user attempt to steal or not

if "%steal%"=="1" (
echo "Error: Steal lock is prohibited !!!." >&2
exit /b 1
)




:: If the person locking matches the lock's owner, allow the lock to
:: happen:
if "%LOCK_OWNER%" == " %username%" (
exit /b 0
)