0

我希望使用 NGit 执行以下操作,但经过将近一整天的时间,我完全迷失了:

  • 创建一个空仓库
  • 使用 URL 和凭据添加远程“来源”
  • 运行 Ls-Remote 以获取master分支的最新哈希值origin

如果有人可以向我展示一个这样的例子,我将不胜感激

4

1 回答 1

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;
}
于 2012-12-03T07:13:48.993 回答