Skip to content

Commit 177a279

Browse files
committed
fix(submodule): resolve inherited branches before migrating remotes
A latest-revision update on a detached superproject could fail after creating `__new_origin__`. A retry saw its matching URL and kept fetching from the original `origin` instead of migrating the remote. Resolve the branch before adding or fetching the temporary remote. The scripted regression verifies that the failed update leaves only the original remote and the retry uses the new remote's newer tip. The regression reproduced the leaked remote before the fix. Six URL change tests pass under both the CLI and Gix backends. Agent-Model: unknown
1 parent 919b169 commit 177a279

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎git/objects/submodule/root.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ def update( # type: ignore[override]
304304
# offline updates instead of replacing the remote.
305305
previous_remote.set_url(git.Git.polish_url(sm_url))
306306
else:
307+
branch, _ = sm._branch_for_update(to_latest_revision=to_latest_revision)
307308
assert nn not in [r.name for r in rmts]
308309
smr = smm.create_remote(nn, sm_url)
309310
with smm.git.custom_environment(**sm._transport_env()):
@@ -312,7 +313,6 @@ def update( # type: ignore[override]
312313
# If we have a tracking branch, it should be available
313314
# in the new remote as well. An inherited branch has no
314315
# name while the superproject HEAD is detached.
315-
branch, _ = sm._branch_for_update(to_latest_revision=to_latest_revision)
316316
if branch.name == ".":
317317
_logger.warning("Failed to resolve tracking branch %s", branch.path)
318318
elif len([r for r in smr.refs if r.remote_head == branch.name]) == 0:

‎test/test_submodule.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,25 @@ def test_root_update_detects_change_to_inherited_branch(self):
21662166
assert module.head.is_detached
21672167
assert module.head.commit.binsha == submodule.binsha
21682168

2169+
@patch_git_config("protocol.file.allow", "always")
2170+
def test_root_update_retries_url_change_after_detached_superproject(self):
2171+
# The submodule URLs point to this private fixture's source and replacement.
2172+
rwdir = self._fixture("root-url-branch-dot", execute=True)
2173+
with git.Repo(rwdir / "clone") as parent, git.Repo(rwdir / "moved") as moved:
2174+
root = RootModule(parent)
2175+
with parent.submodules[0].module() as module:
2176+
with pytest.raises(ValueError, match="superproject HEAD is detached"):
2177+
root.update(previous_commit="before-url-change", to_latest_revision=True)
2178+
2179+
assert {remote.name for remote in module.remotes} == {"origin"}
2180+
assert Path(module.remotes.origin.url).samefile(rwdir / "source")
2181+
2182+
parent.git.checkout("dev")
2183+
root.update(previous_commit="before-url-change", to_latest_revision=True)
2184+
assert {remote.name for remote in module.remotes} == {"origin"}
2185+
assert Path(module.remotes.origin.url).samefile(rwdir / "moved")
2186+
assert module.head.commit == moved.heads.dev.commit
2187+
21692188
@patch_git_config("protocol.file.allow", "always")
21702189
def test_root_update_url_change_checks_submodule_branch(self):
21712190
# The submodule URL points to this private fixture's source.

‎tests/fixtures/submodule-cases.sh‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ case "$scenario" in
4646
deinit-nonempty) printf '%s' 'user content' > parent/module/file ;;
4747
esac
4848
;;
49-
branch-dot|branch-dot-alias)
49+
branch-dot|branch-dot-alias|root-url-branch-dot)
5050
git init -q -b master source
5151
git -C source commit --allow-empty -qm initial
5252
git -C source branch dev
@@ -64,6 +64,15 @@ case "$scenario" in
6464
git -C clone branch master refs/remotes/origin/master
6565
git -C clone symbolic-ref refs/heads/dev refs/heads/master
6666
fi
67+
if test "$scenario" = root-url-branch-dot; then
68+
git -C clone submodule update --init
69+
git -C clone tag before-url-change
70+
git clone -q -b dev source moved
71+
git -C moved commit --allow-empty -qm 'Advance dev on the replacement remote'
72+
git -C clone config -f .gitmodules submodule.module.url "$root/moved"
73+
git -C clone commit -qam 'Change submodule URL'
74+
git -C clone checkout -q --detach HEAD
75+
fi
6776
;;
6877
add-empty) git init -q -b master empty-repo ;;
6978
relative)

0 commit comments

Comments
 (0)