Git 子模块:位于子模块文件夹中的文件必须由同一个子模块跟踪吗?

Git submodules : must a file located in a submodule's folder be tracked by that same submodule?

我有 git 包含子模块 B 的 repo A。一些文件 file.c 位于 B 的文件夹内,它本身位于 A 的文件夹内预计。问题:我可以从 A 而不是从 B 跟踪这个文件 file.c 吗?这有什么意义吗?

想法是 B 的任何用户都必须在 B 的文件夹层次结构的这个特定位置添加他们自己的 file.c。如果有人没有这样做但仍然将 B 添加为子模块,B 将简单地提到 compiling/running.

上没有目标文件

I have git repo A containing submodule B.

换句话说,你可能有:

$ cd /path/to/A
$ ls
B/    README

举个(有点傻)的例子。 (这里还有一个 .gitmodules,但它被隐藏了,因为它是一个点文件。)

A file file.c is located inside B's folder, itself inside of A's folder as you'd expect. Question: can I track this file from A and not from B? Does that even make any sense?

问题有道理,但答案是一个响亮的轰隆隆)。问题是子模块 B 的存在在存储库 A 中的表示方式。

存储库 A 的当前 (HEAD) 提交有一个 tree 对象声称存在至少两个 blob 对象:

  • .gitmodules:此文件中有存储库的 URL,以及一个 path 条目,上面写着 B
  • B:此 blob 具有模式 160000("gitlink" 条目)。这个 blob 的 "contents" 是 Git 应该检查的提交哈希 ID,一旦 Git 克隆了 URL 以便 B/ 存在。据推测,检查哈希 ID 会得到一个名为 file.c 的文件,因此 B/file.c 存在。

要在超级项目 A 中存储将提取到 B/file.c 中的 blob 的存在,Git 需要存储第二个 tree 对象,名为 B 在顶层树中(第二个 tree 对象本身有一个名为 file.c 的 blob,然后将其提取到 B/file.c 中)。但是已经有一个名为 B 的 gitlink blob 对象,所以它不能:不允许重名。

Idea is that any user of B has to add their own file.c in this specific location of B's folder hierarchy. If someone fails to do that but still adds B as submodule, B will simply mention that there is no target file upon compiling/running.

你可以做的是在子模块存储库 B 中存储一个名为 file.csymlink,指向 ../user-supplied-file.c../user/file.c 之类的。现在存储库 A 需要包含 user-supplied-file.cuser/file.c 或 link 指向的任何内容。

请注意,这将子模块与超级项目紧密结合在一起。在这一点上,根本不理会子模块可能更合理。库和其他类似子模块的项目通常不需要额外的源代码;他们可能有采用 函数指针 的例程,并通过这些指针调用这些函数,但他们没有完全外部源依赖性。