Wednesday 8 July 2009

Subversion Log Queries

I want to show off how good Subversion is as a source control system, and one of it's strengths is the vast amount of information you can get about the code that is in your repository. At a previous employer we had a very cool set of old style ASP pages that tied our source repository to our timesheet system to make some extremely useful reports. To make a start at trying to recreate these reports in subversion, I needed to be able to query the subversion logs.

Rather than relying on having the svn command line client installed, or using SharpSVN which I found a bit unsatisfactory for querying logs I thought I'd have a bash at writing a standalone HTTP client in .Net. After a bit of searching for some documentation, I came up with the following fairly simple code:
static void Main(string[] args)
{
var sandpiperUri = new Uri(@"https://sandpiper.svn.sourceforge.net/svnroot/sandpiper");
var request = WebRequest.Create(sandpiperUri + "/!svn/vcc/default");
request.Method = "REPORT";
request.ContentType = "text/xml";
var requestbody = "<S:log-report xmlns:S=\"svn:\"><S:start-revision>2</S:start-revision>" +
"<S:end-revision>2</S:end-revision></S:log-report>";
request.ContentLength = requestbody.Length;
using (var requestStream = request.GetRequestStream())
{
using (var writer = new StreamWriter(requestStream))
{
writer.Write(requestbody);
}
}
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
Console.ReadLine();
}
This code queries the log of my rarely updated sourceforge project and produces the following results:
<?xml version="1.0" encoding="utf-8"?>
<S:log-report xmlns:S="svn:" xmlns:D="DAV:">
<S:log-item>
<D:version-name>2</D:version-name>
<D:comment>Initial Commit of Pre-Alpha Code</D:comment>
<D:creator-displayname>davebrunger</D:creator-displayname>
<S:date>2008-02-02T10:08:17.918376Z</S:date>
</S:log-item>
</S:log-report>
Now all I have to do is parse the ouput and create a nice interface to call the code. Oh yes, and implement some more of the subversion report features.

For information on the subversion http interface see this file: http://svn.collab.net/repos/svn/trunk/notes/webdav-protocol.

No comments:

Post a Comment