Skip to content

Commit 1dfe4e2

Browse files
JROtto5Byron
authored andcommitted
fix(submodule): resolve branch = . when update() clones a submodule
gitmodules(5) defines `submodule.<name>.branch = .` as "the same name as the superproject's current branch". With the superproject on `dev`, `git submodule update --init --remote` checks out the submodule's `dev` tip. When update() cloned such a submodule it looked for a remote branch literally named `.` and never found it: from `repo.submodules` it logged "Failed to checkout tracking branch refs/heads/." and stayed on the remote's default branch, so `to_latest_revision=True` moved the submodule to the wrong tip; after `set_parent_commit()` the branch is re-read without the refs/heads/ prefix and `branch_name` raised ValueError instead, for a named branch such as `dev` as well as `.`. Normalize the configured branch to a full ref first, then resolve `refs/heads/.` to the superproject's active branch before the remote-branch lookup. A detached superproject keeps the previous behaviour (the lookup fails and the warning is logged); git refuses in that case. Already-cloned submodules are not touched: update() follows their existing tracking branch, as before. The new `branch-dot` fixture puts the superproject clone on `dev`. `test_update_to_latest_revision_follows_superproject_branch` covers the `repo.submodules` path and the re-read path, and `test_update_to_latest_revision_after_reread_follows_named_branch` the re-read of `dev`; both fail without this change under the `cli` and `gix` backends and pass with it. A third test pins the detached case. Agent-Model: claude-opus-5-5
1 parent 06aa1cd commit 1dfe4e2

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎git/objects/submodule/base.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,18 +1097,22 @@ def fetch_remotes(module_repo: "Repo") -> None:
10971097

10981098
if not dry_run:
10991099
# See whether we have a valid branch to check out.
1100+
branch_path = git.Head.to_full_path(self.branch_path)
1101+
# gitmodules(5): branch "." names the superproject's current branch.
1102+
if branch_path == git.Head.to_full_path(".") and not self.repo.head.is_detached:
1103+
branch_path = self.repo.head.reference.path
11001104
try:
11011105
mrepo = cast("Repo", mrepo)
1102-
remote_branch = find_first_remote_branch(mrepo.remotes, self.branch_name)
1103-
local_branch = mkhead(mrepo, self.branch_path)
1106+
remote_branch = find_first_remote_branch(mrepo.remotes, git.Head(mrepo, branch_path).name)
1107+
local_branch = mkhead(mrepo, branch_path)
11041108
local_branch.set_object(remote_branch.commit)
11051109
mrepo.head.set_reference(
11061110
local_branch,
11071111
logmsg="submodule: attaching head to %s" % local_branch,
11081112
)
11091113
mrepo.head.reference.set_tracking_branch(remote_branch)
11101114
except (IndexError, InvalidGitRepositoryError):
1111-
_logger.warning("Failed to checkout tracking branch %s", self.branch_path)
1115+
_logger.warning("Failed to checkout tracking branch %s", branch_path)
11121116

11131117
with self.repo.config_writer() as writer:
11141118
writer.set_value(sm_section(self.name), "url", self.url)

‎test/test_submodule.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,54 @@ def test_update_to_latest_revision_after_deinit_fetches_remote(self):
19431943
assert submodule.module().head.commit == source_repo.head.commit
19441944
assert osp.isfile(osp.join(submodule.abspath, "new-file"))
19451945

1946+
@patch_git_config("protocol.file.allow", "always")
1947+
def test_update_to_latest_revision_follows_superproject_branch(self):
1948+
for reread in (False, True):
1949+
with self.subTest(reread=reread):
1950+
# The submodule URL points to this private fixture's source.
1951+
rwdir = self._fixture("branch-dot", execute=True)
1952+
source_repo = git.Repo(rwdir / "source")
1953+
parent_repo = git.Repo(rwdir / "clone")
1954+
submodule = parent_repo.submodules[0]
1955+
if reread:
1956+
# Re-reads the branch from .gitmodules as ".", not "refs/heads/.".
1957+
submodule.set_parent_commit(parent_repo.head.commit)
1958+
1959+
submodule.update(to_latest_revision=True)
1960+
1961+
assert submodule.module().active_branch.name == "dev"
1962+
assert submodule.module().head.commit == source_repo.heads.dev.commit
1963+
1964+
@patch_git_config("protocol.file.allow", "always")
1965+
def test_update_to_latest_revision_after_reread_follows_named_branch(self):
1966+
rwdir = self._fixture("branch-dot", execute=True)
1967+
source_repo = git.Repo(rwdir / "source")
1968+
parent_repo = git.Repo(rwdir / "clone")
1969+
parent_repo.git.config("-f", ".gitmodules", "submodule.module.branch", "dev")
1970+
parent_repo.index.add([".gitmodules"])
1971+
parent_repo.index.commit("Track dev", skip_hooks=True)
1972+
submodule = parent_repo.submodules[0]
1973+
# Re-reads the branch from .gitmodules as "dev", not "refs/heads/dev".
1974+
submodule.set_parent_commit(parent_repo.head.commit)
1975+
1976+
submodule.update(to_latest_revision=True)
1977+
1978+
assert submodule.module().active_branch.name == "dev"
1979+
assert submodule.module().head.commit == source_repo.heads.dev.commit
1980+
1981+
@patch_git_config("protocol.file.allow", "always")
1982+
def test_update_branch_dot_with_detached_superproject_warns(self):
1983+
rwdir = self._fixture("branch-dot", execute=True)
1984+
parent_repo = git.Repo(rwdir / "clone")
1985+
parent_repo.head.set_reference(parent_repo.head.commit)
1986+
submodule = parent_repo.submodules[0]
1987+
1988+
# git refuses here too: there is no superproject branch to follow.
1989+
with self.assertLogs("git.objects.submodule.base", "WARNING") as logs:
1990+
submodule.update(to_latest_revision=True)
1991+
1992+
assert any("Failed to checkout tracking branch refs/heads/." in line for line in logs.output)
1993+
19461994
@patch_git_config("protocol.file.allow", "always")
19471995
def test_update_after_deinit_fetches_new_gitlink_commit(self):
19481996
rwdir = self._fixture("deinit-gitlink", execute=True)

‎tests/fixtures/submodule-cases.sh‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ case "$scenario" in
4646
deinit-nonempty) printf '%s' 'user content' > parent/module/file ;;
4747
esac
4848
;;
49+
branch-dot)
50+
git init -q -b master source
51+
git -C source commit --allow-empty -qm initial
52+
git -C source branch dev
53+
git -C source commit --allow-empty -qm 'Advance master'
54+
git -C source checkout -q dev
55+
git -C source commit --allow-empty -qm 'Advance dev'
56+
git -C source checkout -q master
57+
source="$root/source"
58+
add_module module module
59+
git -C parent config -f .gitmodules submodule.module.branch .
60+
git -C parent commit -qam 'Track the superproject branch'
61+
git -C parent branch dev
62+
git clone -q -b dev parent clone
63+
;;
4964
add-empty) git init -q -b master empty-repo ;;
5065
relative)
5166
git init -q -b master module

0 commit comments

Comments
 (0)