查找包含与给定子模块提交

时间:2017-05-09 07:52:16

标签: git git-submodules

在Git中,我在模块A中有子模块B.我想转到B中的特定提交,然后使用B的特定提交(如果有的话)在A中找到相应的提交。例如,使用该特定B版本的A的最新提交将是可以的。怎么做?

2 个答案:

答案 0 :(得分:1)

在合理的限制下,可以使用git bisect找到该提交:

#!/bin/bash
#
# Find superproject commit introducing given submodule's commit
#
# Usage: git submodule-introduced path/to/submodule <submodule-commit>
#
# Bisect is performed. On success found commit is displayed and written to the
# 'bisect/containing' ref
#
# Limitations:
# 1. Submodule must have monotonous history (all submodule updates are fast-forward)
# 2. Commit adding a submodule must have parents
set -e

export submodule=${1?specify submodule path}
export commit=${2?specify submodule commit}

from=$(git rev-list HEAD -- $submodule | tail -n 1)

git bisect start --no-checkout --term-bad=containing HEAD $from~ -- $submodule

git bisect run sh -c '! git -C $submodule merge-base --is-ancestor $commit $(git rev-parse BISECT_HEAD:$submodule)'

答案 1 :(得分:-1)

不保证存在此类提交。如果确实存在,则可能不是唯一的(可能不止一个)。

当然,并不保证任何事情都有效,但这不是我在这里的意思。子模块背后的设计原理是另一种方式:超级项目,&#34;模块A&#34;在您的情况下,被视为控制项目。您可以查看一些给定的超级项目提交分支名称,标记名称或原始哈希ID,或者其他任何内容,并且在该检出的提交中,超级项目包含子模块中特定提交的哈希ID。

因此,对于模块A中的每个提交(或者无论如何都使用B的每个提交),B都有一些特定的哈希ID。我们暂时假设A中只有三个提交,编号为A1 ,A2和A3,以及B中的三个提交,编号为B1,B2和B3。如果我们查看A1,我们会得到一个B号码。让我们说B2。如果我们查看A2,我们会得到另一个B号码。让我们再说一遍B2吧。最后,如果我们查看A3,我们会得到另一个B号码......这可能再次是B2。

在这种情况下,&#34; A&#34;对应于提交B1和B3的提交都是为空的,以及&#34; A&#34;对应于B2的提交具有基数3.对于某些B提交,有 no 提交,对于其他B提交,没有唯一提交。

这通常适用于子模块使用,因此,如果给出一些 B 提交哈希,则无法找到 对应的 A 提交。但是,您可以查看任意数量的 A 提交并找到相应的 B 哈希值。这些是提交树中的 gitlink 条目,请参阅How can I get a git submodule's associated commit ID from a past commit in the parent clone?(请注意基于git rev-parse的答案,如果您知道子模块的特定路径,那么这就是要做的事情。 #39; s gitlink)。如果您创建了所有 A-> B 映射的完整表格,则可以轻松地反转表格并找到给定的所有 A 值的集合B 值,因此该集合是否为空。