如何查找具有特定父级的所有提交? [英] How to find all commits having a particular parent?

查看:133
本文介绍了如何查找具有特定父级的所有提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在存储库中找到具有特定父级的所有提交?



例如,如果我有提交 A ,我想找到与 A 共享父项的所有其他提交。在LibGit2Sharp中这样做最有效,也就是性能却正确吗?



Git对象允许检索提交的父项。但是,没有简单的方法来查找提交的子代。



然而,下面的代码会解决这个问题。这个想法是从仓库的所有引用(头,标签,...)执行一个 git log ,并且沿途选择每个承载父代的提交与请求的SHA进行比较。



由于最近一次的提交是沿着祖先路径完成的,因此如果您要搜索孩子,可能需要一些时间一个非常早的提交在一个非常大的历史和许多分支的存储库。

  [事实] 
public void CanRetrieveChildrenOfASpecificCommit()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
using(var repo = new Repository(path.RepositoryPath))
{
const string parentSha =5b5b025afb0b4c913b4c338a42934a3863bf3644;

var filter = new Filter
{
/ *所有参考文献中的Revwalk(git log --all)... * /
Since = repo.Refs ,

/ * ...并在到达父母时停止* /
直至= parentSha
};

var commits = repo.Commits.QueryBy(filter);

var children = from c in提交
from c.Parents
let pId = p.Id
其中pId.Sha == parentSha
选择c;

var expectedChildren = new [] {c47800c7266a2be04c571c04d5a6614691ea99bd,
4a202b346bb0fb0db7eff3cffeb3c70babbd2045};

Assert.Equal(expectedChildren,children.Select(c => c.Id.Sha));


限制:




  • 由于LibGit2Sharp没有公开访问reflog的方法,因此它不会检索已被重写的提交(例如通过修改或重新绑定) (尚未)


  • $ b 无法到达(悬空)提交不会被检索到。 p> 测试存储库



    正在查询的存储库内容显示如下

      $ git log --all --graph 
    * commit 4c062a6361ae6959e06292c1fa5e2822d9c96345
    |作者:gor< gorbach.alexey@gmail.com>
    |日期:四月14日星期四18:44:16 2011 +0300
    |
    |目录添加了
    |
    * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644
    | \合并:9fd738e c47800c
    | |作者:Scott Chacon< schacon@gmail.com>
    | |日期:5月25日星期二11:58:27 2010 -0700
    | |
    | |合并分支'br2'
    | |
    | | * commit e90810b8df3e80c413d903f631643c716887138d
    | | |作者:Vicent Marti< tanoku@gmail.com>
    | | |日期:八月五日星期四18:42:20 2010 +0200
    | | |
    | | |测试提交2
    | | |
    | | * commit 6dcf9bf7541ee10456529833502442f385010c3d
    | |作者:Vicent Marti< tanoku@gmail.com>
    | |日期:八月五日星期四18:41:33 2010 +0200
    | |
    | |测试提交1
    | |
    | | * commit a4a7dce85cf63874e984719f4fdd239f5145052f
    | | | \ Merge:c47800c 9fd738e
    | | //作者:Scott Chacon< schacon@gmail.com>
    | | /日期:5月25日星期二12:00:23 2010 -0700
    | | /
    | / |将分支'master'合并为br2
    | |
    * |提交9fd738e8f7967c078dceed8190330fc8648ee56a
    | |作者:Scott Chacon< schacon@gmail.com>
    | |日期:2010年5月24日星期一10:19:19 2010 -0700
    | |
    | |第四次提交
    | |
    * |提交4a202b346bb0fb0db7eff3cffeb3c70babbd2045
    | |作者:Scott Chacon< schacon@gmail.com>
    | |日期:星期一5月24日10:19:04 2010-0700
    | |
    | |第三次提交
    | |
    | * commit c47800c7266a2be04c571c04d5a6614691ea99bd
    | /作者:Scott Chacon< schacon@gmail.com>
    |日期:5月25日星期二11:58:14 2010 -0700
    |
    |分支提交一个
    |
    * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
    |作者:Scott Chacon< schacon@gmail.com>
    |日期:星期二5月11日13:38:42 2010-0700
    |
    |另一个提交
    |
    * commit 8496071c1b46c854b31185ea97743be6a8774479
    作者:Scott Chacon< schacon@gmail.com>
    日期:5月8日星期六16:13:06 2010 -0700

    测试

    *提交41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9
    |作者:Scott Chacon< schacon@gmail.com>
    |日期:星期二5月11日13:40:41 2010-0700
    |
    |打包提交两个
    |
    * commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5
    作者:Scott Chacon< schacon@gmail.com>
    日期:星期二5月11日13:40:23 2010 -0700

    打包提交一个


    How may I find all commits in a repository that have a specific parent?

    For example, if I have a commit A, I would like to find all other commits that share the parent with A. What would be the most effective, i.e. performant yet correct way to do this in LibGit2Sharp?

    解决方案

    That's a tricky question ;-)

    The Git object allows to retrieve the parents of a commit. However, there's no easy way to find the children of a commit.

    The following code would however partially solve this. The idea is to perform a git log from all the references of the repository (heads, tags, ...) and, along the way, select every commit which bear a parent with the requested SHA.

    As the walk is being done from the most recent commits down the ancestor path, it may take some time, if you're searching for the children of a very early commit in a repository with a very large history and many branches.

    [Fact]
    public void CanRetrieveChildrenOfASpecificCommit()
    {
        TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
        using (var repo = new Repository(path.RepositoryPath))
        {
            const string parentSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644";
    
            var filter = new Filter
                             {
                                 /* Revwalk from all the refs (git log --all) ... */
                                 Since = repo.Refs, 
    
                                 /* ... and stop when the parent is reached */
                                 Until = parentSha
                             };
    
            var commits = repo.Commits.QueryBy(filter);
    
            var children = from c in commits
                        from p in c.Parents
                        let pId = p.Id
                        where pId.Sha == parentSha
                        select c;
    
            var expectedChildren = new[] { "c47800c7266a2be04c571c04d5a6614691ea99bd", 
                                            "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" };
    
            Assert.Equal(expectedChildren, children.Select(c => c.Id.Sha));
        }
    }
    

    Limits:

    • This will not retrieve commits that have been rewritten (through amend or rebase, for instance) as LibGit2Sharp doesn't expose a way to access the reflog (yet)
    • Unreachable (dangling) commits won't either be retrieved with this proposal.

    Test repository:

    The content of the repository being queried is show below

    $ git log --all --graph
    * commit 4c062a6361ae6959e06292c1fa5e2822d9c96345
    | Author: gor <gorbach.alexey@gmail.com>
    | Date:   Thu Apr 14 18:44:16 2011 +0300
    |
    |     directory was added
    |
    *   commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644
    |\  Merge: 9fd738e c47800c
    | | Author: Scott Chacon <schacon@gmail.com>
    | | Date:   Tue May 25 11:58:27 2010 -0700
    | |
    | |     Merge branch 'br2'
    | |
    | | * commit e90810b8df3e80c413d903f631643c716887138d
    | | | Author: Vicent Marti <tanoku@gmail.com>
    | | | Date:   Thu Aug 5 18:42:20 2010 +0200
    | | |
    | | |     Test commit 2
    | | |
    | | * commit 6dcf9bf7541ee10456529833502442f385010c3d
    | |   Author: Vicent Marti <tanoku@gmail.com>
    | |   Date:   Thu Aug 5 18:41:33 2010 +0200
    | |
    | |       Test commit 1
    | |
    | | *   commit a4a7dce85cf63874e984719f4fdd239f5145052f
    | | |\  Merge: c47800c 9fd738e
    | |/ /  Author: Scott Chacon <schacon@gmail.com>
    | | /   Date:   Tue May 25 12:00:23 2010 -0700
    | |/
    |/|         Merge branch 'master' into br2
    | |
    * | commit 9fd738e8f7967c078dceed8190330fc8648ee56a
    | | Author: Scott Chacon <schacon@gmail.com>
    | | Date:   Mon May 24 10:19:19 2010 -0700
    | |
    | |     a fourth commit
    | |
    * | commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
    | | Author: Scott Chacon <schacon@gmail.com>
    | | Date:   Mon May 24 10:19:04 2010 -0700
    | |
    | |     a third commit
    | |
    | * commit c47800c7266a2be04c571c04d5a6614691ea99bd
    |/  Author: Scott Chacon <schacon@gmail.com>
    |   Date:   Tue May 25 11:58:14 2010 -0700
    |
    |       branch commit one
    |
    * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
    | Author: Scott Chacon <schacon@gmail.com>
    | Date:   Tue May 11 13:38:42 2010 -0700
    |
    |     another commit
    |
    * commit 8496071c1b46c854b31185ea97743be6a8774479
      Author: Scott Chacon <schacon@gmail.com>
      Date:   Sat May 8 16:13:06 2010 -0700
    
          testing
    
    * commit 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9
    | Author: Scott Chacon <schacon@gmail.com>
    | Date:   Tue May 11 13:40:41 2010 -0700
    |
    |     packed commit two
    |
    * commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5
      Author: Scott Chacon <schacon@gmail.com>
      Date:   Tue May 11 13:40:23 2010 -0700
    
          packed commit one
    

    这篇关于如何查找具有特定父级的所有提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆