Search code examples
gitgit-submodulessparse-checkout

git sparse-checkout of a submodule from a root repo


I have a main repo with 4 submodules:

  • kvm
  • libvmi
  • qemu
  • nitro

and I would like to sparse-checkout kvm, to filter out a few annoying files that are not comptabile with filesystem limitations on Windows (AUX is a reserved filename, and kvm tree contains aux.{h|c})

I understand how to do a sparse-checkout on a normal repository, but when I do the same on a submodule, it doesn't work, the .git/modules directory is missing:

git clone https://github.com/KVM-VMI/kvm-vmi/
git -C kvm config core.sparseCheckout true
echo "!drivers/gpu/drm/nouveau/nvkm/subdev/i2c/aux.*" >> .git/modules/kvm/info/sparse-checkout # ignore aux.*
echo "/*" >> .git/modules/kvm/info/sparse-checkout # checkout everything else
git submodule update --init

The first echo command will fail because the .git/modules hasn't been created.

I don't know who is responsible for creating this directory.

Am I missing an "init" command somewhere ?

Note:

I have been looking for solutions on Stackoverflow, or from blog posts:

but they don't work for me because I don't want to clone a separate kvm repo and later on add it to my main repo.

Thanks.


Solution

  • Clone the kvm repo yourself, with --no-checkout aka -n, to the kvm directory. Set it up as you like, git submodule absorbgitdirs kvm from the superproject to move the repo and set its core.worktree and leave the .git breadcrumb file in that kvm work tree, and git -C kvm reset --hard to finish the checkout.

    git clone https://github.com/KVM-VMI/kvm-vmi
    cd kvm-vmi
    git submodule init
    git clone -n `git config submodule.kvm.url` kvm
    mkdir kvm/.git/info
    

    and you're ready for

    mkdir kvm/.git/info
    git -C kvm config core.sparsecheckout true
    printf %s\\n '/*' '!aux.*' >kvm/.git/info/sparse-checkout
    git submodule absorbgitdirs kvm
    git -C kvm reset --hard