Creating a post-commit hook for Subversion

I am doing some work with Subversion now so I got a chance to play with some hook functionality. Unfortunately, 99.99993% of all information out there for Subversion seems to be directed towards Linux, but after an appropriate amount of head-banging I got it working. Our dev server has Subversion installed with a repository holding our code. Each developer has installed TortoiseSVN (which is a Windows GUI client for the server) and checked out the code for local development. Then, on the server we also checked out the trunk folder into our wwwroot for IIS. The problem was, ever time we committed code from our local machine and wanted to run it on the dev server, we needed to remote into the server to update the code checked out to the wwwroot folder.

[More]

Comments
Dan G. Switzer, II's Gravatar @Brad:

I went through the same pains you're going through when I first started writing SVN hooks for Windows. Fortunately, I found a few really good debugging tips that should save you a bunch of time:

http://blog.pengoworks.com/index.cfm/2008/2/5/Debu...

You already figured out #1, but I think you'll find tip #3 invaluable. By saving the output log of my SVNs, it now takes me no time to figure out where my script is failing. This was the biggest obstacle I kept running into. Before I started using this technique, I never knew why the scripts weren't working correctly.
# Posted By Dan G. Switzer, II | 9/16/08 12:53 AM
Brad Wood's Gravatar Awesome, Dan. That was just was I was looking for last night. Thanks.
# Posted By Brad Wood | 9/16/08 1:00 AM
todd's Gravatar Real Names in Log messages for Subversion, using a post-commit hook for subversion combined with a rev-propset

We had these problems: Does anyone have a way to use "real names" in log messages? We've got
our new SVN server authenticating against active directory, but my
company uses employee IDs for usernames. see http://svn.haxx.se/users/archive-2007-08/0347.shtm...

Here's our proof of concept that might be use to someone. I hope this might be userful

Basic call order:

1.   SVN client checks in
2.   post-commit.bat
a.   calls actual hook and logs
3.   post-commit-run.bat
a.   post-commit does NOT appear to be given the user – go and get it from the repository
b.   svnlook for actual commit <user> and <revision>: svnlook author . && svnlook history . [you will then need to grep the information out of these commands]
c.   ldap lookup for human readable name
d.   update author to <name> rather than <user>: svn propset svn:author --revprop –r <rev> <name>
4.   pre-revprop-change.bat
a.   calls actual hook and log
5.   pre-revprop-change-run.bat
a.   any filtering here – at this stage empty file will suffice but this file MUST exist for svn:author to be set

Tips:
•   chain files for logging (see reference 2 below)
•   pre-revprop-change.bat MUST exist (this is well documented in SVN literature)
•   look at caching ldap lookups (eg Ruby ActiveLDAP library)
•   remember that .bat files must not have shell commands in it. I forgot that # is a remark for sh rather than REM for batch files. The logging will display error easily though

References:

•   Good list of the hooks: http://www.codersrevolution.com/index.cfm/2008/9/1...
•   3 strategies: including how to debug: http://blog.pengoworks.com/index.cfm/2008/2/5/Debu...
•   Good example of a windows hook script: http://svn.haxx.se/users/archive-2006-03/0107.shtm...

Scripts:

post-commit.bat

call %~dp0post-commit-run.bat %* > %1/hooks/post-commit.log 2>&1

post-commit-run.bat

set repos=%1
set rev=%2
set user=%3
set propname=%4
set action=%5

REM this is where the svnlook and ldap look up must occurs
REM then do a svn propset svn:author --revprop –r <rev> <name>

pre-revprop-change.bat

call %~dp0pre-revprop-change-run.bat %* > %1/hooks/pre-revprop-change.log 2>&1

pre-rev-prop-change-run.bat

set repos=%1
set rev=%2
set user=%3
set propname=%4
set action=%5
REM does nothing but must exist


TODO:
•   Write a batch file parser of svnlook results
•   Write an LDAP look up
# Posted By todd | 2/16/09 3:18 PM
BlogCFC (5.9.004) by Ray Camden. Blog Owner: Brad Wood