free hit counter

Thursday, February 07, 2008

SVN email on commit

So getting SVN to email on commit was way harder than it should have been, but it was mostly this was because I'm running my SVN server under Windows.

I was running around in circles with the usual \r\n differences, but the real problem was finding a compliant command line email utility. mailsend was the first one that worked not just when I was logged in and testing, but more importantly when the Apache service kicked off the job.

My post-commit.bat file in the hooks directory:

@echo off
::
:: emails out the log message after a commit
::
perl d:\svn\email-on-commit.pl %1 %2 testing


My full perl script (which isn't working without the testing flag being set):

#!/bin/perl
#
# Rian Sanderson
# Feb 7 2008
#
# First two inputs are standard issue from SVN
# Last argument is optional, and modifies the send to address so you
# don't start spamming your group with half baked emails
#
# Have users include a tilde (~) in their log messages for a quiet
# mode
#
# broken: does not work without testing flag
#
$repos=$ARGV[0];
$repos=~ s/\\/\\\\/g;
$rev=$ARGV[1];
$testingFlag=$ARGV[2];

if ($testingFlag) {
$debugLogName= "$repos/hooks/debug-log-post-commit-hook.txt";
open(DEBUGLOG,">>$debugLogName");

$theTime= localtime time;
print DEBUGLOG "\n\n--$theTime\n";
print DEBUGLOG " $repos $rev\n";
}

$quietModeFlag=`svnlook log -r $rev $repos | grep '~'`;

if ($quietModeFlag ) {
if ($testingFlag){
print DEBUGLOG " not generating an email per the user's request.\n";
exit 0;
}
}

#create the subject
$numFiles=`svnlook changed -r $rev $repos | wc -l`;
chomp($numFiles);
$synopsis=`svnlook log -r $rev $repos | head -c 42`;
$synopsis=~ s/\s*$//;
$subject="\"[$rev] $numFiles files: $synopsis...\"";
#$subject="\"[$rev]: $synopsis...\"";
if ("$testingFlag") {
print DEBUGLOG " numFiles: $numFiles\n";
print DEBUGLOG " subject: $subject\n";
}

# create the body of the message
$bodyFile= "$repos/hooks/$rev.txt";
`echo >> "$bodyFile"`;
system("svnlook log -r $rev $repos > $bodyFile");
`echo >> "$bodyFile"`;
`echo >> "$bodyFile"`;
`echo "files changed:" >> "$bodyFile"`;
system("svnlook changed -r $rev $repos >> $bodyFile");
`echo "." >> "$bodyFile"`;


#ship it off
if ("$testingFlag") {
$toAddr='me@mydomain.com';
}
else {
$toAddr='team@mydomain.com';
}
$fromAddr= 'svn@mydomain.com';
#$mailCmd="blat $bodyFile -server mail.mydomain.com -to $toAddr -from $fromAddr -subject $subject ";
$mailCmd="-d mydomain.com -smtp mail.mydomain.com -t $toAddr -from $fromAddr -sub $subject -m $bodyFile,text/plain +cc +bc";
if ("$testingFlag") {
print DEBUGLOG " mailsend $mailCmd";
}
close DEBUGLOG;

system("mailsend $mailCmd >> $debugLogName");

#dispose of the body
if (! $testingFlag) {
unlink($bodyFile)
}

Labels:

0 Comments:

Post a Comment

<< Home