我希望使用NGit进行以下操作,但在将近一整天后我完全失去了:
master
origin
分支的最新哈希值
如果有人能告诉我这个例子,我会非常感激
答案 0 :(得分: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;
}