Automatic lock-modify-unlock

From SubversionWiki
Revision as of 16:01, 19 February 2009 by Aaron.stover (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Different versions of binary files cannot be merged. Therefore versioning of binary files should follow the lock-modify-unlock model[1]. This setup uses the following three measures

  • forces users to use property svn:needs-lock on newly added binary files. Denies commits when the property is not available
  • sets the svn:needs-lock property on all already existing binary files in repositories
  • configures users to automatically set property svn:needs-lock on newly added binary files


1) - create a pre-commit.cmd script in the repository\hooks directory. This script verifies that property svn:needs-lock is set on binary files and denies the commit if the property is not available (Windows only):

@echo off
set REPOS=%1
set TRANSACTION=%2
set SVNLOOK="c:\Program Files\Subversion\apache2.2\bin\svnlook.exe"
set TEMP=c:\temp

if exist %TEMP%\tempfile%2 del %TEMP%\tempfile%2
for /f "tokens=1,2 usebackq" %%i in (`%SVNLOOK% changed -t %2 %1`) do @if %%i==A @echo %%j >> %TEMP%\tempfile%2
if not exist %TEMP%\tempfile%2 goto NOFILESADDED
for /f "usebackq" %%i in (`findstr /E /I /R "\.bmp.$ \.gif.$ \.ico.$ \.jpeg.$ \.jpg.$ \.png.$ \.tif.$ \.tiff.$ \.doc.$ \.jar.$ \.odt.$ \.pdf.$ \.ppt.$ \.swf.$ \.vsd.$ \.xls.$ \.zip.$" %TEMP%\tempfile%2`) do (
%SVNLOOK% propget -t %2 %1 svn:needs-lock %%i 1> nul 2> nul
if ERRORLEVEL 1 (
echo commit denied, binary files must have property svn:needs-lock >&2
type %TEMP%\tempfile%2 >&2
del %TEMP%\tempfile%2
EXIT /B 1
)
)
del %TEMP%\tempfile%2
:NOFILESADDED
EXIT /B 0

As an alternative, this modification to the script above handles long filenames and by default is setup to work with a VisualSVN Server installation.

set REPOS=%1
set TRANSACTION=%2
set SVNLOOK=c:\Progra~1\Visual~1\bin\svnlook.exe
set TEMP=c:\Progra~1\Visual~1

if exist %TEMP%\tempfile%2 del %TEMP%\tempfile%2

REM Identify all property updates and added files in current transaction and print them to tempfile
for /f "tokens=1,* usebackq" %%i in (`%SVNLOOK% changed -t %2 %1`) do @if %%i==A @echo %%j>> %TEMP%\tempfile%2
for /f "tokens=1,* usebackq" %%i in (`%SVNLOOK% changed -t %2 %1`) do @if %%i==_U @echo %%j>> %TEMP%\tempfile%2

REM If no property updates or file additions occurred go to the end of the script
if not exist %TEMP%\tempfile%2 goto NOFILESADDED

REM For each file with these extensions listed in the tempfile check that it has the needs-lock property set
for /f "tokens=* usebackq" %%i in (`findstr /E /I /R "\.bmp \.gif \.ico \.jpeg \.jpg \.png \.tif \.tiff \.doc \.jar \.odt \.pdf \.ppt \.swf \.vsd \.xls \.zip" %TEMP%\tempfile%2`) do (
REM echo "%SVNLOOK% propget -t %2 %1 svn:needs-lock "%%i" 1> nul 2> nul" 1>&2
%SVNLOOK% propget -t %2 %1 svn:needs-lock "%%i" 1>&2

REM If the property wasn't set
if ERRORLEVEL 1 (
REM Display a helpful error message to the user
echo commit denied, binary files must have property svn:needs-lock >&2
type %TEMP%\tempfile%2 >&2
del %TEMP%\tempfile%2
EXIT /B 1
)
)

del %TEMP%\tempfile%2
:NOFILESADDED
EXIT /B 0

2) Recursively set svn:needs-lock property on binaries

If you need to apply svn:needs-lock on already existing binaries in a repository, do the following on a client (not on the svn server): - checkout a repository - add to following line to a cmd script (Windows only):

FOR /R c:\full\path\to\repository %%v in (*.bmp *.gif *.ico *.jpeg *.jpg *.png *.tif *.tiff *.doc *.jar *.odc *.odf *.odg *.odi *.odp *.ods *.odt *.pdf *.ppt *.ser *.swf *.vsd *.xls *.zip) do svn propset svn:needs-lock yes %%~fv

- run the script

3) Configure users to automatically use svn:needs-lock property on new binary files

New binary files should have the svn:needs-lock property set, this is verified by the script of step 1. This can be achieved automatically if users configure their svn client config file.

- under windows the SVN config file is "C:\Documents and Settings\[USER_NAME]\Application Data\Subversion\config"

Replace or merge the [miscellany] and [auto-props] sections in the svn config file with the following:

[miscellany]
enable-auto-props = yes

[auto-props]
### The format of the entries is:
###   file-name-pattern = propname[=value][;propname[=value]...]
### The file-name-pattern can contain wildcards (such as '*' and
### '?').  All entries which match will be applied to the file.
### Note that auto-props functionality must be enabled, which
### is typically done by setting the 'enable-auto-props' option.
*.bmp = svn:mime-type=image/bmp;svn:needs-lock=*
*.gif = svn:mime-type=image/gif;svn:needs-lock=*
*.ico = svn:mime-type=image/x-icon;svn:needs-lock=*
*.jpeg = svn:mime-type=image/jpeg;svn:needs-lock=*
*.jpg = svn:mime-type=image/jpeg;svn:needs-lock=*
*.png = svn:mime-type=image/png;svn:needs-lock=*
*.tif = svn:mime-type=image/tiff;svn:needs-lock=*
*.tiff = svn:mime-type=image/tiff;svn:needs-lock=*

*.doc = svn:mime-type=application/msword;svn:needs-lock=*
*.jar = svn:mime-type=application/octet-stream;svn:needs-lock=*
*.odc = svn:mime-type=application/vnd.oasis.opendocument.chart;svn:needs-lock=*
*.odf = svn:mime-type=application/vnd.oasis.opendocument.formula;svn:needs-lock=*
*.odg = svn:mime-type=application/vnd.oasis.opendocument.graphics;svn:needs-lock=*
*.odi = svn:mime-type=application/vnd.oasis.opendocument.image;svn:needs-lock=*
*.odp = svn:mime-type=application/vnd.oasis.opendocument.presentation;svn:needs-lock=*
*.ods = svn:mime-type=application/vnd.oasis.opendocument.spreadsheet;svn:needs-lock=*
*.odt = svn:mime-type=application/vnd.oasis.opendocument.text;svn:needs-lock=*
*.pdf = svn:mime-type=application/pdf;svn:needs-lock=*
*.ppt = svn:mime-type=application/vnd.ms-powerpoint;svn:needs-lock=*
*.ser = svn:mime-type=application/octet-stream;svn:needs-lock=*
*.swf = svn:mime-type=application/x-shockwave-flash;svn:needs-lock=*
*.vsd = svn:mime-type=application/x-visio;svn:needs-lock=*
*.xls = svn:mime-type=application/vnd.ms-excel;svn:needs-lock=*
*.zip = svn:mime-type=application/zip;svn:needs-lock=*