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

发布于 2024-09-28 09:18:29 字数 532 浏览 4 评论 0 原文

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

下面是一个示例:

  • 我有一个带有单个子模块 foo 的克隆,该子模块在上个月已更改了多次。
  • 我在父克隆中有一个几周前的标签,名为 released-1.2.3。我想找出此标记提交的 foo 的关联 SHA-1 是什么。
  • 我可以简单地查看 released-1.2.3 并使用 git submodule 来查看,但我想知道是否有办法做到这一点没有 影响工作树,因为我想编写它的脚本。

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

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 currently associated SHA-1 with git submodule.

Here's an example:

  • I have a clone with a single submodule foo that 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 foo was for this tagged commit.
  • I could simply check out released-1.2.3 and use git submodule to see, but I'm wondering if there's a way to do this without affecting the working tree, as I want to script it.

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 foo between these two commits in the parent."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春花秋月 2024-10-05 09:18:29

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

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

我的第一个想法是 git showreleased-1.2.3 foo,但这失败了与“致命:坏对象”。)

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

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

另外:在围绕 git 编写脚本时,请尝试坚持 管道 命令,如手册中所述。它们具有更稳定的界面,而更熟悉的“瓷器”命令可能会以不兼容的方式发生变化。

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

$ 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".)

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

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

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

薆情海 2024-10-05 09:18:29

这对我有用:

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

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

This one worked for me:

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

Perhaps also quite easy to use in script.

踏雪无痕 2024-10-05 09:18:29

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

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

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

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

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.

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.

私藏温柔 2024-10-05 09:18:29

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

http://gitslave.sourceforge.net/

但是你也可以用普通的 git 来做到这一点..它并不像简单的。

git slave (gits) might be your answer.

http://gitslave.sourceforge.net/

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

晨曦÷微暖 2024-10-05 09:18:29

git ls-tree 会完成这项工作。
但有时您应该多次调用它或使用 -r 递归地显示子条目。

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

例如,如果您有这样一个项目目录,并且您希望在父模块中使用 submodule_a 的提交 id 和标记名称。

git ls-tree v5.4.0

将为您提供目录 submodules 的树 ID,而不是提交 ID。看起来如下。

040000 tree e542bc1b084a0394ff16452c27df6572366e0009    submodules

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

git ls-tree e542bc

你会得到这样的东西。

160000 commit 0f253bf94e0345600cb7a234a24eeec8ee3533bd  submodule_a
160000 commit 0edcbaf94e034987cb4eeec8ee3533bd12349ade  submodule_b

或者只是用来

git ls-tree v5.4.0 -r

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

git ls-tree will do the job.
But sometimes you should call it several times or use -r to show subentries recursively.

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

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

git ls-tree v5.4.0

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

040000 tree e542bc1b084a0394ff16452c27df6572366e0009    submodules

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文