dcsimg

Download SVN Code Automatically Using C#

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps


Introduction

During the development phase, developers check out and check in source code every day. Sometimes, the source code checkout task to a local repository could be automatically executed by a job. Automation of these activities will help to automate code setup in the developer's machine or the build process of an application. In this article, I will demonstrate an automated SVN checkout process from a C# console application. The Tortoise SVN command line tool uses svn.exe. This tool is helpful to pull code from an SVN server.

What Is Tortoise SVN?

Tortoise SVN is a free, easy to use, open source Windows shell extension for the Apache Subversion version control system. Subversion provides a command line and GUI client to run. Source code is maintained in the Tortoise SVN central repository that manages files and directories. Developers use the SVN client to check out and check in source code. To automate SVN checkout, we need to have Tortoise SVN installed, along with command line client tools.

SVN Command line tools will install svn.exe, that will be used in our Visual Studio code for checkout.

Sample Code for SVN Automation

Step 1

Create a console application in Visual Studio and name it SVNCheckOut. Add the following App.config file in the application and create the required app setting keys.

  • SVNURL - Server URL: Code repository and version control server
  • SVNUserName: User name of the SVN
  • SVNPassword: Password of the SVN repository
  • SVNLocalCodePath: Local developer's machine path to check out code
  • TortoiseSVNPath: Path where SVN is installed locally
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
      <add key="SVNURL" value="sampleURL" />
      <add key="SVNUserName" value="XYX" />
      <add key="SVNPassword" value="XYZ" />
      <add key="SVNLocalCodePath" value="C:\Tapas\LocalSVN" />
      <add key="TortoiseSVNPath" value="C:\Program Files\
         TortoiseSVN\bin" />
   </appSettings>
</configuration>

Step 2

In the main method, I have written the following code to pull the SVN URL, local path, user name, and password from the app settings. To download the code from SVN, I have called the CheckOutSVNCode method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SVNCheckOut
{
   public class SVNSample
   {
      public void Main(string[] args)
      {
         string strSVNURL = System.Configuration.Configuration
            Manager.AppSettings[SVNURL];
         string strSVNUserName = System.Configuration.Configuration
            Manager.AppSettings[SVNUserName];
         string strSVNPassword = System.Configuration.Configuration
            Manager.AppSettings[SVNPassword];
         string strSVNLocalCodePath = System.Configuration
            .ConfigurationManager.AppSettings[SVNLocalCodePath];
         string strTortoiseSVNPath = System.Configuration
            .ConfigurationManager.AppSettings[TortoiseSVNPath];
         CheckOutSVNCode(strSVNURL, strSVNUserName, strSVNPassword,
            strSVNLocalCodePath, strTortoiseSVNPath);
         Console.ReadLine();

      }

      private static void CheckOutSVNCode(string strSVNURL,
         string strSVNUserName, string strSVNPassword,
         string strSVNLocalCodePath, string strTortoiseSVNPath)
      {
         string arguments = " checkout " + strSVNURL + " " +
            strSVNLocalCodePath + "  --username " + strSVNUserName
            + " --password " + strSVNPassword;
         ProcessStartInfo info = new ProcessStartInfo("svn.exe",
            arguments);
         info.WorkingDirectory = strTortoiseSVNPath;
         info.WindowStyle = ProcessWindowStyle.Normal;
         Process.Start(info);
         Console.WriteLine("Checkout started");
      }
   }
}

Step 3

In the CheckOutSVNCode method body, I have executed run svn.exe with the arguments necessary to pull code from SVN and save it in the local code repository.

Checkout SVN supports a big list of commands. Developers can automate the process to extend these commands from an application by passing various commands to svn.exe and automate the code setup or build process. Following is the list of frequently used commands SVN.exe supports.

Update

svn update [-r rev] PATH

For updating items in Subversion.

Update to Revision

svn update [-r rev] [-depth ARG] [--ignore-externals] PATH

Commit

svn commit -m "LogMessage" [-depth ARG] [--no-unlock] PATH...

LogMessage here represents the contents of the log message edit box.

Diff

svn diff PATH

TortoiseSVN just feeds the two files into the chosen diff program and shows the differences.

Show Log

svn log -v -r 0:N --limit 100 [--stop-on-copy] PATH

or

svn log -v -r M:N [--stop-on-copy] PATH

Revision Graph

svn info URL_of_WC
svn log -v URL

Repo Browser

svn list [-r rev] -v URL

You can use svn info to determine the repository root. Also, this command returns all the locking information shown in the repository browser.

Resolved

svn resolved PATH

Rename

svn rename CURR_PATH NEW_PATH

Delete

svn delete PATH

Revert

svn revert [-R] PATH...

Conclusion

I hope this article explains how to automate the SVN checkout process. Because all commands for Tortoise SVN are controlled through command line parameters, a developer can automate it with batch scripts or applications. That's all for today. Happy coding!

Reference

https://tortoisesvn.net/docs/nightly/TortoiseSVN_en/tsvn-automation.html



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.