Skip to content

Commit 15100e1

Browse files
JROtto5Byron
authored andcommitted
fix(submodule): read branch as a string when re-reading .gitmodules
`Submodule.iter_items()` reads `submodule.<name>.branch` as a string and turns it into a full ref, so `branch = 1.10` gives `refs/heads/1.10`. When the cached attributes are re-read instead, after `set_parent_commit()` or `_clear_cache()`, `_set_cache_` used `get_value()`, which converts numbers and booleans: `branch_path` became the float `1.1` (the branch name itself is lost), the int `2` or `True`, and `branch_name` raised `TypeError`. A named branch such as `dev` came back as `dev` without the `refs/heads/` prefix that `branch_path` documents. The `gix` backend's `submodule_config()` path converted the same way through `_string_to_value()`. Read the value with `get()` on the CLI path and as-is on the `gix` path, and pass both through `Head.to_full_path()`, as `iter_items()` does. Values that already are full refs are unchanged. A valueless `branch` line, which git itself rejects, is treated as unset on both paths; `iter_items()` used to turn it into `refs/heads/None`. `test_reread_branch_matches_iter_items` sets the branch to `1.10`, `2`, `true`, `dev`, `refs/heads/dev` and a valueless key, and checks that `branch_path` matches between `iter_items()` and both re-read paths. Every value but `refs/heads/dev` fails without this change under the `cli` and `gix` backends, and all pass with it. Agent-Model: claude-opus-5-5
1 parent 0816ef5 commit 15100e1

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎git/objects/submodule/base.py‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ def _set_cache_(self, attr: str) -> None:
179179
if native is not NotImplemented and self.name in native:
180180
values = native[self.name]
181181
self.path, self._url = values["path"], values["url"]
182-
self._branch_path = (
183-
GitConfigParser._string_to_value(values[self.k_head_option])
184-
if self.k_head_option in values
185-
else git.Head.to_full_path(self.k_head_default)
186-
)
182+
self._branch_path = git.Head.to_full_path(values.get(self.k_head_option, self.k_head_default))
187183
return
188184
# Default submodule values.
189185
try:
@@ -197,7 +193,9 @@ def _set_cache_(self, attr: str) -> None:
197193

198194
self._url = reader.get("url")
199195
# GitPython extension values - optional.
200-
self._branch_path = reader.get_value(self.k_head_option, git.Head.to_full_path(self.k_head_default))
196+
# Not get_value(): it turns `branch = 1.10` into the float 1.1.
197+
branch = reader.get(self.k_head_option, fallback=None)
198+
self._branch_path = git.Head.to_full_path(self.k_head_default if branch is None else branch)
201199
elif attr == "_name":
202200
raise AttributeError("Cannot retrieve the name of a submodule if it was not set initially")
203201
else:
@@ -1976,9 +1974,9 @@ def iter_items(
19761974
if native is NotImplemented:
19771975
p = parser.get(sms, "path")
19781976
u = parser.get(sms, "url")
1979-
b = cls.k_head_default
1980-
if parser.has_option(sms, cls.k_head_option):
1981-
b = str(parser.get(sms, cls.k_head_option))
1977+
b = parser.get(sms, cls.k_head_option, fallback=None)
1978+
if b is None:
1979+
b = cls.k_head_default
19821980
else:
19831981
p, u = native[n]["path"], native[n]["url"]
19841982
b = native[n].get(cls.k_head_option, cls.k_head_default)

‎test/test_submodule.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,31 @@ def test_list_only_valid_submodules(self):
22252225
with git.Repo(self._fixture("list-valid", write=False) / "parent") as repo:
22262226
assert len(repo.submodules) == 0
22272227

2228+
@patch_git_config("protocol.file.allow", "always")
2229+
def test_reread_branch_matches_iter_items(self):
2230+
rwdir = self._fixture("module")
2231+
with git.Repo(rwdir / "parent") as repo:
2232+
for branch in ("1.10", "2", "true", "dev", "refs/heads/dev", None):
2233+
with self.subTest(branch=branch):
2234+
if branch is None:
2235+
repo.git.config("-f", ".gitmodules", "--unset", "submodule.module.branch")
2236+
with open(rwdir / "parent" / ".gitmodules", "a") as gitmodules:
2237+
gitmodules.write("\tbranch\n")
2238+
else:
2239+
repo.git.config("-f", ".gitmodules", "submodule.module.branch", branch)
2240+
repo.index.add([".gitmodules"])
2241+
repo.index.commit("Track %s" % branch, skip_hooks=True)
2242+
expected = git.Head.to_full_path(Submodule.k_head_default if branch is None else branch)
2243+
2244+
submodule = repo.submodules[0]
2245+
assert submodule.branch_path == expected
2246+
# Both clear the cache, so the branch is read from .gitmodules again.
2247+
submodule.set_parent_commit(repo.head.commit)
2248+
assert submodule.branch_path == expected
2249+
assert submodule.branch_name == git.Head(repo, expected).name
2250+
submodule._clear_cache()
2251+
assert submodule.branch_path == expected
2252+
22282253
@pytest.mark.xfail(
22292254
sys.platform == "win32",
22302255
reason=(

0 commit comments

Comments
 (0)