SVN Client API的.net 接口 SharpSvn介紹 Checkout操作实例

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.

 ShowLog操作

 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 = "你的帳號";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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(<a href="https://url">https://url</a>), logArgs, logHandler);
                this.txtLog.Text += DateTime.Now.ToLongTimeString() + "\r\n";
            }

 

 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);

 

 

}

posted @ 2012-11-01 17:03  PointNet  阅读(11030)  评论(0编辑  收藏  举报