Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make git submodule independently usable from superproject

I have a larger superproject with submodules, some submodules are supposed to build docker images, which I'd like to build independently from superproject. The problem is that git operations are failing within the docker build process because docker cuts the docker context on the level of subproject:

fatal: Not a git repository: ../.git/modules/<submodule>

The issue is basically that <submodule>/.git is available within the docker context, but <superproject>/.git is not. Hence, I need a submodule without reference to the superproject. Now I'm looking for some sort of git-dir splitting mechanism for submodules. Basically the reverse of git-submodule-absorbgitdirs.

like image 303
gnod Avatar asked Jan 28 '26 18:01

gnod


1 Answers

Submodule's .git file is in the form of gitdir: ... which points to the actual .git dir of your submodule.

You can copy the submodule's "real" .git dir into your Docker image before building it, and replace the .git file with that dir when building the Docker image.
This is almost enough - you also need to fix the config worktree and nested submodules.


Here are the steps in detail:

Before building the Docker image, copy the real .git directory of your submodule next to your Dockerfile so you could copy it in the Dockerfile:

DOTGIT=$(cat $SUBMODULE/.git)
GITDIR=${DOTGIT/gitdir: }
cp -rf $SUBMODULE/$GITDIR $SUBMODULE.gitdir

Then remove worktree from the submodule's config:

sed -i '/worktree =/d' $SUBMODULE.gitdir/config

Then in your Dockerfile, copy the submodule

COPY $SUBMODULE /submodule
WORKDIR /submodule

Delete the original .git file

RUN rm .git

And copy the original subdmoule's .git directory instead of it:

COPY ./$SUBMODULE.gitdir .git

If you have nested submodules, you can fix them with absorbgitdirs

RUN apt-get update && apt-get install -y git
RUN git submodule absorbgitdirs
like image 183
Amir Gonnen Avatar answered Jan 30 '26 13:01

Amir Gonnen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!