如何从父克隆中的过去提交中获取 git 子模块的关联提交 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3983829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:39:36  来源:igfitidea点击:

How can I get a git submodule's associated commit ID from a past commit in the parent clone?

gitdiffgit-submodules

提问by davidA

Is there a way, short of actually checking out the parent commit, to determine a submodule's SHA-1 commit ID based on a commit ID in the parent clone? I know I can find the currentlyassociated SHA-1 with git submodule.

除了实际检查父提交之外,有没有办法根据父克隆中的提交 ID 确定子模块的 SHA-1 提交 ID?我知道我可以找到当前关联的 SHA-1 与git submodule.

Here's an example:

下面是一个例子:

  • I have a clone with a single submodule foothat has changed several times in the last month.
  • I have a tag in the parent clone that is a few weeks old called released-1.2.3. I want to find out what the associated SHA-1 of foowas for this tagged commit.
  • I could simply check out released-1.2.3and use git submoduleto see, but I'm wondering if there's a way to do this withoutaffecting the working tree, as I want to script it.
  • 我有一个带有单个子模块的克隆,该子模块foo在上个月更改了几次。
  • 我在几周前的父克隆中有一个名为released-1.2.3. 我想找出foo这个标记提交的关联 SHA-1是什么。
  • 我可以简单地查看released-1.2.3并使用git submodule查看,但我想知道是否有办法在影响工作树的情况下执行此操作,因为我想编写脚本。

I want to do this because I want to construct a script to do a 'diff' on all changes within a submodule between two commits within the parent repository - i.e. "tell me what files changed within the submodule foobetween these two commits in the parent."

我想这样做是因为我想构建一个脚本来对父存储库中两次提交之间的子模块内的所有更改进行“差异”——即“告诉我在父存储库中的foo这两次提交之间子模块中更改了哪些文件。 ”

回答by Josh Lee

You may use git-ls-treeto see what the SHA-1 id of a given path was during a given commit:

您可以git-ls-tree用来查看在给定提交期间给定路径的 SHA-1 id 是什么:

$ git ls-tree released-1.2.3 foo
160000 commit c0f065504bb0e8cfa2b107e975bb9dc5a34b0398  foo

(My first thought was git show released-1.2.3 foo, but that fails with "fatal: bad object".)

(我的第一个想法是git show released-1.2.3 foo,但由于“致命:坏对象”而失败。)

Since you are scripting the output, you will probably want to get just the SHA-1 id by itself, e.g.:

由于您正在编写输出脚本,因此您可能只想自己获取 SHA-1 id,例如:

$ git ls-tree released-1.2.3 foo | awk '{print }'
c0f065504bb0e8cfa2b107e975bb9dc5a34b0398

Also: When writing scripts around git, try to stick to the plumbingcommands, as described in the manual. They have a more stable interface, while the more familiar “porcelain” commands will possibly change in incompatible ways.

另外:当围绕 git 编写脚本时,尽量坚持使用管道命令,如手册中所述。他们有一个更稳定的界面,而更熟悉的“瓷器”命令可能会以不兼容的方式改变。

回答by mash5

This one worked for me:

这个对我有用:

$ git rev-parse released-1.2.3^{commit}:foo
<SHA1>

Perhaps also quite easy to use in script.

也许在脚本中也很容易使用。

回答by davidA

I did find one promising avenue:

我确实找到了一个有前途的途径:

$ git log --raw <since>..<until> --submodule -- <path/to/submodule>

With the --raw option, this does print out the (abbreviated) SHA-1 IDs corresponding to the submodule's associated commits. Unfortunately the output is very verbose and will take some work to process in a script.

使用 --raw 选项,这会打印出与子模块相关提交对应的(缩写)SHA-1 ID。不幸的是,输出非常冗长,需要一些工作才能在脚本中处理。

What I really need is a git facility that, given a parent commit ID, gives me the latest change to a submodule prior to that commit. I.e. which submodule SHA-1 'git submodule update' would check out if I were to actually checkout that parent commit.

我真正需要的是一个 git 工具,它在给定父提交 ID 的情况下,在提交之前为我提供对子模块的最新更改。即哪个子模块 SHA-1 'git submodule update' 会检查我是否要实际检查该父提交。

回答by Adam Dymitruk

git slave (gits) might be your answer.

git slave (gits) 可能是你的答案。

http://gitslave.sourceforge.net/

http://gitslave.sourceforge.net/

But you can also do this with plain git.. it's not as easy.

但是你也可以用普通的 git 来做到这一点......这并不容易。

回答by W.Perrin

gitls-treewill do the job. But sometimes you should call it several times or use -rto show subentries recursively.

gitls-tree会做的工作。但有时您应该多次调用它或用于-r递归显示子条目。

    ├─project
    │  └─submodules
    │      ├─submodule_a
    │      ├─submodule_b
    │      ├─...

For example, if you has such a project directory, and you want the commit id of submodule_awith an tag name in parent module.

例如,如果您有这样一个项目目录,并且您想要submodule_a父模块中带有标记名称的提交 ID 。

git ls-tree v5.4.0

will give you the tree id of directory submodules, not commit id. It looks like as following.

会给你目录的树ID submodules,而不是提交ID。它看起来如下。

040000 tree e542bc1b084a0394ff16452c27df6572366e0009    submodules

Just feed ls-treewith the tree id, and you will get the actual commit id of each submodule.

只需ls-tree输入树 id,您将获得每个子模块的实际提交 id。

git ls-tree e542bc

You will get something like this.

你会得到这样的东西。

160000 commit 0f253bf94e0345600cb7a234a24eeec8ee3533bd  submodule_a
160000 commit 0edcbaf94e034987cb4eeec8ee3533bd12349ade  submodule_b

Or just use

或者只是使用

git ls-tree v5.4.0 -r

to check all entries tree id or commit id.

检查所有条目树 ID 或提交 ID。