From 175f494c657c9de87682ef7bbbacd6f536de89ba Mon Sep 17 00:00:00 2001 From: JROtto5 <222316562+JROtto5@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:54:47 -0500 Subject: [PATCH] fix(submodule): read `branch` as a string when re-reading `.gitmodules` `Submodule.iter_items()` reads `submodule..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 --- git/objects/submodule/base.py | 16 +++++++--------- test/test_submodule.py | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 615b9b777..b64a8f9e6 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -178,11 +178,7 @@ def _set_cache_(self, attr: str) -> None: if native is not NotImplemented and self.name in native: values = native[self.name] self.path, self._url = values["path"], values["url"] - self._branch_path = ( - GitConfigParser._string_to_value(values[self.k_head_option]) - if self.k_head_option in values - else git.Head.to_full_path(self.k_head_default) - ) + self._branch_path = git.Head.to_full_path(values.get(self.k_head_option, self.k_head_default)) return # Default submodule values. try: @@ -196,7 +192,9 @@ def _set_cache_(self, attr: str) -> None: self._url = reader.get("url") # GitPython extension values - optional. - self._branch_path = reader.get_value(self.k_head_option, git.Head.to_full_path(self.k_head_default)) + # Not get_value(): it turns `branch = 1.10` into the float 1.1. + branch = reader.get(self.k_head_option, fallback=None) + self._branch_path = git.Head.to_full_path(self.k_head_default if branch is None else branch) elif attr == "_name": raise AttributeError("Cannot retrieve the name of a submodule if it was not set initially") else: @@ -1933,9 +1931,9 @@ def iter_items( if native is NotImplemented: p = parser.get(sms, "path") u = parser.get(sms, "url") - b = cls.k_head_default - if parser.has_option(sms, cls.k_head_option): - b = str(parser.get(sms, cls.k_head_option)) + b = parser.get(sms, cls.k_head_option, fallback=None) + if b is None: + b = cls.k_head_default else: p, u = native[n]["path"], native[n]["url"] b = native[n].get(cls.k_head_option, cls.k_head_default) diff --git a/test/test_submodule.py b/test/test_submodule.py index f293e8742..9a4989d4f 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -2096,6 +2096,31 @@ def test_list_only_valid_submodules(self): with git.Repo(self._fixture("list-valid", write=False) / "parent") as repo: assert len(repo.submodules) == 0 + @patch_git_config("protocol.file.allow", "always") + def test_reread_branch_matches_iter_items(self): + rwdir = self._fixture("module") + with git.Repo(rwdir / "parent") as repo: + for branch in ("1.10", "2", "true", "dev", "refs/heads/dev", None): + with self.subTest(branch=branch): + if branch is None: + repo.git.config("-f", ".gitmodules", "--unset", "submodule.module.branch") + with open(rwdir / "parent" / ".gitmodules", "a") as gitmodules: + gitmodules.write("\tbranch\n") + else: + repo.git.config("-f", ".gitmodules", "submodule.module.branch", branch) + repo.index.add([".gitmodules"]) + repo.index.commit("Track %s" % branch, skip_hooks=True) + expected = git.Head.to_full_path(Submodule.k_head_default if branch is None else branch) + + submodule = repo.submodules[0] + assert submodule.branch_path == expected + # Both clear the cache, so the branch is read from .gitmodules again. + submodule.set_parent_commit(repo.head.commit) + assert submodule.branch_path == expected + assert submodule.branch_name == git.Head(repo, expected).name + submodule._clear_cache() + assert submodule.branch_path == expected + @pytest.mark.xfail( sys.platform == "win32", reason=(