Thursday, October 25, 2012

Nested (recursive) submodules in Git

Git submodules

Git submodules are the way to implement dependencies between projects.

Let's say you have a common libray 'mylib' shared between apps 'myapp1' and 'myapp2' then you will keep only 3 repositories (1 for each of them).

Both 'myapp1' and 'myapp2' will have 'mylib' declared as a submodule, which is cloned from / pushed to its own repository. And the repository of each parent app will only keep a reference to the commit to use from the submodule repository.

Nested Git submodules

Extending the same principle, 'mylib' could as well be dependant on 'somebaselib', declared as a submodule and hosted elsewhere.
There's plenty of documentation about how to clone or update such repositories (once they exist), using
   git clone ... --recursive
   git submodule update --recursive
But how to build one of those is not so intuitive.

Adding nested submodules: the problem

You may have used this procedure to add a submodule into the 'mylib' repository:
   cd ~/mylib
   git submodule add git@someurl/somebaselib baselib   
As the baselib folder does not exist, the previous command does a clone of the specified repository into that folder, and then adds it as a submodule.
You may think the same would to add the nested submodules to the superproject:
   cd ~/myapp
   git submodule add git@someurl/mylib lib
but unfortunatelly this does not work... because the cloning fails.

Adding nested submodules: the solution

The workaround is to manually clone the nested submodules into the subfolder with the --recursive option, before doing the 'submodule add'
   cd ~/myapp
   git clone git@someurl/mylib lib --recursive
   git submodule add git@someurl/mylib lib
Now, as the lib folder does exist, the 'submodule add' command does not try to clone it and just adds it as a submodule. You are all set!

1 comment :

Anonymous said...

Hi there! I know this is kinda off topic but
I was wondering if you knew where I could find a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having problems finding
one? Thanks a lot!|

Review my web-site - program pit 2013