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=(