Demo using SHARPSVN to perform SVN operations

Source: Internet
Author: User
Tags subversion client

1. SHARPSVN Introduction

SharpSvn.dll is a Subversion Client API for. Net 2.0-4.0+ applications, see https://sharpsvn.open.collab.net/for more details.

2. Authentication operation

SHARPSVN provides related authentication actions through the authentication interface, such as user name and password acquisition, certificate validation, and so on. The authentication interface defines the associated event handle (handlers) for each of these operations. When certain authentication actions are required, the SHARPSVN will invoke the corresponding handle. All we need to do is mount the authentication action we want to perform on the corresponding handle. Examples of user name and password acquisition and certificate validation are provided below.

2.1 User name and password acquisition

When an SVN operation is performed that requires user rights authentication, the handle in usernamepasswordhandlers is called to obtain the user name and password. For example

using New svnclient ()) {    +=        New Eventhandlers<svnusernamepasswordeventargs>(            delegate (object  s), Svnusernamepasswordeventargs e)            {                "test";                 " Password " ;            
2.2 Certificate Confirmation

When we access an SVN server that uses a protocol other than our default execution criteria, the SVN customer will let the user confirm whether the server is trustworthy and continues to be accessed. At this point, SHARPSVN executes the event handle that is mounted in the Sslservertrusthandlers in the authentication interface. For example

using New svnclient ()) {    +=        Delegate (object s, svnsslservertrusteventhandlerargs> e)        {            true;             //  // indicates that the server is not trusted and that access is discarded.         };}
2.3 Sharpsvnui Bindings

The SharpSvn.UI.dll provides a default interface for the operation. For example, the above user name and password get interface, certificate confirmation interface. We only need to write the following two lines of code into our program, SHARPSVN will automatically mount the corresponding event handle. The corresponding interface is ejected when a user name and password are required or when the certificate is acknowledged.

using New svnclient ()) {    new  SharpSvn.UI.SvnUIBindArgs ();    SharpSvn.UI.SvnUI.Bind (client, Uibindargs);}
3 SVN basic Operation 3.1 updates update
using New svnclient ()) {    string path = @ "D:\\svn\temp";    New Svnupdateargs ();     = Svndepth.empty;    Client. Update (path, Updateargs);}
3.2 Adding version control add
using New svnclient ()) {    string@ "d:\\svn\temp\new.txt";     New Svnaddargs ();     = Svndepth.empty;    Client. ADD (path, args);}
3.3 Commit Commit
 using  (svnclient client = new   Svnclient ()) {Svncommitargs Commitargs  = new   Svncommitargs ();    Commitargs.depth  = Svndepth.empty; Commitargs.logmessage  =  my Test Commit      ;    Svncommitresult commitresult  = null   @ " d:\\svn\temp\test.txt  , Commitargs, out   Commitresult);}  


Other operations, such as Delete, Lock, Unlock, and so on, the code structure of these basic operations are similar to the above operations, here is not to repeat.

3.4 Getting this more file column
using(Svnclient client =Newsvnclient ()) {Svnstatusargs args=NewSvnstatusargs (); Args. Depth=Svndepth.empty; Args. Retriveremotestatus=false; Collection<SvnStatusEventArgs> list =NewCollection<svnstatuseventargs>(); Client. GetStatus (@"d:\\svn\temp", args, outlist); foreach(Svnstatuseventargs EventArgsinchlist) {        //get information about each change file from the EventArgs    }}
4 SHARPSVN Operation Log Acquisition

With Svnclientreporter, you can redirect Sharpsvn's operation log information to a StringBuilder or a file, and the following two lines of code SHARPSVN redirect to a StringBuilder,

using New svnclient ()) {    new  StringBuilder ();     New svnclientreporter (client, strbuilder);}

Note: SHARPSVN can be redirected to multiple targets and can be redirected to the same target multiple times. For example, if more than one Svnclientreporter object is defined for the same StringBuilder, the SHARPSVN operations log in that StringBuilder records multiple copies. You can manually call Dispose () of the Svnclientreporter object to release a log redirect.

5 Cancel Operation

The cancellation mechanism of the SHARPSVN is similar to the cancellation mechanism of the thread. There will be some cancellation detection points during the SHARPSVN operation. When the operation executes to a checkpoint, it will determine if the user has set the cancellation flag, if not, continue the operation, or terminate the current operation if the cancellation flag is set.

During the SHARPSVN operation, a cancel event is punished when a cancellation checkpoint is reached, and the cancel event is passed to get the user to set the cancellation flag. When we want to cancel the SVN operation, register the Cancel event handling method, then set the Cancel property of the Svncanceleventargs object to True for the method. For example

using New svnclient ()) {    //dosomething    client. Cancel + =        delegate (object  s, Svncanceleventargs e        )            {true ;        };     // Do SVN operations}
6 SVN Remote Session Svnremotesession


An SVN remote session svnremotesession can be established by providing an SVN control element path.

string @" Https://10.23.34.45:6801/svn/temp/test.txt "  New svnremotesession (new Uri (ElementPath));

Call the Getrepositoryroot () method of the Svnremotesession object to get the root directory of the current SVN configuration library server.

NULL ; Remotesession.getrepositoryroot (out Reporooturi);

Once we get to the root of the SVN configuration library, we can call the Reparent () method to set the Svnremotesession session object to the root of the SVN configuration library.

Remotesession.reparent (Reporooturi);

After setting up the remote session to the SVN configuration Coogan directory, we can perform related remote operations for each configuration library element, such as listing all elements under a configuration library directory, getting the latest version number of the current configuration library, obtaining a commit record for an element, and so on. Here are some examples of these three operations,

6.1 List all elements under the directory (non-recursive)
stringDirpath =@"https://10.23.34.45:6801/svn/temp";stringDirrelpath = Remotesession.makerepositoryrootrelativepath (NewUri (Dirpath));//Get relative directories relative to the Coogan directoryremotesession.list (Dirrelpath,NewEventhandler<svnremotelisteventargs>(        Delegate(Objects, Svnremotelisteventargs e) {            //e.name: element name//E.path: Element Path//e.retrievedrevistion: The version number of the element}));
6.2 Get the latest version number of the configuration library
Long 0 ; Remoetsession.getlatestrevision (out latestrevision);
6.3 Get the commit record for the specified file
New Uri (@ "https://10.23.34.45:6801/svn/temp/test.txt"); string Filerelpath = Remotesession.makerepositoryrootrelativepath (fileuri);     Delegate (object  s, Svnremotelogeventargs e)    {        //E.author: Author         //e.revision: Version no         .//   E.logmessage: Submit comment information    }));
7 concluding remarks

The Demo sample provided by the SHARPSVN project itself is almost no, and the documentation provided is poorly documented for each API. Before because of job needs, on-line search also did not find much about SHARPSVN sample introduction. These are just some of their own needs to use and validation of some of the demo sample, there is no more to use SHARPSVN, so the exploration of sharpsvn is so shallow, too lazy to continue in depth.

Demo using SHARPSVN to perform SVN operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.