如何获取libgit2sharp中包含提交的远程分支列表

时间:2017-02-10 10:16:36

标签: c# git libgit2sharp

在git中我可以运行命令:

git branch -r --contains '#commit-hash#'

其中列出了我感兴趣的提交的远程分支。

我已经阅读了libgit2sharp wiki上的文档但是那里有本地分支的例子?

我怎样才能在 libgit2sharp 中做同样的事情?

1 个答案:

答案 0 :(得分:2)

查看libgit2wiki文档后,您可以将其示例代码修改为:

using (var repo = new Repository("path/to/your/repo"))
{
    const string commitSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644";
    foreach(Branch b in ListBranchesContainingCommit(repo, commitSha))
    {
        Console.WriteLine(b.Name);
    }
}

private IEnumerable<Branch> ListBranchesContainingCommit(Repository repo, string commitSha)
{
    var commit = repo.Lookup<Commit>(commitSha);var commit = repo.Lookup<Commit>(commitSha);

    IEnumerable<Reference> headsContainingTheCommit = repo.Refs.ReachableFrom(repo.Refs, new[] {commit});
    return headsContainingTheCommit.Select(branchRef => repo.Branches[branchRef.CanonicalName]).ToList();
}