将子模块列表添加到 git

Add a list of submodules to the git

我克隆了一个项目并对其进行了定制。该项目正在使用一些额外的项目作为子模块。我已经设置了我的 git 存储库并将主项目推送到那里。但我没有将子模块添加到我的 git 项目中。

我想将子模块拉到我的项目中。我知道使用以下命令我可以一个一个地添加子模块:

git submodule add <sub-m url> <path>

但是他们很多。还有另一种方法可以自动将它们添加在一起吗?这是子模块列表:

submodule "submodules/bcg729"]
    url = git://git.linphone.org/bcg729.git
[submodule "submodules/bctoolbox"]
    url = git://git.linphone.org/bctoolbox.git
[submodule "submodules/belcard"]
    url = git://git.linphone.org/belcard.git
[submodule "belle-sip"]
    url = git://git.linphone.org/belle-sip.git
[submodule "submodules/belr"]
    url = git://git.linphone.org/belr.git
[submodule "submodules/bzrtp"]
    url = git://git.linphone.org/bzrtp.git
[submodule "submodules/cmake-builder"]
    url = git://git.linphone.org/linphone-cmake-builder.git
[submodule "submodules/externals/antlr3"]
    url = git://git.linphone.org/antlr3.git
[submodule "submodules/externals/axmlrpc"]
    url = git://git.linphone.org/axmlrpc.git
[submodule "submodules/externals/bv16-floatingpoint"]
    url = git://git.linphone.org/bv16-floatingpoint.git
[submodule "submodules/externals/cunit"]
    url = git://git.linphone.org/cunit.git
[submodule "submodules/externals/ffmpeg"]
    url = git://git.linphone.org/ffmpeg.git
[submodule "submodules/externals/gsm"]
    url = git://git.linphone.org/gsm.git
[submodule "submodules/externals/libmatroska"]
    url = git://git.linphone.org/libmatroska-c.git
[submodule "submodules/externals/libupnp"]
    url = git://git.linphone.org/libupnp.git
[submodule "submodules/externals/libvpx"]
    url = https://chromium.googlesource.com/webm/libvpx
[submodule "submodules/externals/libxml2"]
    url = git://git.gnome.org/libxml2.git
[submodule "submodules/externals/mbedtls"]
    url = git://git.linphone.org/mbedtls.git
[submodule "submodules/externals/opencore-amr"]
    url = git://git.linphone.org/opencore-amr.git
[submodule "submodules/externals/openh264"]
    url = https://github.com/cisco/openh264
[submodule "submodules/externals/opus"]
    url = git://git.linphone.org/opus.git
[submodule "submodules/externals/speex"]
    url = git://git.linphone.org/speex.git
[submodule "submodules/externals/srtp"]
    url = git://git.linphone.org/srtp.git
[submodule "submodules/externals/vo-amrwbenc"]
    url = git://git.linphone.org/vo-amrwbenc.git
[submodule "submodules/externals/x264"]
    url = git://git.linphone.org/x264.git
[submodule "submodules/linphone"]
    url = git://git.linphone.org/linphone.git
[submodule "submodules/msamr"]
    url = git://git.linphone.org/msamr.git
[submodule "submodules/mscodec2"]
    url = git://git.linphone.org/mscodec2.git
[submodule "submodules/msopenh264"]
    url = git://git.linphone.org/msopenh264.git
[submodule "submodules/mssilk"]
    url = git://git.linphone.org/mssilk.git
[submodule "submodules/mswebrtc"]
    url = git://git.linphone.org/mswebrtc.git
[submodule "submodules/msx264"]
    url = git://git.linphone.org/msx264.git
[submodule "linphone"]
    url = git://git.linphone.org/linphone.git

我认为 git 没有这样的方法。但是,您可以只执行一个 shell 脚本来迭代您的列表并在每个列表上逐一调用 git submodule add


为什么仅仅将它们复制到 .gitmodules 或 .git/index 是行不通的:

The "git submodule add" command does a couple of things:

  • It clones the submodule under the current directory and by default checks out the master branch.
  • It adds the submodule's clone path to the ".gitmodules" file and adds this file to the index, ready to be committed.
  • It adds the submodule's current commit ID to the index, ready to be committed.

(source)

所以你会错过最后一步,git 子模块 init/update 期望提交 ID 已经到位。这就是为什么你需要 git submodule add

根据@eis 的建议,您必须创建一个脚本来执行所有 git add 命令。

一种方法是创建包含您的需求的 .gitmodules,然后使用像这样的脚本解析它:

iterator=1;
subpaths=$(git config --file .gitmodules --get-regexp path | awk '{ print }');
subrepos=$(git config --file .gitmodules --get-regexp url | awk '{ print }');
for path in $subpaths; do
  repo=$(echo "$subrepos"| sed $iterator'q;d');
  git submodule add $repo $path;
  let iterator++;
done