1

I'm trying to come up with a way to handle Git project dependencies like the Swift Package Manager.

I'd like to specifically capture that, let's say my app version 1.2, depends on framework A, version 1.0.

When I then checkout v1.2 of my app, the framework code should be automatically be pulled in at tag v1.0.

code/myapp/               tag v1.2
           lib/frameworkA tag v1.0

I experimented with Git submodules, but I could not figure out a way to automatically checkout a specific submodule tag when I checkout a specific tag of the parent app.

The .gitmodules file is checked in as part of the parent project. Ideally it would contain details about which tag of the submodule to use. Is this possible at all? If not, is there maybe another way that doesn't use submodules?

Mark
  • 6,647
  • 1
  • 45
  • 88

1 Answers1

0

By default, git-checkout doesn't update the submodules.

However, you can change this behavior as of Git 2.14 by setting the submodule.recurse configuration option to true:

submodule.recurse
Specifies if commands recurse into submodules by default. This applies to all commands that have a --recurse-submodules option, except clone. Defaults to false.

As of Git 2.13, git-checkout got the --recurse-submodules option, so all you need to do is:

git config --global submodule.recurse true

and Git will automatically run git submodule update --recursive whenever you switch to a different branch.

As for associating a submodule with a specific tag, you can do it by manually checking out the tag within the submodule directory:

cd path/to/submodule
git checkout <submodule-tag-name>

At this point, the submodule will be pointing to the commit referenced by <tag-name>. Then, all you have to do is tell the parent project about the modified submodule:

cd path/to/parent/project
git add path/to/submodule
git commit -m "Sets submodule to tag <submodule-tag-name>"
git tag -a <parent-tag-name>

From this point on, whenever you check out <parent-tag-name> in the parent project, Git will automatically checkout the commit associated to <submodule-tag-name> in the submodule.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • Thanks for your answer. So if I run "git checkout v1.2" on the parent (app) project know which tag of the submodule to checkout? – Mark Aug 23 '19 at 12:26
  • @Mark Yes, assuming you've configured the submodule to point to that tag. I updated my answer with a short explanation. – Enrico Campidoglio Aug 23 '19 at 12:56
  • Last puzzle piece: how can I "persist" the information that I checked out tagX for the submodule? Let's say, I continue development and work on new versions of the app and the submodule. Therefore I have to checkout to the latest version of my submodule. A few weeks later I want to check out "tagX" of the app without having to manually check out "tagX" for the submodule. Is this possible? – Mark Aug 23 '19 at 14:04
  • @Mark Keeping in mind that a submodule is a reference to a _specific commit_ (not a tag) in another repository, yes, Git will update the submodule to whatever commit it's set to reference whenever you switch the parent project to `tagX`. I updated my answer with some more details. – Enrico Campidoglio Aug 26 '19 at 07:06