Skip to content

Commit da0fe0d

Browse files
codexByron
authored andcommitted
fix(submodule): follow the current superproject branch on update
When a submodule with `branch = .` was cloned on `dev`, subsequent `update(to_latest_revision=True)` calls kept using `origin/dev` after the superproject switched to `master`. The branch selection only resolved `.` during cloning. Resolve the remote-tracking ref from the current superproject branch when selecting the latest revision. If the selected ref differs from the submodule's upstream, check out its commit detached to preserve the local branch. The regression switches the superproject from `dev` to `master`, fetches a newer `master` commit, and switches back using cached refs. It failed before the fix and passes with the CLI and Gix backends. Git's `submodule update --remote` selects the same commits. Agent-Model: gpt-6.2-sol Agent-Reasoning-Effort: high
1 parent df722c2 commit da0fe0d

2 files changed

Lines changed: 52 additions & 20 deletions

File tree

‎git/objects/submodule/base.py‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,9 @@ def update(
901901
the remote will be fetched, and the local tracking branch updated. This only
902902
works if we have a local tracking branch, which is the case if the remote
903903
repository had a master branch, or if the ``branch`` option was specified
904-
for this submodule and the branch existed remotely.
904+
for this submodule and the branch existed remotely. With ``branch = .``
905+
and an active superproject branch, the submodule follows the remote branch
906+
of the same name and checks it out detached if it differs from the upstream.
905907
906908
:param progress:
907909
:class:`UpdateProgress` instance, or ``None`` if no progress should be
@@ -1122,31 +1124,36 @@ def fetch_remotes(module_repo: "Repo") -> None:
11221124
#############################
11231125
binsha = self.binsha
11241126
hexsha = self.hexsha
1125-
is_detached = False
1127+
checkout_detached = False
11261128
if mrepo is not None:
11271129
# mrepo is only set if we are not in dry-run mode or if the module
11281130
# existed.
1129-
is_detached = mrepo.head.is_detached
1131+
checkout_detached = mrepo.head.is_detached
11301132
# END handle dry_run
11311133

11321134
if mrepo is not None and to_latest_revision:
11331135
msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir
1134-
if not is_detached:
1135-
rref = mrepo.head.reference.tracking_branch()
1136-
if rref is not None:
1137-
rcommit = rref.commit
1138-
binsha = rcommit.binsha
1139-
hexsha = rcommit.hexsha
1140-
else:
1141-
_logger.error(
1142-
"%s a tracking branch was not set for local branch '%s'",
1143-
msg_base,
1144-
mrepo.head.reference,
1145-
)
1146-
# END handle remote ref
1147-
else:
1136+
upstream = None if checkout_detached else mrepo.active_branch.tracking_branch()
1137+
rref = upstream
1138+
if (
1139+
git.Head.to_full_path(self.branch_path) == git.Head.to_full_path(".")
1140+
and not self.repo.head.is_detached
1141+
):
1142+
remote = upstream.remote_name if isinstance(upstream, git.RemoteReference) else "origin"
1143+
rref = git.RemoteReference(mrepo, f"refs/remotes/{remote}/{self.repo.active_branch.name}")
1144+
# Keep the local branch pointing at its own history when following another branch.
1145+
checkout_detached = rref != upstream
1146+
1147+
if rref is not None:
1148+
rcommit = rref.commit
1149+
binsha = rcommit.binsha
1150+
hexsha = rcommit.hexsha
1151+
elif checkout_detached:
11481152
_logger.error("%s there was no local tracking branch", msg_base)
1149-
# END handle detached head
1153+
else:
1154+
_logger.error(
1155+
"%s a tracking branch was not set for local branch '%s'", msg_base, mrepo.head.reference
1156+
)
11501157
# END handle to_latest_revision option
11511158

11521159
# Update the working tree.
@@ -1169,7 +1176,7 @@ def fetch_remotes(module_repo: "Repo") -> None:
11691176
else:
11701177
msg = "Skipping %s on branch '%s' of submodule repo '%s' as it contains un-pushed commits"
11711178
msg %= (
1172-
is_detached and "checkout" or "reset",
1179+
checkout_detached and "checkout" or "reset",
11731180
mrepo.head,
11741181
mrepo,
11751182
)
@@ -1193,7 +1200,7 @@ def fetch_remotes(module_repo: "Repo") -> None:
11931200
)
11941201

11951202
if not dry_run and may_reset:
1196-
if is_detached:
1203+
if checkout_detached:
11971204
# NOTE: For now we force. The user is not supposed to change
11981205
# detached submodules anyway. Maybe at some point this becomes
11991206
# an option, to properly handle user modifications - see below

‎test/test_submodule.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,31 @@ def test_update_to_latest_revision_follows_superproject_branch(self):
19611961
assert submodule.module().active_branch.name == "dev"
19621962
assert submodule.module().head.commit == source_repo.heads.dev.commit
19631963

1964+
@patch_git_config("protocol.file.allow", "always")
1965+
def test_update_to_latest_revision_after_superproject_switch(self):
1966+
# The submodule URL points to this private fixture's source.
1967+
rwdir = self._fixture("branch-dot", execute=True)
1968+
with git.Repo(rwdir / "source") as source, git.Repo(rwdir / "clone") as parent:
1969+
submodule = parent.submodules[0]
1970+
submodule.update(to_latest_revision=True)
1971+
with submodule.module() as module:
1972+
assert module.active_branch.tracking_branch() == module.remotes.origin.refs.dev
1973+
assert module.head.commit == source.heads.dev.commit
1974+
1975+
parent.git.checkout("master")
1976+
source.index.commit("Advance master again", skip_hooks=True)
1977+
submodule.update(to_latest_revision=True)
1978+
1979+
with submodule.module() as module:
1980+
assert module.head.commit == source.heads.master.commit
1981+
assert module.heads.dev.commit == source.heads.dev.commit
1982+
1983+
parent.git.checkout("dev")
1984+
submodule.update(to_latest_revision=True, no_fetch=True)
1985+
1986+
with submodule.module() as module:
1987+
assert module.head.commit == source.heads.dev.commit
1988+
19641989
@patch_git_config("protocol.file.allow", "always")
19651990
def test_update_to_latest_revision_after_reread_follows_named_branch(self):
19661991
rwdir = self._fixture("branch-dot", execute=True)

0 commit comments

Comments
 (0)