In continuation to my pevious blog post on defining configuration path to connect to remote repos using SharpSvn, I present quick sample code for connecting to and fetching logs from a remote svn repo requiring authentication.

The topic as such should not need much introduction, so here’s the plain sample code:

 

{syntaxhighlighter brush: csharp;fontsize: 100; first-line: 1; }using (SvnClient client = new SvnClient())
{
//See the above referenced blog post to understand what the first line below does.
client.LoadConfiguration(Path.Combine(Path.GetTempPath(), “Svn”), true);

client.Authentication.SslServerTrustHandlers += delegate(object sender, SvnSslServerTrustEventArgs e)
{
//You might want to double-check that the server is trusted, instead of accepting them blindly.
e.Cancel = false;
};

SvnLogArgs args = new SvnLogArgs(new SvnRevisionRange(500, new SvnRevision(SvnRevisionType.Head)));

System.Collections.ObjectModel.Collection<SvnLogEventArgs> revisions;
client.Authentication.DefaultCredentials = new System.Net.NetworkCredential(“svn-username”, “svn-password”);
client.GetLog(new Uri(“https://your-svn-server.com”), args, out revisions);

foreach (var item in revisions)
{
//Do whatever you want with a revision entry here. item.Author, item.LogMessage, item.Revision etc. are some sample available properties.
………….
}
}{/syntaxhighlighter}

We start with creating a new SvnClient object from the SharpSvn library, set the configuration path for the library, add a delegate for specifying whether to trust the svn server certificate or not, create an SvnLogArgs object specifying various criteria for which we need the log entries, and finally call the GetLog method on SvnClient object, additionally passing the NetWorkCredentials object with username/password for the svn repo.

In the SslServerTrustHandlers delegate above, you would want to verify the server identity using information available in SvnSslServerTrustEventArgs parameter before trusting it (setting e.Cancel to true would mean you do not trust the server and SharpSvn would throw a SharpSvn.SvnRepositoryIOException).

It took me sometime to figure out the above code for remote repos (whereto specify the credentials and how to trust a server took much of the time to figure out as should be obvious). A good example demonstrating SharpSvn operations on a local repo is available here.