SVN post-commit for Windows
As I mentioned early I've been working on a SVN post-commit script. We've got a SVN repository that will be modified by several remote developers and I really need to keep an eye on this repository and I need to closely monitor changes to this repository.
There are two major functions that I needed in my post-commit script:
- I needed to update the working copy on my server
- I needed to e-mail the changes to myself, so I know when developers are making changes
There are an abundant of examples showing off how to do this in various *nix flavors, but I couldn't find any good Windows-based solutions that didn't require Perl to be installed on the server. That led me to create the following post-commit.bat script.
@ECHO OFF REM ************************************************************* REM * this sets the arguments supplied by Subversion * REM ************************************************************* SET REPOS=%1 SET REV=%2 REM ************************************************************* REM * define directory paths * REM * you *must* add any paths for command line tools you plan * REM * since SVN does not include the Windows %PATH% environment * REM * variable for security reasons. * REM * * REM * DIR - the current hooks directory * REM * PATH - a user set path of where executables are located * REM * * REM ************************************************************* SET DIR=%REPOS%/hooks REM ************************************************************* REM * make sure to add the path to the SendEmail executable * REM ************************************************************* SET PATH=%PATH%;%DIR%;C:\Utils REM ************************************************************* REM * define the path to the working copy of your code * REM * * REM * the default working_copy folder would be: * REM * file:///%REPOS% * REM ************************************************************* SET WORKING_COPY=c:\path\to\working\copy REM ************************************************************* REM * define e-mail parameters * REM ************************************************************* SET SITENAME=My Code Base SET SMTP_SERVER=smtp.yourdomain.com SET EMAIL_TO=svnupdates@yourdomain.com SET EMAIL_FROM=svnupdates@yourdomain.com SET SUBJECT=SVN Update - %SITENAME% - rev %REV% - %REPOS% REM ************************************************************* REM * update the local working copy * REM * this will sync the new changes to your servers copy * REM ************************************************************* svn update %WORKING_COPY% REM ************************************************************* REM * generate the header to use for the e-mail message * REM ************************************************************* ECHO The following changes were made to the code: > %DIR%/email.txt ECHO. >> %DIR%/email.txt REM ************************************************************* REM * dump the log of changes to the e-mail message * REM ************************************************************* svn log %WORKING_COPY% -v -r "%REV%" >> %DIR%/email.txt REM ************************************************************* REM * dump the diff changes to the e-mail message * REM * * REM * WARNING: Generates tons of output * REM * * REM * NOTE: this is optional, you can remove this information * REM * if you do not want a verbose message of changes * REM ************************************************************* svn diff %WORKING_COPY% -c "%REV%" --no-diff-deleted >> %DIR%/email.txt REM ************************************************************* REM * send the e-mail message to the user * REM * * REM * NOTE: to create a SendEmail.log file, append: * REM * * REM * -l %DIR%/SendEmail.log * REM * * REM * at the end of the line below. * REM ************************************************************* sendEmail -s %SMTP_SERVER% -t %EMAIL_TO% -f %EMAIL_FROM% -u "%SUBJECT%" -o message-file=%DIR%/email.txt
A couple of notes on the script above:
- You'll need to download SendEmail in order to send the e-mail messages. In the environmental variable %PATH% you'll want to make sure to include the path where you copied the SendEmail.exe. In the example above, the SendEmail.exe is located in the C:\Utils directory.
- Make sure to go through the script and replace any variables declared with the SET command with the values that coorelated to your environment.
- In my case I required a working copy to be placed on my server, as it's used as a global development environment. If you do not require a local working copy, you can comment out the svn update %WORKING_COPY% line (by placing the command REM in front of the line.) You would then change your WORKING_COPY variable to the URL of your SVN repository—which would be file:///%REPOS%.
- The svn diff command can generate a ton of information. In most use cases, you probably do not want to add this information to the e-mail output.
If you have any problems getting this script working, I recommend you check out my blog entry on debugging SVN repository hook scripts in Windows.

Comments
For others that come across this, Here's where I got stumped. If you're running subversion with apache, this script will be run as Apache. I needed to run this against a network path like: \\server\share I couldn't, because apache didn't have network access rights.
I had to update the apache service (and perhaps the subversion service) to run using my user account. It worked fine after that.
@echo off
set REPOS=%1
set REV=%2
c:\SVN\ServiceRoot\bin\svnlook changed -r %REV% %REPOS% >
c:\SVN\Repo\CodeBoy\hooks\changed.txt
c:\python25\python C:\SVN\Repo\CodeBoy\hooks\updateChanged.py
This way you can only call an update on the modified paths instead of killing your box trying to update the entire checkout.