Will the last developer out please turn off the lights?

Random postings that others might find useful

Changing the svn:author in an SVN Commit Hook script

leave a comment »

I recently wanted to change the Author that appears in SVN commit logs from the account name of the commiter to their email address. I knew that this could be done from a Post Commit Hook using the svn propset command, but I had real problems getting it to work on Windows.

The hook script provides a path to the repository being used, but this was in the form D:\Respositories\…. and svn propset kept thinking that this was a path to a working copy of the repository rather than the repository iteself. My original hook script looked like this:

set REPOS=%1
set REV=%2
set EMAILADDRESSES=”developers@developers.com
set OS=Windows_NT
set PATH=%PATH%;C:\Program Files\VisualSVN Server\bin\;C:\Perl\site\bin;C:\Perl\bin;

REM Lookup the author of the commit
FOR /F “tokens=*” %%i in (‘”svnlook.exe author -r “%REV%” “%REPOS%””‘) do SET AUTHOR=%%i

REM Lookup the email address of the author
FOR /F “tokens=*” %%A IN (d:\emailmaps\%AUTHOR%) DO set USERNAME=”%%A”

REM Rewrite the author
svn propset –revprop -r %REV% svn:author %USERNAME% %REPOS%

REM Send out a email to development team to notify them of the commit
svnnotify –repos-path %REPOS% –revision %REV% –to %EMAILADDRESSES% –from %USERNAME% –smtp 127.0.0.1 –subject-prefix “SVN – Rev: %%d – ” –handler HTML::ColorDiff –with-diff –max-diff-length 8192

I tried changing the URL of the Repo to be file:// so that the path was actually a URL (at least in a form that Internet Explorer understands:

svn propset –revprop -r %REV% svn:author %USERNAME% file://%REPOS%

However, this gave an svn: Illegal repository URL error. After some experimenting I found that the slashes had to be replaced with forward slashes and an additional slash needed putting after the file:. I ended up using a useful feature of DOS to replace the slashes. The final script looks like this:

set REPOS=%1
set REPOS_URL=file:///%REPOS:\=/%
set REV=%2
set EMAILADDRESSES=”developers@developers.com
set OS=Windows_NT
set PATH=%PATH%;C:\Program Files\VisualSVN Server\bin\;C:\Perl\site\bin;C:\Perl\bin;

REM Lookup the author of the commit
FOR /F “tokens=*” %%i in (‘”svnlook.exe author -r “%REV%” “%REPOS%””‘) do SET AUTHOR=%%i

REM Lookup the email address of the author
FOR /F “tokens=*” %%A IN (d:\emailmaps\%AUTHOR%) DO set USERNAME=”%%A”

REM Rewrite the author
svn propset –revprop -r %REV% svn:author %USERNAME% %REPOS_URL%

REM Send out a email to development team to notify them of the commit
svnnotify –repos-path %REPOS% –revision %REV% –to %EMAILADDRESSES% –from %USERNAME% –smtp 127.0.0.1 –subject-prefix “SVN – Rev: %%d – ” –handler HTML::ColorDiff –with-diff –max-diff-length 8192

 You also need a pre-revprop-change script to make sure that changing of properties is allowed. Mine looks like this:

REM Only allow log messages or author to be changed.
if “%4” == “svn:log” exit 0
if “%4” == “svn:author” exit 0
echo “Property ‘%4’ cannot be changed > &2
exit 1

Written by Tim

July 25, 2011 at 9:46 am

Posted in SVN

Tagged with , ,

Leave a comment