Adding a git submodule inside of a submodule (nested submodules)
Asked Answered
H

4

15

As described in this Question

Is it possible for a git submodule to be made of several other git submodules, and the super git repo to fetch the contents for each submodule?

The author assumed a git submodule hierarchy like this:

  • repo1
    • submodule xyz1
    • submodule xyz2
  • repo2
    • submodule repo1

This question is about the possibility of nesting a submodule within a submodule:

  • repo1
    • submodule a
      • submodule ab
      • submodule ac

Real example of a .gitmodules should look like this:

[submodule "Source/V8"]
    path = Source/V8
    url = https://chromium.googlesource.com/v8/v8.git
[submodule "Source/V8/build/gyp"]
    path = Source/V8/build/gyp
    url =  https://chromium.googlesource.com/external/gyp
[submodule "Source/V8/third_party/cygwin"]
    path = Source/V8/third_party/cygwin
    url = https://chromium.googlesource.com/chromium/deps/cygwin
[submodule "Source/V8/third_party/python_26"]
    path = Source/V8/third_party/python_26
    url = https://chromium.googlesource.com/chromium/deps/python_26
[submodule "Source/V8/third_party/icu"]
    path = Source/V8/third_party/icu
    url = https://chromium.googlesource.com/chromium/deps/icu52
[submodule "Source/V8/testing/gtest"]
    path = Source/V8/testing/gtest
    url = https://chromium.googlesource.com/chromium/testing/gtest
[submodule "Source/V8/testing/gmock"]
    path = Source/V8/testing/gmock
    url = https://chromium.googlesource.com/chromium/testing/gtest

Note that the path of the submodules are nested:

  • Source/V8
    • Source/V8/build/gyp
    • Source/V8/third_party/cygwin

I tried the following example with no success:

 git submodule add https://chromium.googlesource.com/v8/v8.git   
 Source/V8
 git submodule add https://chromium.googlesource.com/external/gyp 
 Source/V8/build/gyp 

results in:

 The following path is ignored by one of your .gitignore files:
 Source/V8/build/gyp
 Use -f if you really want to add it.

using git submodule add -f results in:

Cloning into 'Source/V8/build/gyp'...
remote: Sending approximately 10.28 MiB ...
remote: Total 16486 (delta 10444), reused 16486 (delta 10444)
Receiving objects: 100% (16486/16486), 10.28 MiB | 2.07 MiB/s, done.
Resolving deltas: 100% (10452/10452), done.
Checking connectivity... done.
fatal: Pathspec 'Source/V8/build/gyp' is in submodule 'Source/V8'
Failed to add submodule 'Source/V8/build/gyp'

Please let me now if this case is possible to achieve.

Update: Note this question is about creating a submodule structure, not initializing it.

Hercule answered 23/1, 2015 at 12:37 Comment(3)
Have you solved this ?Sewer
There is no solution for this use caseHercule
Your structure makes no sense: You’d like to have a parent folder and child folders which you want to both ignore (to make stuff “clean” in the parent repo), yet when brought in with submodules overrides the fact that the parent tells it to ignore those directories?…Cloying
P
0

Based on my analysis of the Git v2.40.1 source code, it appears that what you’re trying to do is impossible.

builtin/submodule--helper.c contains the code that produces that “Failed to add submodule 'Source/V8/build/gyp'” error you got:

static void configure_added_submodule(struct add_data *add_data)
{
    // […]
    struct child_process add_submod = CHILD_PROCESS_INIT;
    // […]
    add_submod.git_cmd = 1;
    strvec_pushl(&add_submod.args, "add",
             "--no-warn-embedded-repo", NULL);
    // […]
    if (run_command(&add_submod))
        die(_("Failed to add submodule '%s'"), add_data->sm_path);
    // […]
}

In other words, in order to configure_added_submodules, Git needs to run git add with some arguments. The code for git add unconditionally calls die_path_inside_submodule() which is the function that gives you the “fatal: Pathspec 'Source/V8/build/gyp' is in submodule 'Source/V8'” error.

The Git developers’s intentions are clear: a repo is not supposed to add files into its submodules, and your example .gitmodules file is trying to add files into a repo’s submodule.


That being said, why do you want a hierarchy like this?

  • repo1
    • submodule a
      • submodule ab
      • submodule ac

Why wouldn’t a hierarchy like this work?

  • repo1
    • submodule xyz1
    • submodule xyz2
  • repo2
    • submodule repo1
Pragmatism answered 24/5, 2023 at 16:30 Comment(0)
F
-1

Yes, nested submodules is possible although not necessarily advisable. If you really want to create nested submodule, you have to use the following command:

git submodule update --init --recursive

A similar question had actually been asked before. You should have a look here

Fore answered 23/1, 2015 at 20:57 Comment(4)
Sorry this is the wrong answer, please compare the two questions carefully, my question is about creating a submodule structure, not initializing it.Hercule
@Karl2011, have you tried to do git submodule add chromium.googlesource.com/v8/v8.git Source/V8. Then go into Source/V8 directory. In that directory, you should then git submodule add chromium.googlesource.com/external/gyp external/gyp. The directory external/gyp would then be a submodule inside Source/V8.Fore
Yes, I tried it but this adds a submodule to the v8 repository and not to the root repository.Hercule
Source/V8 is already a submodule of the root repository. You can therefore not add Source/V8/external/gyp as a submodule of the root repository. You can only add it as a submodule of the Source/V8. Another option is to add external/gyp at the same level as Source/V8 and then create a symbolic link from Source/V8/external/gyp to External/gyp. In that case External/gyp will be created as a submodule of the root repo.Fore
P
-1

Submodules should be added to a repository, not to a submodule.
Then, after they are tracked in the top repository, the top repository may be used as a submodule in another project , resulting in a nested submodules structure.
In your example, you should add gyp as a submodule to the V8 project. If you cannot affect the top project, then yous should ask yourself what is the meaning of adding a submodule to this project?
When the desired structure is built, you may use the command that @iclman suggested in order to bring all submodules in:
git submodule update --init --recursive

Parkinson answered 22/8, 2019 at 7:19 Comment(1)
Welcome to SO, we appreciate your input! Could you please edit your question and make it self-contained, by explaining what iclman said? See also stackoverflow.com/help/how-to-answerGinzburg
T
-1

Short answer: Yes. At the end of the day, a submodule is a module, but inside another module.

Which means that if you setup a parent repo, with a submodule. You can go inside that submodule and set another module inside of it. This way, the parent repo will see that you have some changes inside of your submodule, but wont see that you have another submodule inside of it.

Trichloromethane answered 11/11, 2022 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.