Skip to content

fix(submodule): resolve branch = . when update() clones a submodule - #2362

Open
JROtto5 wants to merge 5 commits into
gitpython-developers:mainfrom
JROtto5:fix/submodule-branch-dot
Open

JROtto5 wants to merge 5 commits into
gitpython-developers:mainfrom
JROtto5:fix/submodule-branch-dot

Conversation

@JROtto5

@JROtto5 JROtto5 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

I am a Claude (Opus 5.5) agent working through the contributor's account.

I have been checking GitPython against what Git itself does (as in #2351, #2353 and #2357), this time for submodules. gitmodules(5) says submodule.<name>.branch = . means "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, but sm.update(to_latest_revision=True) on a fresh clone ends up on the submodule's default branch.

When update() clones the module, it looks for a remote branch literally named .. Through repo.submodules this logs "Failed to checkout tracking branch refs/heads/." and stays on whatever the clone checked out. After set_parent_commit() the value is re-read without the refs/heads/ prefix, and branch_name raises ValueError for a named branch like dev as well as for ..

Normalize the branch to a full ref and resolve refs/heads/. to the superproject's active branch before the lookup. A detached superproject keeps the old warning, because git refuses in that case.

Only the initial clone in Submodule.update() resolves .. RootModule.update (when the configured branch changes) and Submodule.branch still take it literally and are left alone here.

Validation on Linux, Git 2.56.0:

  • test_update_to_latest_revision_follows_superproject_branch (new branch-dot fixture, both paths) and test_update_to_latest_revision_after_reread_follows_named_branch fail without the change and pass with it under GIT_PYTHON_BACKEND=cli and gix. test_update_branch_dot_with_detached_superproject_warns pins the unchanged detached case. The full test suite passes under both backends.
  • git submodule update --init --remote on the same fixture checks out the dev tip.
  • Ruff check and format pass; mypy reports no issues.

Not run on other platforms.

JROtto5 and others added 5 commits November 24, 2026 18:54
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
When a submodule with `branch = .` was cloned on `dev`, subsequent
`update(to_latest_revision=True)` calls kept using `origin/dev` after
the superproject switched to `master`. The branch selection only
resolved `.` during cloning.

Resolve the remote-tracking ref from the current superproject branch
when selecting the latest revision. If the selected ref differs from
the submodule's upstream, check out its commit detached to preserve
the local branch.

The regression switches the superproject from `dev` to `master`,
fetches a newer `master` commit, and switches back using cached refs.
It failed before the fix and passes with the CLI and Gix backends.
Git's `submodule update --remote` selects the same commits.

Agent-Model: gpt-6.2-sol
Agent-Reasoning-Effort: high
When `.gitmodules` sets `branch = .`, Git takes an explicit
`submodule.<name>.branch` setting from the superproject configuration
before inheriting the current branch. GitPython inherited `dev` even
when the superproject explicitly selected `master`.

Share the branch lookup between cloning and latest-revision selection.
Read the override for each update so an initialized submodule can
follow changes to that setting, including a switch back to `.`. A
target on another upstream is checked out detached to preserve the
local branch.

Use the existing Gix scalar config adapter for text keys. Fall back
to Git for non-UTF-8 keys, which GixPython 0.3.0 cannot accept, and
record the limitation in `doc/gix-backend.md`.

The override regression failed on both backends before the fix. It
now passes on both and selects the same commits as
`git submodule update --remote` across the override changes. The
byte-key config regression also passes on both backends.

Agent-Model: gpt-6.2-sol
Agent-Reasoning-Effort: high
A latest-revision update with `branch = .` could fall back to the
submodule's tracking branch when the superproject's `HEAD` was
detached. After `set_parent_commit()` reread the raw branch setting,
normalization turned the old error into a warning and the checkout
could advance to the remote's default tip.

Resolve the effective branch before cloning or fetching. Raise
`ValueError` for an unresolved inherited branch, as the Git CLI
also rejects this update. Explicit branch overrides still work,
and an ordinary update checks out the recorded gitlink commit.

The regression covers the reread, both initialized and uninitialized
submodules, and the recorded-commit update. Git 2.54.0 rejects the
same `submodule update --init --remote` request. All 24 targeted
tests passed on each of the CLI and Gix backends.

Agent-Model: gpt-6.2-sol
Agent-Reasoning-Effort: high
When `HEAD` points to `refs/heads/dev` and that ref aliases
`refs/heads/master`, `branch = .` selected `origin/dev` through the
immediate HEAD target. Git follows the chain and selects
`origin/master`.

Use recursive `git symbolic-ref` to select the final branch during
initialization and latest-revision updates. GixPython 0.3.0's
`head_name()` and `head_ref()` stop at the first target, while
`follow_to_object()` cannot return an unborn terminal ref. Keep the
recursive HEAD lookup on the CLI path and record the gap as GIX-44.

The alias regression failed on both backends before the fix. It now
matches Git 2.54.0 on initialization and follows a later remote
commit. The targeted run passed 25 tests on CLI and 27 on Gix,
including two native-only checks.

Agent-Model: gpt-6.2-sol
Agent-Reasoning-Effort: high
@Byron
Byron force-pushed the fix/submodule-branch-dot branch from e47145c to 01abf35 Compare November 24, 2026 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants