子模块初始化验证 [Git]

9

最近我在我的项目中开始使用git子模块。我的一个脚本创建了一个带有子模块的仓库。我想创建一个函数来检查子模块是否成功初始化。

如何验证子模块已成功初始化?

我想,我们首先应该检查主仓库中是否有一个 .git 目录。然后我应该检查每个子模块,但是检查什么呢?

希望我的问题可以理解,如果不能,请让我更明确些。

3个回答

3
git submodule status 命令会为每个子模块输出可解析的一行:
status [--cached] [--recursive] [--] [<path>...]
    Show the status of the submodules. This will print the SHA-1 of the currently checked out commit for each
    submodule, along with the submodule path and the output of git describe for the SHA-1. Each SHA-1 will
    possibly be prefixed with - if the submodule is not initialized, + if the currently checked out submodule
    commit does not match the SHA-1 found in the index of the containing repository and U if the submodule has
    merge conflicts.

如果您只想知道每个子模块是否已初始化,只需执行以下操作即可:
if git submodule status | grep --quiet '^-'; then
    echo "A git submodule is not initialized."
fi

1
我会执行例如 git submodule foreach statusgit submodule summary 命令,并且会在父仓库中双重检查 .gitmodules 文件。

0
为了更好地解析git子模块摘要,请使用Git 2.21(2019年2月):"git submodule summary"子命令通过机械地将其截断为7个十六进制数字来显示缩短的提交对象名称,这已经得到改进,使"rev-parse --short"可以根据存储库的大小来缩短缩写的长度。
请参见提交0586a43(2019年2月3日),作者是Sven van Haastregt(svenvh
(由Junio C Hamano -- gitster --合并于提交257507a,2019年2月7日)

git-submodule.sh:使用rev-parse缩短子模块SHA-1

到目前为止,git submodule summary一直在发出具有更高歧义性的7个字符SHA-1,对于更大的存储库而言,这种情况可能更加普遍。现在改用git rev-parse --short,它将确定适当的短SHA-1长度。

当子模块尚未使用“submodule init”初始化或未克隆时,git rev-parse尚无法在其中工作;作为后备,使用原始方法在7个十六进制数字处切割。


请注意,从Git 2.29(2020年第四季度)开始,来自“git submodule手册git submodule summary将以C语言重写。
它不再是git-submodule.sh的一部分。

请查看 提交 e83e333 (2020年8月13日),作者为 Prathamesh Chavan (pratham-pc)
请查看 提交 ede8a5b, 提交 180b154, 提交 6414c3d (2020年8月13日),作者为 Shourya Shukla (periperidip)
(由Junio C Hamano -- gitster --合并于提交 bbdba3d,2020年9月9日)

submodule: 将子模块子命令“summary”从shell移植到C

指导者:Christian Couder
指导者:Stefan Beller
指导者:Kaartic Sivaraam
协助者:Johannes Schindelin
签署者:Prathamesh Chavan
签署者:Shourya Shukla

Convert submodule subcommand 'summary' to a builtin and call it via 'git-submodule.sh'.

The shell version had to call $diff_cmd twice, once to find the modified modules cared by the user and then again, with that list of modules to do various operations for computing the summary of those modules.

On the other hand, the C version does not need a second call to $diff_cmd since it reuses the module list from the first call to do the aforementioned tasks.

In the C version, we use the combination of setting a child process' working directory to the submodule path and then calling 'prepare_submodule_repo_env()' which also sets the 'GIT_DIR' to '.git', so that we can be certain that those spawned processes will not access the superproject's ODB by mistake.

A behavioural difference between the C and the shell version is that the shell version outputs two line feeds after the 'git log(man)' output when run outside of the tests while the C version outputs one line feed in any case.
The reason for this is that the shell version calls log with '--pretty=format:<fmt>' whose output is followed by two echo calls; 'format' does not have "terminator" semantics like its 'tformat' counterpart.
So, the log output is terminated by a newline only when invoked by the user and not when invoked from the scripts.

This results in the one & two line feed differences in the shell version.

On the other hand, the C version calls log with '--pretty=<fmt>' which is equivalent to '--pretty:tformat:<fmt>' which is then followed by a 'printf("\n")'.
Due to its "terminator" semantics the log output is always terminated by newline and hence one line feed in any case.

Also, when we try to pass an option-like argument after a non-option argument, for instance:

git submodule summary HEAD --foo-bar  

(or)  

git submodule summary HEAD --cached  

That argument would be treated like a path to the submodule for which the user is requesting a summary.
So, the option ends up having no effect.
Though, passing '--quiet' is an exception to this:

git submodule summary HEAD --quiet  

While 'summary' doesn't support '--quiet', we don't get an output for the above command as '--quiet' is treated as a path which means we get an output only if a submodule whose path is '--quiet' exists.

The error message in case of computing a summary for non-existent submodules in the C version is different from that of the shell version.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接