Created
December 9, 2011 04:00
-
-
Save ishisaka/1450092 to your computer and use it in GitHub Desktop.
sharpSVN Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Collections.ObjectModel; | |
using SharpSvn; | |
namespace SVNSample | |
{ | |
/// <summary> | |
/// SharpSVNのサンプル | |
/// </summary> | |
class Program | |
{ | |
private static SvnClient client; //Subversion Client | |
static void Main(string[] args) { | |
if (args.Length <= 0) { | |
Console.WriteLine("Usage: SVNSample [Repo URI] [Work Directory]"); | |
} | |
client = new SvnClient(); | |
//イベント設定 | |
client.Notify += new EventHandler<SvnNotifyEventArgs>(client_Notify); | |
try { | |
//資格情報を作成してクライアントに登録する。(ネットワーク越しで認証が必要なケース) | |
//var cred = new System.Net.NetworkCredential("ishisaka", ""); | |
//client.Authentication.DefaultCredentials = cred; | |
//ワーキングディレクトリの状態によってチェックアウトもしくはアップデートを行う。 | |
CheckoutOrUpdate(args[0], args[1]); | |
//listコマンドの実行 | |
Console.WriteLine("+++ Repo Directory / File List +++"); | |
GetRepoList(new Uri(args[0])); | |
//テスト用のファイルの作成 | |
Console.WriteLine("+++ File Cleate \"hello[yyyymmddhhmmss].py\" +++"); | |
var contents = "print 'Hello py'\n"; | |
var dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); | |
var filePath = args[1] + "\\trunk\\hello" + dateStr + ".py"; | |
File.WriteAllText(filePath, contents); | |
//ADD | |
Console.WriteLine("+++ File Add +++"); | |
client.Add(filePath); | |
//ステータス | |
Console.WriteLine("+++ Status +++"); | |
PrintStatus(args[1]); | |
//コミット | |
Console.WriteLine("+++ Commit +++"); | |
Commit(args[1]); | |
Console.Write("何か入力で終了:"); | |
Console.Read(); | |
} | |
finally { | |
client.Dispose(); | |
} | |
} | |
private static void Commit(string workingDir) { | |
SvnCommitArgs ca = new SvnCommitArgs(); | |
ca.LogMessage = "ファイルを追加"; | |
SvnCommitResult results; | |
client.Commit(workingDir, ca, out results); | |
Console.WriteLine("Commit Time :{0}", results.Time); | |
Console.WriteLine("Commit Author :{0}", results.Author); | |
Console.WriteLine("Commit Revision :{0}", results.Revision); | |
} | |
private static void PrintStatus(string workingDir) { | |
//ステータスを取得する | |
Collection<SvnStatusEventArgs> stats; | |
client.GetStatus(workingDir, out stats); | |
foreach (var s in stats) { | |
Console.WriteLine("File :{0}, Last change time :{1}, Staus :{2}", | |
s.FullPath, s.LastChangeTime, s.LocalContentStatus); | |
} | |
} | |
private static void CheckoutOrUpdate(string repo, string dir) { | |
//チェックアウト | |
string svnDir = @".svn"; | |
if (Directory.Exists(dir + "\\" + svnDir)) { | |
//.svnディレクトリが存在したらチェックアウト済みと見なし、アップデートする。 | |
Console.WriteLine("++ Working Directory Update ++"); | |
client.Update(dir); | |
} | |
else { | |
//チェックアウトする。 | |
Console.WriteLine("++ CheckOut ++"); | |
client.CheckOut(new Uri(repo), dir); | |
} | |
} | |
private static void GetRepoList(Uri url) { | |
//Collectionの用意 | |
Collection<SvnListEventArgs> list; | |
string myPath = url.LocalPath; | |
//ターゲットの作成 | |
SvnUriTarget repo = new SvnUriTarget(url); | |
//listコマンドの実行 | |
client.GetList(repo, out list); | |
foreach (var l in list) { | |
//レポジトリのルートディレクトリは無視する。 | |
if (l.Uri.LocalPath == l.RepositoryRoot.LocalPath) continue; | |
switch (l.Entry.NodeKind) { | |
case SvnNodeKind.Directory: | |
if (l.Uri.LocalPath != myPath) { | |
Console.WriteLine("Folder\t:{0}", l.Uri.LocalPath); | |
GetRepoList(l.Uri); | |
} | |
break; | |
case SvnNodeKind.File: | |
Console.WriteLine("\tFile\t:{0}", Path.GetFileName(l.Uri.LocalPath)); | |
break; | |
} | |
} | |
list.Clear(); | |
} | |
private static void client_Notify(object sender, SvnNotifyEventArgs e) { | |
switch (e.CommandType) { | |
//チェックアウト/アップデート中のファイルを表示する | |
case SvnCommandType.CheckOut: | |
case SvnCommandType.Update: | |
case SvnCommandType.Add: | |
case SvnCommandType.Commit: | |
Console.WriteLine(e.FullPath + ", Rev. " + e.Revision + "\t : " + e.Action); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment