Skip to content

Commit be93e83

Browse files
committed
fix(submodule): preserve path-like branch paths
The inheritance check called `Head.name` on the original argument to `Submodule.__init__()`. A full `PurePosixPath` caused both `branch_path` and `branch` to raise because the name lookup uses string methods. Compare the filesystem representation with the inheritance marker and preserve the original named path. Use a string for the shortest-name lookup as well. The new regression failed before the fix. It and the existing branch inheritance accessor tests pass with both the CLI and Gix backends. Agent-Model: unknown
1 parent 2600a4e commit be93e83

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎git/objects/submodule/base.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ def branch_path(self) -> PathLike:
18411841
:raise ValueError:
18421842
If the configured branch is ``.`` and the superproject HEAD is detached.
18431843
"""
1844-
if git.Head(self.repo, self._branch_path).name != ".":
1844+
if os.fspath(self._branch_path) not in (".", "refs/heads/."):
18451845
return self._branch_path
18461846
branch = self._superproject_branch()
18471847
if branch is None:
@@ -1865,7 +1865,7 @@ def branch_name(self) -> str:
18651865
"""
18661866
# Use an instance method, for this we create a temporary Head instance which
18671867
# uses a repository that is available at least (it makes no difference).
1868-
return git.Head(self.repo, self.branch_path).name
1868+
return git.Head(self.repo, os.fspath(self.branch_path)).name
18691869

18701870
def _superproject_branch(self) -> Union["Head", None]:
18711871
"""Return the superproject's current branch, or ``None`` if its HEAD is detached."""

‎test/test_submodule.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from io import BytesIO
66
import os
77
import os.path as osp
8-
from pathlib import Path
8+
from pathlib import Path, PurePosixPath
99
import shutil
1010
import sys
1111
from types import SimpleNamespace
@@ -2071,6 +2071,23 @@ def test_update_branch_dot_rejects_latest_revision_with_detached_superproject(se
20712071
with submodule.module() as module:
20722072
assert module.head.commit.binsha == submodule.binsha
20732073

2074+
@patch_git_config("protocol.file.allow", "always")
2075+
def test_branch_accessors_accept_path_like_branch(self):
2076+
# The submodule URL points to this private fixture's source.
2077+
rwdir = self._fixture("branch-dot", execute=True)
2078+
with git.Repo(rwdir / "clone") as parent:
2079+
configured = parent.submodules[0]
2080+
configured.update()
2081+
branch_path = PurePosixPath("refs/heads/dev")
2082+
submodule = Submodule(
2083+
parent, configured.binsha, path=configured.path, name=configured.name, branch_path=branch_path
2084+
)
2085+
assert submodule.branch_path is branch_path
2086+
assert submodule.branch_name == "dev"
2087+
branch = submodule.branch
2088+
with branch.repo:
2089+
assert branch.path == branch_path
2090+
20742091
@patch_git_config("protocol.file.allow", "always")
20752092
def test_branch_dot_accessors_follow_superproject_branch(self):
20762093
# The submodule URL points to this private fixture's source.

0 commit comments

Comments
 (0)