递归添加 git 子文件夹中的 repos 作为子模块

Add recursively git repos in subfolder as submodules

递归添加 git 子文件夹中的 repos 作为子模块:

我正在寻找一种解决方案,将 git 存储库作为子模块添加到 git 存储库的文件夹中。

我已经在目标文件夹中有了文件夹结构和克隆的存储库。我需要的只是自动将这些存储库作为子模块添加到子文件夹中。

有数百个 git 存储库,所以我想将其自动化。

结构:文件夹 1 和 2 作为类别。

~/gitrepo-main/folder1/folder01/submodule
~/gitrepo-main/folder1/folder02/submodule
~/gitrepo-main/folder2/folder01/submodule
~/gitrepo-main/folder2/folder02/submodule

编辑:

可能通过 Convert git repo to submodule

找到了解决方案

脚本 运行 来自 gitrepo-main 文件夹

#!/bin/bash
for d in `find folder1/ -maxdepth 3 -mindepth 3 -type d`
do
    pushd $d
    export url=$(git config --get remote.origin.url)
    popd
    git submodule add $url $d
done

I already have the folder structure and the cloned repositories in the destination folder.

我认为这会是一个问题,但 git submodule add man page 确实提到:

<path> is the relative location for the cloned submodule to exist in the superproject.

  • If <path> does not exist, then the submodule is created by cloning from the named URL.
  • If <path> does exist and is already a valid Git repository, then this is added to the changeset without cloning.

所以你需要做的就是:

  • find all gitlinks(如果那些嵌套的回购已经添加到父回购,并在其根目录中完成 git add .
  • 或通过在父存储库中查找任何 .git 子文件夹来查找所有嵌套存储库。
  • 查询远程子仓库 url(每个文件夹中 git remote -v)。
    请注意,这些子文件夹中没有“cd”,您可以执行以下操作:

    git -C /full/path/of/subrepo remote -v
    
  • 将该路径声明为子模块

    git submodule add -- /remote/url/of/subrepo relative/path/of/subrepo
    

OP 提到“Convert git repo to submodule”。使用 -C 语法,将给出:

for d in `find .vim/bundle/ -maxdepth 1 -mindepth 1 -type d`
do
    export url=$(git -C $d config --get remote.origin.url)
    git submodule add $url $d
done

没有了 pushd/popd.