Skip to content

Commit 919b169

Browse files
committed
fix(submodule): honor branch overrides in accessors
The accessors resolved `branch = .` from `.gitmodules` even when the superproject's Git config selected a named branch. This exposed the wrong branch on an attached superproject and raised while detached. Use `_branch_for_update()` to apply the same override precedence as remote updates. Preserve the accessor-specific error for unresolved inheritance and the recursive Git lookup for symbolic `HEAD` aliases. Only look up an override when the submodule has a name, so callers can still read an explicit branch path on an unnamed instance. The override and unnamed-path regressions failed before their fixes. The focused branch accessor, override, and alias tests pass under both the CLI and Gix backends. Agent-Model: unknown
1 parent be93e83 commit 919b169

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

‎doc/source/changes.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ API changes
202202
literally, so an entry named ``*.txt`` no longer removes every ``.txt`` file.
203203
Path strings keep their pathspec semantics. ``Submodule.move()`` and
204204
``Submodule.remove()`` remove the submodule's own index entry the same way.
205-
* With ``branch = .``, ``Submodule.branch``, ``branch_name`` and ``branch_path``
206-
return the superproject's current branch instead of ``.``, matching
205+
* ``Submodule.branch``, ``branch_name`` and ``branch_path`` honor a branch
206+
override in the superproject's Git configuration. With an effective
207+
``branch = .``, they return the superproject's current branch, matching
207208
``git submodule update --remote``, and raise ``ValueError`` while the
208209
superproject HEAD is detached. When a submodule's URL changes,
209210
``RootModule.update()`` looks for the submodule's own branch on the new remote,

‎git/objects/submodule/base.py‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,16 +1835,15 @@ def branch_path(self) -> PathLike:
18351835
the remote and track
18361836
18371837
:note:
1838-
A configured branch of ``.`` resolves to the path of the superproject's
1838+
The superproject's Git configuration takes precedence over ``.gitmodules``.
1839+
An effective branch of ``.`` resolves to the path of the superproject's
18391840
current branch.
18401841
18411842
:raise ValueError:
18421843
If the configured branch is ``.`` and the superproject HEAD is detached.
18431844
"""
1844-
if os.fspath(self._branch_path) not in (".", "refs/heads/."):
1845-
return self._branch_path
1846-
branch = self._superproject_branch()
1847-
if branch is None:
1845+
branch, _ = self._branch_for_update()
1846+
if os.fspath(branch.path) == "refs/heads/.":
18481847
raise ValueError(
18491848
f"Cannot resolve branch = . of submodule {self.name!r} because the superproject HEAD is detached"
18501849
)
@@ -1879,16 +1878,18 @@ def _superproject_branch(self) -> Union["Head", None]:
18791878

18801879
def _branch_for_update(self, *, to_latest_revision: bool = False) -> Tuple["Head", bool]:
18811880
"""Return the clone branch and whether it selects the latest revision."""
1882-
key = f"submodule.{self.name}.{self.k_head_option}"
1883-
try:
1884-
override = self.repo.git._call_process_safe("config", "--get", key)
1885-
except GitCommandError as exc:
1886-
if exc.status != 1:
1887-
raise
1888-
override = None
1881+
name = getattr(self, "name", None)
1882+
override = None
1883+
if name is not None:
1884+
key = f"submodule.{name}.{self.k_head_option}"
1885+
try:
1886+
override = self.repo.git._call_process_safe("config", "--get", key)
1887+
except GitCommandError as exc:
1888+
if exc.status != 1:
1889+
raise
18891890

18901891
branch = mkhead(self.repo, self._branch_path if override is None else override)
1891-
if branch.name != ".":
1892+
if os.fspath(branch.path) != "refs/heads/.":
18921893
return branch, override is not None
18931894
superproject_branch = self._superproject_branch()
18941895
if superproject_branch is None:

‎test/test_submodule.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,13 +2016,25 @@ def test_update_to_latest_revision_respects_local_branch_override(self):
20162016
assert module.active_branch.name == "master"
20172017
assert module.head.commit == source.heads.master.commit
20182018

2019+
assert submodule.branch_name == "master"
2020+
assert submodule.branch_path == "refs/heads/master"
2021+
branch = submodule.branch
2022+
with branch.repo:
2023+
assert branch.name == "master"
2024+
20192025
parent.git.config("--local", "submodule.module.branch", ".")
2026+
assert submodule.branch_name == "dev"
20202027
submodule.update(to_latest_revision=True, no_fetch=True)
20212028
with submodule.module() as module:
20222029
assert module.head.commit == source.heads.dev.commit
20232030

20242031
parent.head.set_reference(parent.head.commit)
20252032
parent.git.config("--local", "submodule.module.branch", "master")
2033+
assert submodule.branch_name == "master"
2034+
assert submodule.branch_path == "refs/heads/master"
2035+
branch = submodule.branch
2036+
with branch.repo:
2037+
assert branch.name == "master"
20262038
submodule.update(to_latest_revision=True, no_fetch=True)
20272039
with submodule.module() as module:
20282040
assert module.head.commit == source.heads.master.commit
@@ -2088,6 +2100,10 @@ def test_branch_accessors_accept_path_like_branch(self):
20882100
with branch.repo:
20892101
assert branch.path == branch_path
20902102

2103+
unnamed = Submodule(parent, configured.binsha, branch_path=branch_path)
2104+
assert unnamed.branch_path is branch_path
2105+
assert unnamed.branch_name == "dev"
2106+
20912107
@patch_git_config("protocol.file.allow", "always")
20922108
def test_branch_dot_accessors_follow_superproject_branch(self):
20932109
# The submodule URL points to this private fixture's source.

0 commit comments

Comments
 (0)