SVN Client API的.net 接口 SharpSvn介紹

Subversion是一個文件版本管理工具, 廣泛的被大家採用來作為源代碼版本管理.

已有的工具不管是其自帶的命令行工具還是Windows UI的tortoiseSVN等還是很方便實用的, 但是如果想跟已有的系統整合的話,除了用其內建的Hook Script功能外,必然要使用SVN的API,這個API是用C寫的, 所以對於其他開發語言來說如java, C#等使用起來不方便.

於是就有了SVN Client的java實現,或者是用其他語言對C接口的DLL包裝了一層的代碼(參考SWIG),這樣我們就可以方便的使用其他語言來與SVN repository 進行溝通了

SharpSvn就是 .net平台的一個SVN API的實現, 其被廣泛採用到AnkhSVN 等工具中, 可以被用來擴展為自定義的訪問SVN的Client, 或者用來跟其他系統如Bug/CR追踪等進行整合.

 

下面是一位網友整理的一個簡單的指南,類似hello world, 告訴我們如何開始使用Sharp SVN

The SharpSvn library basically gives you a .NET interface to all the operations that you would normally perform through a tool like TortoiseSVN.

I found myself needing this exact library while writing a tool that changes files that have been checked out from SVN.

The problem with manipulating files that are under SVN is that you need to be careful about renaming files (and sometimes even deleting). If you don’t do it through the SVN api then you will end up with duplicates files/folders in SVN since SVN thinks that it’s a new file.

To solve this I finally got a chance to crack open the SharpSVN library which is used by my favourite AnkhSVN.

1. Download the latest library from http://sharpsvn.open.collab.net/. You have to pick between either 1.5 or 1.6. I went with 1.6 and didn’t run into any issues. I think this is based on the version of the SVN server that your connecting to.

2. In your Visual Studio project add a reference to the following assemblies.
- SharpSvn.dll
- SharpSvn.UI.dll (Only needed if you need the UI to prompt for login)

3. If like me your building on a 64 bit OS and you want your app to run on a 32 bit OS, make sure the project that references the SharpSvn.dll is set to Build for the x86 Platform. (Build –> Configuration Manager – Solution Platform)

4. Write your code using the SvnClient object. Here are some samples from the SharpSvn Wiki and some that I wrote.

原文鏈接

 

CheckOut 代碼範例.

下面的例子用來連接使用https的web dav作為接口的SVN database, 其他的接入方式也差不多.

using (SvnClient client = new SvnClient())
            {
                //client.Authentication.Clear();
                client.Authentication.UserNamePasswordHandlers += new EventHandler<SharpSvn.Security.SvnUserNamePasswordEventArgs>(
                delegate(Object s, SharpSvn.Security.SvnUserNamePasswordEventArgs ee)
                {
                    ee.UserName = "你的帳號";
                    ee.Password = "你的密碼";
                });

                client.Authentication.SslServerTrustHandlers+= new EventHandler<SharpSvn.Security.SvnSslServerTrustEventArgs>(
                delegate(Object ssender, SharpSvn.Security.SvnSslServerTrustEventArgs se)
                {
                  // Look at the rest of the arguments of E whether you wish to accept

                  // If accept:
                  se.AcceptedFailures = se.Failures;
                  se.Save = true; // Save acceptance to authentication store
                });


                client.CheckOut(
                  new Uri("https://yourSVN-Server:8443/svn/prd/UTL/trunk/ExcelPool"), //SVN repository url
                  @"c:\wc");                                                                                                          //local direcotory

            }


 

Add new files to the working copy

using(SvnClient client = new SvnClient())
{
  SvnAddArgs args = new SvnAddArgs();
  // TODO: Set optional settings on args

  client.Add(@"C:\file\in\workingcopy", args);
}

 

Show Log

using (SvnClient client = new SvnClient())
            {
                //client.Authentication.Clear();
                client.Authentication.UserNamePasswordHandlers += new EventHandler<SharpSvn.Security.SvnUserNamePasswordEventArgs>(
                delegate(Object s, SharpSvn.Security.SvnUserNamePasswordEventArgs ee)
                {
                    ee.UserName = "你的帳號";
                    ee.Password = "你的密碼";
                });

                client.Authentication.SslServerTrustHandlers += new EventHandler<SharpSvn.Security.SvnSslServerTrustEventArgs>(
                delegate(Object ssender, SharpSvn.Security.SvnSslServerTrustEventArgs se)
                {
                    // Look at the rest of the arguments of E whether you wish to accept

                    // If accept:
                    se.AcceptedFailures = se.Failures;
                    se.Save = true; // Save acceptance to authentication store
                });

                this.txtLog.Text += DateTime.Now.ToLongTimeString() + "\r\n";
                SvnLogArgs logArgs = new SvnLogArgs();
                logArgs.Range = new SvnRevisionRange(long.Parse(this.txtRevisionFrom.Text), long.Parse(this.txtRevisionTo.Text));
                logArgs.RetrieveAllProperties = true;

                EventHandler<SvnLogEventArgs> logHandler = new EventHandler<SvnLogEventArgs>(delegate(object lo, SvnLogEventArgs le)
                {
                    foreach (SvnChangeItem changeItem in le.ChangedPaths)
                    {
                        this.txtLog.Text += string.Format(
                                                    "{0} {1} {2} {3} {4}\r{5} {6}",
                                                    changeItem.Action,
                                                    changeItem.Path,
                                                    changeItem.CopyFromRevision,
                                                    changeItem.CopyFromPath,
                                                    le.Author,
                                                    le.LogMessage,
                                                    le.Revision);

                    }
                });

                client.Log(new Uri(https://url), logArgs, logHandler);
                this.txtLog.Text += DateTime.Now.ToLongTimeString() + "\r\n";
            }


 

 

SharpSVN

SharpSvn is a binding of the Subversion Client API for .Net 2.0-4.0+ applications contained within a set of xcopy-deployable dll's. Notable users of this api are (at this time):

 

這裡有更多範例
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值