gpt4 book ai didi

git - 如何找到具有特定父级的所有提交?

转载 作者:太空狗 更新时间:2023-10-29 13:41:37 25 4
gpt4 key购买 nike

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

例如,如果我有一个提交 A,我想找到与 A 共享父级的所有其他提交。什么是最有效的,即在 LibGit2Sharp 中执行此操作的最有效但正确的方法?

最佳答案

这是个棘手的问题 ;-)

Git 对象允许检索提交的父级。但是,没有简单的方法可以找到提交的子项。

不过,下面的代码可以部分解决这个问题。这个想法是从存储库的所有引用(heads、tags、...)执行 git log,并在此过程中选择每个带有所请求 SHA 的父项的提交。

由于遍历是从最近的提交沿着祖先路径完成的,如果您要在具有非常长的历史记录和许多分支的存储库中搜索非常早提交的子项,则可能需要一些时间。

[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));
}
}

限制:

  • 这不会检索已重写的提交(例如,通过修改或 rebase ),因为 LibGit2Sharp 尚未公开访问 reflog 的方法(目前)
  • 此提案不会检索无法访问的(悬空的)提交。

测试库:

查询到的版本库内容如下图

$ 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

关于git - 如何找到具有特定父级的所有提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10539918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com