0

I am looking to do the following using NGit, but after nearly a full day am completely lost:

  • Create an empty repo
  • Add a remote "origin" using a URL and credentials
  • Run Ls-Remote to get the latest hashes of the master branch on origin

If anyone could show me an example of this in action I'd greatly appreciate it

Doug
  • 6,460
  • 5
  • 59
  • 83

1 Answers1

1
using System.Collections.Generic;
using System.Linq;
using NGit.Api;
using Sharpen;

// git init
string path = @"C:\git\repo1";
Git git = Git.Init().SetDirectory(new FilePath(path)).Call();
// after init, you can call the below line to open
// Git git = Git.Open(new FilePath(path));

// git remote origin
StoredConfig config = git.GetRepository().GetConfig();
config.SetString("remote", "origin", "url", @"http://user:password@github.com/user/repo1.git");
config.Save();

// git ls-remote
ICollection<Ref> refs = git.LsRemote().SetRemote("origin").Call();
Ref master = refs.FirstOrDefault(a => a.GetName() == "refs/heads/master");
if (master != null)
{
    string hash = master.GetObjectId().Name;
}
linquize
  • 19,828
  • 10
  • 59
  • 83
  • Thanks for answering! This appears to not show any refs for my repo though (its definitely got refs as a `git ls-remote origin` works fine in the command prompt. Am I doing something wrong? Also, on a side note, how would you do with this github if you are using a private key? – Doug Dec 05 '12 at 11:15
  • have you pushed master branch to github? – linquize Dec 05 '12 at 12:15