2

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?

Eenoku
  • 2,741
  • 4
  • 32
  • 64

1 Answers1

0

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.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • Out of curiosity... Is there any way to do this "purely" in CMake without Bash? – Eenoku Apr 01 '19 at 03:54
  • @Eenoku: I found couple of this links useful - https://foonathan.net/blog/2016/07/07/cmake-dependency-handling.html and https://stackoverflow.com/questions/43761594/cmake-and-using-git-submodule-for-dependence-projects. I'm not an expert in that, but your original post didin't refer to `CMake` at all. I answered it from a `bash` perspective. Ask a new question with `CMake` tag if you are interested and consider voting up this answer – Inian Apr 01 '19 at 04:15
  • Thank you! I know, that I've mentioned `Bash` and your answer is correct. Also I accepted it. – Eenoku Apr 01 '19 at 09:05
  • To be honest, accepting the answer is usually considered the proper reaction - I believe, that we shouldn't really "force" somebody to upvote our reactions. – Eenoku Apr 01 '19 at 09:33