如何使用repo forall命令跳过Git子模块?
问题
Repo工具的多数命令支持--fetch-submodules、--submodules这类子模块操作参数,但repo help forall和repo forall --help的文档里完全没提相关内容。实际执行repo forall -c <command>时,命令却会遍历所有子模块。请问:
- 这种行为的原因是什么?
- 如何让
repo forall仅在manifest文件指定的、由Repo管理的仓库中执行Shell命令?(注:$REPO_PROJECT或$REPO_REMOTE这类环境变量无法区分仓库类型,没法解决这个问题)
Repo版本信息:
repo launcher version 1.27 (from /usr/bin/repo) (currently at 1.13.7-40-gd92076d) repo User-Agent git-repo/1.13.7-40-gd92076d (Linux) git/2.17.1 Python/2.7.17 git 2.17.1 git User-Agent git/2.17.1 (Linux) git-repo/1.13.7-40-gd92076d Python 2.7.17 (default, Jul 1 2022, 15:56:32) [GCC 7.5.0]
原因分析
Repo的forall命令在设计上会默认遍历所有Git仓库——包括Repo管理的主仓库和子模块仓库。这是因为早期版本的Repo在实现forall时,直接基于Git的仓库遍历逻辑,没有针对子模块做特殊过滤,而且相关文档没有及时更新,导致help信息里遗漏了这个行为说明。
解决方法
要让repo forall仅在manifest指定的仓库执行命令,可以通过检查当前仓库是否在Repo的项目列表中来实现:
- 先导出所有Repo管理的项目路径到临时文件:
repo list | awk '{print $2}' > /tmp/repo_projects.txt
- 在
repo forall的命令里判断当前路径是否在这个列表中,仅当存在时才执行目标命令:
repo forall -c 'if grep -qx "$PWD" /tmp/repo_projects.txt; then <你的命令>; fi'
或者合并成单条命令,避免生成临时文件:
repo forall -c 'PROJECTS="$(repo list | awk '\''{print $2}'\'')"; if echo "$PROJECTS" | grep -qx "$PWD"; then <你的命令>; fi'
内容的提问来源于stack exchange,提问作者phuclv
相关产品推荐
相关产品推荐

