2010年4月18日日曜日

[C#] SharpSVNを使ってSubversionを操作してみる

SharpSVNはSubvertionの.Net用クライアントライブラリ。 それを使って、ローカルのファイルを更新する処理のメモです。 ざっくりコードを乗せてみます。
  1. using (SvnClient client = new SvnClient())  
  2. {  
  3.   
  4.     //サーバー上のリポジトリ位置の設定  
  5.     SvnUriTarget repos = new SvnUriTarget(new Uri("file:///C:/svn_repository/SvnTest/"));  
  6.     //クライアントのファイル位置を設定  
  7.     SvnPathTarget local = new SvnPathTarget(@"C:\source\subvertionTest");  
  8.   
  9.     //両方の情報を取得  
  10.     SvnInfoEventArgs serverInfo;  
  11.     SvnInfoEventArgs clientInfo;  
  12.     client.GetInfo(repos, out serverInfo);  
  13.     client.GetInfo(local, out clientInfo);  
  14.   
  15.     //リビジョンを比較  
  16.     if (serverInfo.Revision != clientInfo.Revision)  
  17.     {  
  18.         SvnUpdateResult ret;  
  19.         //クライアントのファイルを最新に更新  
  20.         client.Update(@"C:\source\subvertionTest"out ret);  
  21.     }  
  22.   
  23.     //ファイルの一覧取得用引数の指定  
  24.     SvnListArgs args = new SvnListArgs();  
  25.     //リビジョンを指定  
  26.     args.Revision = 2;  
  27.     //処理結果格納変数を定義  
  28.     System.Collections.ObjectModel.Collection<svnlisteventargs> list;  
  29.     //一覧の取得を実行  
  30.     client.GetList(repos, out list);  
  31.   
  32.     //処理結果の表示  
  33.     foreach (var i in list)  
  34.     {  
  35.         switch (i.Entry.NodeKind)  
  36.         {  
  37.             case SvnNodeKind.Directory:  
  38.                 Console.WriteLine("Directory:{0}",i.EntryUri.LocalPath);  
  39.                 break;  
  40.             case SvnNodeKind.File:  
  41.                 Console.WriteLine("File:{0}",i.EntryUri.LocalPath);  
  42.                 break;  
  43.         }  
  44.     }  
  45. }  
  46. </svnlisteventargs>