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 | Leave a comment

Testing using GitGub hosted images for WordPress.com blog posts

One of the things that has been a massive pain when writing these blog posts has been the really bad workflow that WordPress.com has to deal with image’s uploads.

The workflow that I want is one where I can take a screenshot into my local clipboard and ‘automagically’ that image is available to be inserted in the blog (I’m happy to wait a coupe seconds as long as no other action is needed).

In the past, I did come up with a ‘semi automated way’ to upload these images via WordPressweb GUI (see O2 Script to automatically upload clipboard images to WordPress.com), but it was not 100% stable and recently that O2 script stopped working since WordPress.com changed its own image uploading code .

Since I’m now heavily into Git, and GitHub, I was thinking ‘why don’t I copy the image from the clipboard into a local Git Repository, then push that repository to the web, and expose the GitHub raw link available’

And with the latest version of O2 (in .NET 4.0 + native .NET Git suport) I was able to build such Gui.

Here it is in action:

 

After taking a screenshot, click on the ‘Commit…’ link and eventually the image will be shown in a WebBrowser and the image URL is also placed in the clipboard (for example https://github.com/DinisCruz/Images/raw/master/images/3_18_2012_6_00_48_AM.jpg)

Now to add an image to a blog post (like this one), one can use the url in the clipboard, or just drag and drop the image from the WebBrowser:

Here is the code that creates this GUI

var topPanel = O2Gui.open("Util - Copy Clipboard Image to GitHub Repository",400,400);
//var topPanel = panel.clear().add_Panel();
var actionsPanel = topPanel.insert_Above(40, "Actions");
var url_TextBox = topPanel.insert_Above(20).add_Label("Image Url").append_TextBox("").align_Right();
var browser = topPanel.add_WebBrowser_Control();
topPanel.insert_LogViewer();
var localRepository = @"C:\_WorkDir\O2\Images";
var nGit = new API_NGit();
nGit.open(localRepository);
actionsPanel.add_Link("Open Screenshot tool", ()=> API_Cropper.openGui())
            .append_Link("Commit image from clipboard and push",
                        ()=>{
                               
                                var image = localRepository.pathCombine("images").createDir().saveImageFromClipboardToFolder(); 
                                if (image.notNull())
                                {
                                    var o2Timer = new O2Timer("upload to Git").start();
                                    var url = "https://github.com/DinisCruz/Images/raw/master/images/{0}".format(image.fileName());
                                    "Current Status: {0}".info(nGit.status());
                                    nGit.add_and_commit_using_Status();                               
                                    nGit.push();
                                    "Url copied to Cliboard: {0}".info(url);
                                    url.clipboardText_Set();
                                    o2Timer.stop();
                                    browser.open(url);
                                }
                            });           



return nGit;
//O2File:API_NGit.cs
//O2File:API_Cropper.cs
//O2Ref:NGit.dll
//O2Ref:NSch.dll
//O2Ref:Mono.Security.dll
//O2Ref:Sharpen.dll

//O2Tag_DontAddExtraO2Files

March 18, 2012 Posted by | .NET, GitHub | Leave a comment