threads / discuss / 44410

Submodules: two confusing situations

Subject: Submodules: two confusing situations

## tl;dr

2 messages between Nov 1, 2016 and Nov 1, 2016.

replies: 1people: 2as markdown or json

David Turner· Nov 1, 2016, 22:13 UTC · lore
Consider the following two cases:
We have commit X and commit Y.  X is an ancestor of Y.

We're at X and doing get checkout Y -- or we're at Y and we're doing git checkout X.

Case 1: Between X and Y, we have deleted a submodule. In order to move from X to Y, git removes the submodule from the working tree.

Case 2: Between X and Y, we have instead added a submodule. In order to move from Y to X (that is, the opposite direction), git *does not* remove the submodule from the tree; instead, it gives a warning and leaves the submodule behind.

I don't understand why these two cases are not symmetric.
-- 
Perhaps relatedly, consider the attached shell-script, which I have not yet made into a full git test because I'm not sure I understand the issues fully.

It creates three commits:

Commit 1 adds a submodule
Commit 2 removes that submodule and re-adds it into a subdirectory (sub1 to sub1/sub1).
Commit 3 adds an unrelated file.

Then it checks out commit 1 (first deinitializing the submodule to avoid case 2 above), and attempts to cherry-pick commit 3.  This seems like it ought to work, based on my understanding of cherry-pick.  But in fact it gives a conflict on the stuff from commit 2 (which is unrelated to commit 3).

This is confusing to me, and looks like a bug.  What am I missing?
Stefan Beller· Nov 1, 2016, 22:56 UTC · re: David Turner · lore

Re: Submodules: two confusing situations

On Tue, Nov 1, 2016 at 3:13 PM, David Turner <David.Turner@twosigma.com> wrote:
Show 17 quoted lines
> Consider the following two cases:
>
> We have commit X and commit Y.  X is an ancestor of Y.
>
> We're at X and doing get checkout Y -- or we're at Y and we're doing
> git checkout X.
>
> Case 1: Between X and Y, we have deleted a submodule.
> In order to move from X to Y, git removes the submodule
> from the working tree.
>
> Case 2: Between X and Y, we have instead added a submodule.  In order
> to move from Y to X (that is, the opposite direction), git *does not*
> remove the submodule from the tree; instead, it gives a warning and
> leaves the submodule behind.
>
> I don't understand why these two cases are not symmetric.

Because you are using git-submodule.sh that is only an approximation of what is supposed to happen. ("git checkout [X | Y] && git submodule update" I'd guess).

"git submodule update" only *updates* the submodules and doesn't *delete* them at all. I think this originated from the historic behavior of having the submodules .git directory inside the submodule and not in the superprojects .git/module. When having the .git dir inside the submodule, you cannot delete the submodule as you'd potentially loose information (local commits) from the submodule.

I am currently working on implementing a flag --recurse-submodules for git-checkout that would be symmetrical from X -> Y and back, but that feature hasn't seen the mailing list yet as I discovered yet another bug locally. I think I found a reviewer though. ;)

The problem with doing a propoer checkout, i.e. update and deletion of
a submodule,
you need to be sure the submodule in its state can go away:
* no untracked files that are lost (except for gitignored files)
* clean index in the submodule and
* the submodule points at the recorded sha1 (i.e. there are no
  commits that would be dangling)
Show 10 quoted lines
>
> Perhaps relatedly, consider the attached shell-script, which I have not yet made into a full git test because I'm not sure I understand the issues fully.
>
> It creates three commits:
>
> Commit 1 adds a submodule
> Commit 2 removes that submodule and re-adds it into a subdirectory (sub1 to sub1/sub1).
> Commit 3 adds an unrelated file.
>
> Then it checks out commit 1 (first deinitializing the submodule to avoid case 2 above), and attempts to cherry-pick commit 3.  This seems like it ought to work, based on my understanding of cherry-pick.  But in fact it gives a conflict on the stuff from commit 2 (which is unrelated to commit 3).
That sounds like a bug indeed.
>
> This is confusing to me, and looks like a bug.  What am I missing?
>

← back to recent threads