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

How to get list of remote branches that contain a commit in libgit2sharp

在 git 我可以 运行 命令:

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

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

我已经阅读了 libgit2sharp wiki 上的文档,但是本地分支机构的示例?

如何在 libgit2sharp 中做同样的事情?

查看 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();
}