LibGit2Sharp - 传出和传入提交的列表

时间:2018-04-23 17:20:20

标签: git libgit2sharp

我试图找到一种简单的方法来查找传出提交(我在本地提交但仍需要推送)和传入提交(我已经完成了提取并发现有在遥控器上提交,但我还没有将它们拉到我的本地副本中。)

Diff几乎做了我想要的,但我只想要提交的总数,而不是每个文件(ex 2传出提交,而不是18个修改过的文件)。

请问在LibGit2Sharp中执行此操作的最佳方法是什么?

foreach (Repos r in config.Repos)
{
    using (var repo = new Repository(r.LocalPath))
    {
        foreach (StatusEntry statusEntry in repo.RetrieveStatus())
        {
            ..... I'm able to find info about my local differences here .....
        }

        var headTree = repo.Head.Tip.Tree;
        var remoteMasterTree = repo.Branches["origin/master"].Tip.Tree;
        var diffs = new Dictionary<ChangeKind, int>();
        foreach (TreeEntryChanges treeEntryChange in repo.Diff.Compare<TreeChanges>(remoteMasterTree, headTree))
        {
            ..... this is giving me each file, but I only want "2 outgoing commits" .....
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我明白了!

    public IEnumerable<Commit> GetOutgoingCommits(Repository repo, String branchName = "master")
    {
        var branch = repo.Branches.FirstOrDefault(x => x.FriendlyName == branchName);

        if (branch.TrackingDetails.AheadBy != null)
        {
            foreach (var commit in repo.Commits.Take(branch.TrackingDetails.AheadBy.Value))
            {
                yield return commit;
            }
        }
    }