SVN – Sub-Directory Renames via C#

I had to update a number of sub-directory names in an SVN repository. To do this I used C# (due to me getting far too comfortable in NetBeans and needing to dust off some cobwebs for Visual Studio).

I also used the SharpSVN library.

There is nothing amazing about the code, just essentially using my blog as a quick repository for future reference if required:

string checkoutPath = @"c:\temp\tempcheckout";
string svnUrl = "svn://mysvnrepositorypath";
SvnClient client = null;

try
{
   client = new SvnClient();
   client.CheckOut(new Uri(svnUrl), checkoutPath);
   client.Update(checkoutPath);

   DirectoryInfo di = new DirectoryInfo(checkoutPath);

   foreach (DirectoryInfo level1 in di.GetDirectories())
   {
      foreach (DirectoryInfo level2 in level1.GetDirectories())
      {
           foreach (DirectoryInfo level3 in level2.GetDirectories())
           {
              if (!level3.FullName.Contains(".svn"))
              {
                   if (level3.Name.Equals(matchPattern))
                   {
                        client.Move(level3.FullName, newDirectoryName);
                   }
              }
          }
      }
   }

   SvnCommitArgs args = new SvnCommitArgs();
   args.LogMessage = "My logging message";
   client.Commit(checkoutPath, args);
}
catch (Exception e)
{
   Console.WriteLine("*** Exception : " + e.Message + " ****");
}
finally
{
   if (client != null)
   {
      client.Dispose();
   }
}

Console.WriteLine("SVN rename complete.");

As I’m typing this I’m realising my subtle change from using a using() block as suggested by the SharpSVN examples and my try…catch…finally block. I’ve been weary about using ‘using’ blocks since exposure to WCF in .NET 3.5 which didn’t implement the using block quite the same as in other places. I’m now off to do some reading about using blocks to gain a bit of better understanding of them.

As a final note, I did original implement this just to do the check-out and then rename the directories outside of using the SharpSVN toolkit, and then tried do a commit of changes via TortoiseSVN – which failed.

Advertisements

4 thoughts on “SVN – Sub-Directory Renames via C#

    • Good point – hadn’t considered that. Was just looking at the set-up I had! I’ll certainly bare it in mind in the future. Thank you.

    • Hi,

      I’m not certain exactly what you mean? If you’d like to expand I’d be more than happy to help.

      You could use something along the lines of TortoiseSVN to view you repository history, and take the versions you require from a known state / check-in?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s