Demo of using SharpSvn to execute svn operations, sharpsvnsvn

Source: Internet
Author: User
Tags subversion client svn client

Demo of using SharpSvn to execute svn operations, sharpsvnsvn
1. Introduction to SharpSvn

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

2. Authentication

SharpSvn provides related Authentication operations through the Authentication interface, such as obtaining the user name and password, and verifying the certificate. The Authentication interface defines related event Handlers for these operations respectively ). When some authentication operations are required, SharpSvn will call the corresponding handle. We only need to mount the authentication operation we need to perform to the corresponding handle. The following are examples of user name and password acquisition and certificate validation.

2.1 username and password acquisition

When user permission authentication is required for executing a svn operation, the user name and password are obtained by calling the handle in UserNamePasswordHandlers. For example,

using (SvnClient client = new SvnClient()){    client.Authentication.UserNamePasswordHandlers +=        new EventHandlers<SvnUserNamePasswordEventArgs>(            delegate (object s, SvnUserNamePasswordEventArgs e)            {                e.UserName = "test";                e.Password = "password";            });} 
2.2 certificate confirmation

When the Svn server we access uses a protocol that is not implemented by default, the svn client will ask the user to confirm whether the server is trustworthy and continue to be accessed. In this case, SharpSvn executes the event handle mounted in SslServerTrustHandlers in the Authentication interface. For example,

Using (SvnClient client = new SvnClient () {client. authentication. sslServerTrustHandlers + = delegate (object s, svnsslservertrusteven thandlerargs> e) {e. save = true; // e. cancel = true; // indicates that the server is not trusted and the access is abandoned. };}
2.3 SharpSvnUI binding

SharpSvn. UI. dll provides a default operation interface. For example, the above user name and password Acquisition Interface and 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. When you need to obtain the user name and password or confirm the certificate, the corresponding interface is displayed.

using (SvnClient client = new SvnClient()){    SharpSvn.UI.SvnUIBindArgs uiBindArgs = new SharpSvn.UI.SvnUIBindArgs();    SharpSvn.UI.SvnUI.Bind(client, uiBindArgs);}
3 SVN basic operations 3.1 Update
using (SvnClient client = new SvnClient()){    string path = @”d:\\svn\temp";    SvnUpdateArgs updateArgs = new SvnUpdateArgs();    updateArgs.Depth = SvnDepth.Empty;    client.Update(path, updateArgs);}
3.2 Add Version Control
using (SvnClient client = new SvnClient()){    string path = @"d:\\svn\temp\new.txt";    SvnAddArgs args = new SvnAddArgs();    args.Depth = SvnDepth.Empty;    client.Add(path, args);}
3.3 submit Commit
using (SvnClient client = new SvnClient()){    SvnCommitArgs commitArgs = new SvnCommitArgs();    commitArgs.Depth = SvnDepth.Empty;    commitArgs.LogMessage = "My Test Commit";    SvnCommitResult commitResult = null;    client.Commit(@"d:\\svn\temp\test.txt", commitArgs, out commitResult);}


For other operations, such as Delete, Lock, and Unlock, the code structure of these basic operations is similar to the preceding operations.

3.4 obtain the columns of the current file
Using (SvnClient client = new SvnClient () {SvnStatusArgs args = new SvnStatusArgs (); args. depth = SvnDepth. empty; args. retriveRemoteStatus = false; Collection <SvnStatusEventArgs> list = new Collection <SvnStatusEventArgs> (); client. getStatus (@ "d :\\ svn \ temp", args, out list); foreach (SvnStatusEventArgs eventArgs in list) {// obtain information about each change file from eventArgs }}
4. Obtain SharpSvn operation logs

SvnClientReporter can be used to redirect SharpSvn operation log information to a StringBuilder or a file. The following two lines of code SharpSvn are redirected to a StringBuilder,

using (SvnClient client = new SvnClient()){    StringBuilder strBuilder = new StringBuilder();    SvnClientReporter reporter = 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 multiple SvnClientReporter objects are defined for the same StringBuilder, The SharpSvn operation log in the StringBuilder records multiple copies. You can manually call the Dispose () of the SvnClientReporter object to release a log redirection.

5. Cancel the operation.

The cancellation mechanism of SharpSvn is similar to that of the thread. The SharpSvn operation will have some cancellation Detection Points. When the operation reaches a detection point, the user determines whether the cancellation flag is set. If not, the operation continues. If the cancellation flag is set, the current operation is terminated.

During SharpSvn operations, a Cancel event is penalized when a cancellation detection point is reached, and the Cancel event is used to determine whether the cancellation flag has been set by the user. When we want to Cancel the svn operation and register the Cancel event processing method, set the Cancel attribute of the SvnCancelEventArgs object to true in the modification method. For example,

using (SvnClient client = new SvnClient()){    //do something    client.Cancel +=        delegate (object s, SvnCancelEventArgs e)        {            e.Cancel = true;        };    //do svn operations}
6 Svn remote session SvnRemoteSession


You can create a Svn remote session SvnRemoteSession by providing a Svn control element path,

string elementPath = @"https://10.23.34.45:6801/svn/temp/test.txt";SvnRemoteSession remoteSession = new SvnRemoteSession(new Uri(elementPath));

Call the GetRepositoryRoot () method of the SvnRemoteSession object to obtain the root directory of the current svn configuration Library server,

Uri repoRootUri = null;remoteSession.GetRepositoryRoot(out repoRootUri);

After obtaining the root directory 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 the remote session to the svn configuration database root directory, we can execute related remote operations on each configuration database element, for example, list all elements in a configuration library directory, obtain the latest version number of the current configuration library, and obtain the submission records of an element. The following are examples of these three operations,

6.1 list all elements in the directory (non-recursive)
String dirPath = @ "https: // 10.23.34.45: 6801/svn/temp"; string dirRelPath = remoteSession. makeRepositoryRootRelativePath (new Uri (dirPath); // obtain the remoteSession path of the relative directory to the configured root directory. list (dirRelPath, new EventHandler <SvnRemoteListEventArgs> (delegate (object s, SvnRemoteListEventArgs e) {// e. name: element Name // e. path: element Path // e. retrievedRevistion: Element version number }));
6.2 obtain the latest version number of the configuration Library
long latestRevision = 0;remoetSession.GetLatestRevision(out latestRevision);
6.3 obtain the submission record of the specified file
Uri fileUri = new Uri (@ "https: // 10.23.34.45: 6801/svn/temp/test.txt"); string fileRelPath = remoteSession. makeRepositoryRootRelativePath (fileUri); delegate (object s, SvnRemoteLogEventArgs e) {// e. author: Submitted by // e. revision: Version Number // e. logMessage: submit remarks }));
7 conclusion

There are almost no Demo examples provided by the SharpSvn project itself, and the documentation provided is not very easy to introduce to each API. I did not find any examples of SharpSvn on the Internet for my work. The above are only some demo examples that have been verified for use. SharpSvn is not used much more in the future, so the exploration of SharpSvn is so simple, I am too lazy to continue.

Related Article

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.