1

Add recursively git repos in subfolder as submodules:

iam looking for a solution to add git repositories as submodules to a folder which is a git repo.

I already have the folder structure and the cloned repositories in the destination folder. All i need is to automate adding those repos in the subfolders as submodules.

there are hundreds of git repos so i want to automate it.

structure: folder 1 & 2 serve as categories.

~/gitrepo-main/folder1/folder01/submodule
~/gitrepo-main/folder1/folder02/submodule
~/gitrepo-main/folder2/folder01/submodule
~/gitrepo-main/folder2/folder02/submodule

EDIT:

probably found the solution via Convert git repo to submodule

script run from gitrepo-main folder

#!/bin/bash
for d in `find folder1/ -maxdepth 3 -mindepth 3 -type d`
do
    pushd $d
    export url=$(git config --get remote.origin.url)
    popd
    git submodule add $url $d
done
Community
  • 1
  • 1

1 Answers1

0

I already have the folder structure and the cloned repositories in the destination folder.

I thought it would be a problem, but git submodule add man page does mention:

<path> is the relative location for the cloned submodule to exist in the superproject.

  • If <path> does not exist, then the submodule is created by cloning from the named URL.
  • If <path> does exist and is already a valid Git repository, then this is added to the changeset without cloning.

So all you need to do:

  • find all gitlinks (if those nested repos are already added to the parent repo with a git add . done at its root directory)
  • or find all nested repo by looking for any .git subfolder within the parent repo.
  • query for the remote subrepo url (git remote -v within each of those folders).
    Note that you don't have 'cd' into those subfolders, you can do a:

    git -C /full/path/of/subrepo remote -v
    
  • declare that path as submodule

    git submodule add -- /remote/url/of/subrepo relative/path/of/subrepo
    

The OP mentions "Convert git repo to submodule". Using the -C syntax, that would give:

for d in `find .vim/bundle/ -maxdepth 1 -mindepth 1 -type d`
do
    export url=$(git -C $d config --get remote.origin.url)
    git submodule add $url $d
done

No more pushd/popd.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thx for your answer, please see my edit - have to rush now, will check back later - regards –  Apr 05 '17 at 04:50
  • @Aurigae yes, that is the same idea – VonC Apr 05 '17 at 04:51
  • 1
    @Aurigae I have rewritten it with the -C syntax. – VonC Apr 05 '17 at 04:53
  • kk, so we can closeup the question i guess, tyvm - was pure fluke i came across the linked script. I suggest to leave the QA open as the answer wasnt visible by google in the first place, keywords here should do. –  Apr 05 '17 at 04:55