我有一个Git存储库,其中有几个子模块。如何在git子模块init运行后列出所有子模块的名称?
git子模块foreach命令可以回显子模块的名称,但这只有在它们被签出后才有效,而在init步骤之后没有发生。在签出之前,链中还有更多的步骤需要执行,我不希望将子模块的名称硬连接到脚本中。
那么是否存在一个Git命令来获取所有当前已注册但尚未签出的子模块的名称?
我有一个Git存储库,其中有几个子模块。如何在git子模块init运行后列出所有子模块的名称?
git子模块foreach命令可以回显子模块的名称,但这只有在它们被签出后才有效,而在init步骤之后没有发生。在签出之前,链中还有更多的步骤需要执行,我不希望将子模块的名称硬连接到脚本中。
那么是否存在一个Git命令来获取所有当前已注册但尚未签出的子模块的名称?
当前回答
我用的是这个:
git submodule status | cut -d' ' -f3-4
输出(路径+版本):
tools/deploy_utils (0.2.4)
其他回答
你可以使用与git子模块init使用自身相同的机制,即查看.gitmodules。该文件枚举每个子模块路径及其引用的URL。
例如,从存储库的根目录,cat .gitmodules将把内容打印到屏幕上(假设你有cat)。
因为.gitmodule文件有Git配置格式,你可以使用Git配置来解析这些文件:
git config --file .gitmodules --name-only --get-regexp path
将显示所有子模块条目,并与
git config --file .gitmodules --get-regexp path | awk '{ print $2 }'
您只能得到子模块路径本身。
下面是另一种从. Git modules解析Git子模块名称的方法,不需要sed或花哨的IFS设置。: -)
#!/bin/env bash
function stripStartAndEndQuotes {
temp="${1%\"}"
temp="${temp#\"}"
echo "$temp"
}
function getSubmoduleNames {
line=$1
len=${#line} # Get line length
stripStartAndEndQuotes "${line::len-1}" # Remove last character
}
while read line; do
getSubmoduleNames "$line"
done < <(cat .gitmodules | grep "\[submodule.*\]" | cut -d ' ' -f 2-)
使用内置git函数显示每个子模块的所有信息:
git submodule foreach -q git config -l
或者只是url:
git submodule foreach -q git config remote.origin.url
从这里偷来的。
这招对我很管用:
git ls-files --stage | grep ^160000
它基于这篇伟大的文章:理解Git子模块
它必须读取grep ^160000。
假设你目前已经注册了,但还没有签出子模块,如下:
$ cat .gitmodules
[submodule ".github/workflows/packages"]
path = .github/workflows/packages
url = https://github.com/MarketLeader/Packages
[submodule ".github/workflows/builders"]
path = .github/workflows/builders
url = https://github.com/chetabahana/runner
[submodule "docs"]
path = docs
url = https://github.com/eq19/lexer
下面是Git命令来获取存储库的根目录:
$ git submodule foreach -q '[[ "$sm_path" == */* ]] || git config remote.origin.url'
https://github.com/eq19/lexer
下面是Git命令在特定路径上获取列表:
$ git submodule foreach -q '[[ ! "$sm_path" == .github/* ]] || git config remote.origin.url'
https://github.com/chetabahana/runner
https://github.com/MarketLeader/Packages
当然,你也可以用其他命令修改git config remote.origin.url。
顺便说一下,我使用的是git 2.38.1版