C# showbranch的LibGit2Sharp实现
我试图使用LibGit2Sharp重新创建git show brach--independent的功能,根据下面的说明:C# showbranch的LibGit2Sharp实现,c#,git,libgit2,libgit2sharp,C#,Git,Libgit2,Libgit2sharp,我试图使用LibGit2Sharp重新创建git show brach--independent的功能,根据下面的说明:在给定的s中,只显示其他任何人无法访问的那些。 到目前为止,我的最佳尝试如下: List<Commit> GetIndependent(IRepository repo, IEnumerable<Commit> commits) { var indep = new List<Commit>();
在给定的s中,只显示其他任何人无法访问的那些。
到目前为止,我的最佳尝试如下:
List<Commit> GetIndependent(IRepository repo, IEnumerable<Commit> commits)
{
var indep = new List<Commit>();
foreach (var commit in commits)
{
if (repo.Commits.QueryBy(new CommitFilter
{
FirstParentOnly = false,
IncludeReachableFrom = commit,
ExcludeReachableFrom = commits.Where(x => x.Equals(commit) == false)
}).Any())
{
indep.Add(commit);
}
}
return indep;
}
List GetIndependent(IRepository repo,IEnumerable提交)
{
var indep=新列表();
foreach(提交中的var commit)
{
if(repo.Commits.QueryBy)(新提交过滤器
{
FirstParentOnly=false,
IncludeReachableFrom=提交,
ExcludeReachableFrom=提交。其中(x=>x.Equals(提交)==false)
}).Any())
{
indep.Add(提交);
}
}
返回索引;
}
不幸的是,随着历史的增加,这一过程变得极其缓慢。对我来说,直接执行git、解析输出并让LibGit2Sharp查找结果SHAs比使用上述代码快得多。我假设这与Git进行的一些优化有关,而LibGit2没有。这是我想要的吗?如果是这样的话,在LibGit2Sharp中有没有更好的方法来实现这一点?我终于找到了一种更好的方法来利用合并基,这要感谢我指出了正确的方向 以下是新代码:
/// <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;
}
//
///'git show branch--independent的实现`
///
///在给定的中,仅显示其他用户无法访问的
///
///
///
私有列表GetIndependent(IRepository repo、IEnumerable Commits ToCheck)
{
var commitList=commitsToCheck.ToList();
对于(var i=commitList.Count-1;i>0;--i)
{
var first=委托列表[i];
对于(var j=commitList.Count-1;j>=0;--j)
{
如果(i==j)继续;
var second=委托列表[j];
var mergeBase=repo.ObjectDatabase.FindMergeBase(第一,第二);
if(第一个等于(合并基))
{
//第一次提交(i)可以从第二次提交(j)到达,所以删除i
删除委员会名单(i);
//没有理由根据此提交检查任何其他内容
j=-1;
}else if(second.Equals(mergeBase))
{
//第二个(j)可以从第一个到达,所以放下j
委员会名单删除(j);
//如果这是在一个比我低的指数,12月1日,因为我们把一
if(j