Search code examples
bashgitgit-submodules

How to clone multiple specific submodules?


I need to write a script to clone Boost library, but the repository is unfortunately really big and I need to use just some submodules afterwards. I'd like to store them in one string variable like this

MODULES="tools/build libs/system"

and then pass the variable in to one command like this

git clone --recurse-submodules=${MODULES} https://github.com/boostorg/boost.git

The problem is, that after passing multiple arguments into --recurse-submodules, all of them get ignored.

I had a look at How to only update specific git submodules?, but the answers cover only cloning of one submodules or repeating --recurse-submodules multiple times, which I don't like to, as I want to make the script prepared for arbitrary number of submodules.

Is there any way how to achieve that with Git?


Solution

  • Your idea is right, but don't use a variable, use an array and build your sub-modules that way.

    modules=()
    for mod in "tools/build" "libs/system"; do
        modules+=( --recurse-submodules="$mod" )
    done
    

    In the for loop add all your modules names, so that each iteration adds the required fields before it and generates the complete sub-module array. Now pass it git clone as a quoted array expansion of modules

    git clone "${modules[@]}" https://github.com/boostorg/boost.git
    

    The "${modules[@]}" expands to array generated in the step above, with each generated entry separated by a white-space character.