[英]Is there a way to get the number of children of a git commit node?
I'd like to find the nth node in the commit graph from where it has 2 or more children.我想在提交图中找到第 n 个节点,从那里它有 2 个或更多的孩子。 So if the history was:所以如果历史是:
A -- B -- C -- D
\
E -- F*
And I am at F
, I'd like to find B
if I was searching for the 1st node with more than one child.而且我在F
,如果我正在搜索有多个孩子的第一个节点,我想找到B
。
How would I accomplish this?我将如何做到这一点?
All of Git's internal linkages point backwards, from child to parent: Git 的所有内部链接都向后指向,从子级到父级:
A <-B <-C <-D
↖︎ [this is an up-and-left arrow, but it may not work in some fonts]
E <-F
That said, git rev-list
has the ability to reverse the links while walking the graph.也就是说, git rev-list
能够在遍历图形时反转链接。 So you can walk the graph from F
backwards, reversing each link so as to build a list of children.因此,您可以从F
向后遍历图形,反转每个链接以构建子项列表。 The problem here is that Git only sees the links it walks:这里的问题是 Git 只能看到它所走的链接:
A-> B <-C <-D
↘︎ [this is a down-and-right arrow]
E-> F
This means that even using the git rev-list
option that does this ( --children
), Git will claim that B
has one child, namely E
.这意味着即使使用执行此操作的git rev-list
选项( --children
),Git 也会声称B
有一个孩子,即E
。 To get Git to claim that B
has two children, E
and C
, you must start from both F
and D
so that git rev-list
walks back from D
to C
to B
:要让 Git 声明B
有两个孩子, E
和C
,您必须从F
和D
开始,以便git rev-list
从D
返回到C
再到B
:
A-> B-> C-> D
↘︎ [this is a down-and-right arrow]
E-> F
You can get Git to start at all branch names using --branches
, or all tag names using --tags
, or all references (including things like refs/stash
) using --all
.你可以得到Git的使用在所有分支名称开始--branches
,或使用所有的标签名称--tags
,或所有引用(包括像refs/stash
使用) --all
。 Add --topo-order
to the git rev-list
command and you'll get a list of commits and their children.将--topo-order
添加到git rev-list
命令,您将获得提交及其子项的列表。 From this list you will want the last commit that leads to F
but also has multiple children.从这个列表中,您将需要最后一次提交,该提交导致F
但也有多个子项。 Unfortunately the output from git rev-list
here is still not terribly useful.不幸的是,这里git rev-list
的输出仍然不是非常有用。 To do this job quickly, you might want to write your own graph reader, in which case you can do your own internal marking and can just use git rev-list --parents --all
or similar.为了快速完成这项工作,您可能需要编写自己的图形阅读器,在这种情况下,您可以进行自己的内部标记,并且可以使用git rev-list --parents --all
或类似命令。
(This job is much easier in Mercurial, although even there it may still be nontrivial.) (这项工作在 Mercurial 中要容易得多,尽管即使在那里它可能仍然很重要。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.