在Ancestry Path找到所有合并

时间:2015-11-02 12:23:22

标签: c# libgit2 libgit2sharp

在Git中,我可以通过

获得所有需要的提交
  

git log hash..master --ancestry-path --merges

在LibGit2Sharp中是否有相同的功能?

1 个答案:

答案 0 :(得分:1)

首先是一些基础知识:

  

git log topic..master

var filter = new CommitFilter { 
    SortBy = CommitSortStrategies.Time,
    Since = master,
    Until = topic,
};
var commitList = repo.Commits.QueryBy (filter);
  

git log topic..master --merges

var filter = new CommitFilter { 
    SortBy = CommitSortStrategies.Time,
    Since = master,
    Until = topic,
};
var commitList = repo.Commits.QueryBy (filter);
var mergeList = commitList.Where (p => p.Parents.Count () >= 2);

现在问你的问题:

git log topic..master --ancestry-path --merges

  

ancestry-path =

       When given a range of commits to display (e.g.  commit1..commit2 or
       commit2 ^commit1), only display commits that exist directly on the
       ancestry chain between the commit1 and commit2, i.e. commits that
       are both descendants of commit1, and ancestors of commit2.

一旦我们拥有祖先路径,-merges就很容易,因为我们可以过滤具有多个父级的提交。获得祖先路径需要一些编码; - )

由于上面第一个示例中从ICommitLog返回的commitList包含通过.Parents属性的DAG(有向无环图),我们需要走图表并获取“所有简单路径“通过深度优先搜索来查找所有非循环路径。一旦有了所有simple路径的列表,只需过滤掉哪些提交有>=2个父项。

注意:我已经在一些C#项目中完成了这项工作,甚至是简单的,例如计算与特定提交相关的pull-request使用这种深度优先的祖先。我倾向于远离Linq来执行此操作,因为我有huge提交列表(在我搜索的子DAG中的开始到结束节点之间的100k +节点)并且还避免了由于堆栈大小而导致的递归方法但是用例可能/会有所不同。如果此时遇到问题,请发布您的算法/代码问题。

相关问题