Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork938
Description
We have encountered an issue withsubmodule.update
that reproduces in a rather special circumstance:
Setup
Have a git repo, saytest
, containing one submodule, saytest_submodule
.test_submodule
must have nomaster
branch (let say the default branch is calleddevelop
instead).
Check out a revision oftest
that points to the tip ofdevelop
fortest_submodule
, but donotgit submodule init
the submodule. The directory hierarchy should look like this:
test/ ... contents of repo ... test_submodule/ <empty>
Now, create aRepo
fortest
, and callrepo.submodules[0].update(init=True)
Expected results
The tip ofdevelop
is checked out intest_submodule
and the index is clean.
Actual resultstest_submodule
is pointing at the tip of develop, but all of the files are staged for deletion. Additionally, there is a warning printed:
Failed to checkout tracking branch refs/heads/master
My analysis of why this happens
Before any bad behavior happens,submodule.update
clones the submodule repository with-n
, which means the clone does not actually check out the commit the clone ends up with. Remember this for later.
After cloning, we begin the process of updating the submodule to match what the parent repo specifies.submodule.update
takes an optionalbranch
argument, which defaults toNone
. Whenbranch
isNone
, we assume the branch ismaster
.Here, we try to find this branch and point HEAD to it, but of course in the repro case this fails because the branch does not exist. This is crucial, because this means we skip the line that marks the repo as "not checked out" by pointing the branch to the"NULL" sha.
Now, recall that a requirement of the repro is thattest
points to the tip ofdevelop
fortest_submodule
. Since we did not move the repo to the NULL sha beforehand, the repo is already at the desired sha when we arriveat this conditional. Therefore, the condition evaluates to false, and we skip all the code that actually checks out code.
Finally, recall our-n
checkout from the first paragraph. Since we did not check out any code after cloning, the repo is left in an un-checked-out state, which is exactly the "Actual results" state described above.
Closing thoughts
We can workaround this issue simply by adding a dummymaster
branch totest_submodule
, but this should not be required. Ideally,submodule.update
does not require a valid branch to operate correctly, sincegit submodule update --init
works just fine without one.