SVN post-commit trigger

We use a post-commit trigger at work to notify the team of all commits made to the repository. I needed to make some modifications, so I thought I’d update my sorely neglected blog with the details. You know, for the kids…

Original post-commit file

This isn’t the exact file we use, but the majority of it was taken from this post. Dan has a great file that you can pretty much drag ‘n drop and have some seriously cool emails flying around. As Dan notes, you’ll need SendMail, but it is practically an out-of-the-box solution.

Recent tweaks

I get email. A LOT of email. It is a bit hard to manage, so I got rules up the wazoo! I originally had all the commit emails simply going into my “Commits” subfolder. However, we have some different apps, and a few different web sites going on, so having all the notifications dumped into a single folder wasn’t working either. Dan’s script adds some useful information to the subject line, but it wasn’t quite what I was looking for. I was trying to use keywords from the body of the email, but that wasn’t 100% either. So, I had to make some tweaks to get what I wanted.

I wanted to add the repo folder that was updated to the subject line. This way, I could write my rules to look for keywords based on the folder names of our projects in the repo. This would be fool-proof. Batch files have always been a little odd to me, and some of the deep, dark recesses of DOS are left unexplored. I did some searching and came up with this StackOverflow question about getting details of what was committed via svnlook. Using the dir-changed along with the suggested for loop worked quite well. Now all I had to do was get a part of the dir-changed.

More searching turned up this and this, which are both essentially the same. They pointed me down the right path and I got it going.

Not quite perfect

I am totally anal. I have personally labeled myself a “Code Nazi”. I can’t stand extra spaces, or extra line breaks, or anything that is seemingly unnecessary. It seems that svnlook author must add some extra spaces to the end of the author name, because there were 3 extra spaces at the end of every name. Up until now, I decided to just let this go. I mean, really, it wasn’t like it made the subject line illegible or anything. But, I love Mike Holmes, and like he says, make it right! So more searching turned up this great page on DOS string manipulation. I used it to replace the spaces in the author name and now the subject line is perfect!

The script

After all this talking, I could at least share the code, right? As I said earlier, it is heavily borrowed from Dan’s great code, with some minor updates made for my taste. Here you go:

@ECHO OFF
REM *************************************************************
REM * this sets the arguments supplied by Subversion            *
REM *************************************************************
SET REPOS=%1
SET REV=%2

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=file:///c:/web/svnroot
SET DIR=%REPOS%/hooks

REM *************************************************************
REM * get author's name                                         *
REM *************************************************************
for /f %%a in ( 'c:\svn\svnlook.exe author %REPOS% -r %REV%' ) do ( SET AUTHOR=%%a )

REM *************************************************************
REM * define e-mail parameters                                  *
REM *************************************************************
SET SITENAME=emw2k8dev
SET SMTP_SERVER=192.168.0.112
SET EMAIL_TO=[email protected]
SET EMAIL_FROM=[email protected]
for /F "eol=; tokens=1 delims=/" %%a in ( 'c:\svn\svnlook.exe dirs-changed %REPOS% -r %REV%' ) do ( SET SITENAME=%%a )
SET SUBJECT=SVN Update - %AUTHOR: =% - %SITENAME% - rev %REV% - \\emw2k8dev\%REPOS:C:\=%

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 *************************************************************
c:\svn\svn log %WORKING_COPY% -v -r "%REV%" >> %DIR%/email.txt
ECHO. >> %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 *************************************************************
c:\svn\svn diff %WORKING_COPY% -c "%REV%" --no-diff-deleted >> %DIR%/email.txt
ECHO. >> %DIR%/email.txt

REM *************************************************************
REM * send the e-mail message to the user                       *
REM *************************************************************
c:\web\svnroot\sendEmail.exe -s %SMTP_SERVER% -t %EMAIL_TO% -f %EMAIL_FROM% -u "%SUBJECT%" -o message-file=%DIR%/email.txt -l %DIR%/SendEmail.log

So it is by no means perfect or anything, but it may come in handy to someone else.