LibGit2Sharp实现showbranch独立

时间:2017-08-02 19:09:29

标签: c# git libgit2 libgit2sharp

我正在尝试使用LibGit2Sharp来重新创建combobox的功能,根据docs执行此操作:git show-brach --independent

到目前为止,我最好的尝试如下:

Among the <reference>s given, display only the ones that cannot be reached from any other <reference>.

不幸的是,随着历史数量的增加,这变得天文数字变慢。实际上,直接执行git,解析输出,并让LibGit2Sharp查找生成的SHA比使用上面的代码要快得多。我认为这与Git有一些优化有关,但LibGit2却没有。这甚至做我想要的吗?如果是这样,有没有更好的方法在LibGit2Sharp中实现这一目标?

1 个答案:

答案 0 :(得分:1)

我终于找到了一种利用合并基础的更好方法,这要归功于this question指向我正确的方向。

这是新代码:

    /// <summary>
    /// Implementation of `git show-branch --indepenent`
    /// 
    /// "Among the <reference>s given, display only the ones that cannot be reached from any other <reference>"
    /// </summary>
    /// <param name="commitsToCheck"></param>
    /// <returns></returns>
    private List<Commit> GetIndependent(IRepository repo, IEnumerable<Commit> commitsToCheck)
    {
        var commitList = commitsToCheck.ToList();

        for (var i = commitList.Count - 1; i > 0; --i)
        {
            var first = commitList[i];
            for (var j = commitList.Count - 1; j >= 0; --j)
            {
                if (i == j) continue;

                var second = commitList[j];

                var mergeBase = repo.ObjectDatabase.FindMergeBase(first, second);

                if (first.Equals(mergeBase))
                {
                    // First commit (i) is reachable from second (j), so drop i
                    commitList.RemoveAt(i);

                    // No reason to check anything else against this commit
                    j = -1;
                } else if (second.Equals(mergeBase))
                {
                    // Second (j) is reachable from first, so drop j
                    commitList.RemoveAt(j);

                    // If this was at a lower index than i, dec i since we shifted one down
                    if (j < i)
                    {
                        --i;
                    }
                }
            }
        }

        return commitList;
    }
相关问题