OWASP O2 Platform Blog

Consuming NGit from O2 (first pass)

One of the key requirements that I had (when moving O2 from SVN to Git) was to be able to have the same auto-update workflow using Git that I had using SVN.

Until I found NGIT , I was considering that my only option to have O2 use Git for its Scripts library was to ask the users to install Git in their systems, which would be a pain since it is quite a heavy install.

But NGit changes the whole game, since NGit gives me a C# native implementation of the GIT version control system (even more interresting is the fact that NGIT is a semi-automatic port of JGit (http://eclipse.org/jgit ))

My first steps were to see if I could use this library , and using O2’s Scripting Environment, here are the initial tests I performed:

Open Repository


var testRepository = "_gitTest".tempDir(false);

var git = Git.Open(testRepository);
var repository = (Repository)git.field("repo");

return repository.Directory;

//using NGit
//using  NGit.Api;
//O2Ref:E:\Dev Tests\mono-ngit-17c0d7f\bin\NGit.dll
//O2Ref:E:\Dev Tests\mono-ngit-17c0d7f\bin\Mono.Security.dll
//O2Ref:E:\Dev Tests\mono-ngit-17c0d7f\bin\NSch.dll
//O2Ref:E:\Dev Tests\mono-ngit-17c0d7f\bin\Sharpen.dll

another option of getting the repository object


var testRepository = @"C:\_WorkDir\O2\O2_Install\GitHub.Repositories\O2.Platform.Exe";

var git = Git.Open(testRepository);
var repository = git.GetRepository();

return repository.Directory;

//using NGit
//using  NGit.Api;
//O2Ref:C:\_WorkDir\Git_O2OPlatform\_O2_Platform_Source_Code\O2.Platform.Exe\O2_Reference_Dlls\NGit.dll
//O2Ref:C:\_WorkDir\Git_O2OPlatform\_O2_Platform_Source_Code\O2.Platform.Exe\O2_Reference_Dlls\Mono.Security.dll
//O2Ref:C:\_WorkDir\Git_O2OPlatform\_O2_Platform_Source_Code\O2.Platform.Exe\O2_Reference_Dlls\NSch.dll
//O2Ref:C:\_WorkDir\Git_O2OPlatform\_O2_Platform_Source_Code\O2.Platform.Exe\O2_Reference_Dlls\Sharpen.dll

adding a file


var testRepository = "_gitTest".tempDir(false);
var git = Git.Open(testRepository);

"some text".saveAs(testRepository.pathCombine("a file.txt"));

git.Add().AddFilepattern(".").Call();
return git.Status().Call().GetAdded();

Commit a File


var testRepository = "_gitTest".tempDir(false);
var git = Git.Open(testRepository);

"some text".saveAs(testRepository.pathCombine("a file.txt"));

git.Add().AddFilepattern(".").Call();

git.Commit().SetMessage("test Commit").Call();
return git.Status().Call().GetAdded();


<strong>clone a repository</strong>
var tempDir = "_cloneTest".tempDir().info();

var command = Git.CloneRepository();
command.SetDirectory(tempDir);
command.SetURI("git://github.com/o2platform/Scripts-by-O2-Users.git");
var git = command.Call();
var repository = git.GetRepository();
return repository;

cloning O2 Scripts GitHub repository  (in 13 seconds)


var tempDir = "_O2_Scripts".tempDir(false).info();

var command = Git.CloneRepository();
command.SetDirectory(tempDir);
command.SetURI("git://github.com/o2platform/O2-Platform-Scripts.git");
var o2Timer = new O2Timer("Repository clone").start();
var git = command.Call();
o2Timer.stop();
files in checked out repository
var git = Git.Open(@"E:\O2_Tests\tmp_Folder\_cloneTest_tmpD053");

var repository = git.GetRepository();

return repository.WorkTree.List();

pull updates

var git = Git.Open(@"E:\O2_Tests\tmp_Folder\_cloneTest_tmpD053");

return git.Pull().Call().GetFetchResult().GetTrackingRefUpdates();
push into remote

var result = Git.Open(@"E:\O2_Tests\tmp_Folder\_O2_Scripts")
.Push().Call();

Viewing Push Status

var pushCommand = Git.Open(@"E:\O2_Tests\tmp_Folder\_O2_Scripts")
.Push();
var pushResults = pushCommand.Call();

foreach(var pushResult in pushResults)
{
var remoteUpdates = pushResult.GetRemoteUpdates();
foreach(var remoteUpdate in remoteUpdates)
{
"status:{0}".debug(remoteUpdate.GetStatus());
}

}

pull with progress text

var git = Git.Open(testRepository);
var repository = git.GetRepository();
var stringWriter = new StringWriter();
var textMonitor = new  TextProgressMonitor(stringWriter);

var pullCommand= git.Pull();
pullCommand.SetProgressMonitor(textMonitor);
var pullResponse = pullCommand.Call();
return stringWriter.str() + " \n\n.........\n\n " + pullResponse.str();

March 18, 2012 - Posted by | GitHub

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: