如何强制 git submodule update --init 跳过错误

Rap*_*ert 6 git

我的文件夹中有我正在处理的活动项目,并且所有项目都被版本化为 git 子模块。当我设置一个新的工作环境时,我会使用--recursive(或正常克隆它然后执行git submodule update --init --recursive. /update 只更新可用的存储库。但是,git 会在遇到第一个问题时停止更新:

Cloning into 'dir/name'...
Connection closed by remote.host
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Clone of 'user@example.com:path/to.git' into submodule path 'dir/name' failed
Run Code Online (Sandbox Code Playgroud)

这个过程就到此为止了。

如何强制 git 忽略此类错误并继续下一个子模块?

小智 2

我不知道有哪个标志可以让 git 忽略此类错误,所以我编写了一个 Python 脚本来实现(大致)相同的效果。您可以将其放在主项目的目录中。

#!/usr/bin/python

import os


PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))


def main():
    # The following command may fail
    os.system('cd {} && git submodule update --init --recursive'.format(PROJECT_ROOT))

    # In case the above command failed, also go through all submodules and update them individually
    for root, dirs, files in os.walk(PROJECT_ROOT):
        for filename in files:
            if filename == '.gitmodules':
                with open(os.path.join(root, filename), 'r') as gitmodules_file:
                    for line in gitmodules_file:
                        line = line.replace(' ', '')
                        if 'path=' in line:
                            submodule = line.replace('path=', '')
                            os.system('cd {} && git submodule init {}'.format(root, submodule))
                            os.system('cd {} && git submodule update {}'.format(root, submodule))


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)