Skip to content

fix(submodule): read branch as a string when re-reading .gitmodules - #2363

Merged
Byron merged 2 commits into
gitpython-developers:mainfrom
JROtto5:fix/submodule-branch-reread
Nov 25, 2026
Merged

Byron merged 2 commits into
gitpython-developers:mainfrom
JROtto5:fix/submodule-branch-reread

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 found this while fixing branch = . for submodules (#2362), where update() had to re-normalize the branch it got back after set_parent_commit(). Submodule reads its branch setting two different ways. iter_items() turns branch = 1.10 into branch_path == "refs/heads/1.10". After set_parent_commit() or _clear_cache(), the value is re-read with get_value(), so branch_path becomes the float 1.1 and branch_name raises TypeError. 2 becomes an int, true becomes True, and dev comes back without the refs/heads/ prefix. The gix backend's submodule_config() path converts the same way. Release branches named like 1.10 are the most likely to hit this.

Read the value as a string on both paths and pass it through Head.to_full_path(), as iter_items() already does. Full refs such as refs/heads/dev are unchanged. A valueless branch key, which Git rejects with missing value for 'submodule.<name>.branch', now raises ValueError from Repo.submodules/iter_items() and from re-reads (31cb7c8). Before, iter_items() gave refs/heads/None for it and a re-read raised TypeError. The gix backend falls back to the Git config reader when such a key may be present (GIX-45 in doc/gix-backend.md).

test_reread_branch_matches_iter_items checks unset, 1.10, 2, true, dev and refs/heads/dev after both re-read paths. 1.10, 2, true and dev fail without the change and pass with it, under both GIT_PYTHON_BACKEND=cli and gix; unset and refs/heads/dev pass either way. test_valueless_branch_is_invalid covers a bare key on its own and one followed by branch = dev. test_update_branch_dot_rejects_latest_revision_with_detached_superproject from #2362 now expects refs/heads/. after the re-read, matching iter_items(). The full suite passes under cli; under gix the 14 failures are the same on main (GixPython 0.3.0 here, 0.4.0 is pinned). Ruff check and format pass, and mypy reports no issues. Tested only on Linux with Git 2.56.0.

No changelog entry, as with #2351.

`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
@Byron
Byron force-pushed the fix/submodule-branch-reread branch from 175f494 to 31cb7c8 Compare November 25, 2026 05:24
@JROtto5

JROtto5 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

(Claude agent, through the contributor's account.)

The ubuntu failures came from #2362's branch_path == "." check: this PR's reread now gives refs/heads/., the same as iter_items(), and _branch_for_update() only compares Head.name, which is . either way. 5e6d51f updates that assertion. It passes under cli and gix, and the full cli suite passes. The description now reflects the valueless-branch error from 31cb7c8.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues blocking the submodule branch re-reading fix.

No open issues 🎉

What changed in this PR

Fixes submodule branch re-reading so cached values remain strings normalized to full refs, consistent with initial enumeration across the CLI and Gix backends.

Changes:

  • Preserves numeric-looking branch names and normalizes branch paths after cache invalidation.
  • Rejects valueless branch settings, with a Gix fallback for potentially affected configuration.
  • Adds regression tests and documents the Gix compatibility gap.
File Description
test/​test_submodule.py Tests branch re-reading and valueless settings; updates the branch = . expectation.
git/​objects/​submodule/​base.py Normalizes branch strings and shares validation between enumeration and re-reading.
git/​_backend.py Falls back to the Git config reader for possible valueless branch keys.
doc/​gix-backend.md Documents GIX-45 and the updated submodule fallback behavior.

Review effort: Balanced

- [P2] Reject valueless submodule branch settings — git/objects/submodule/base.py:1934-1936
  When a bare `branch` key replaces `branch = dev`, both backends now interpret
it as `refs/heads/master`. The baseline Git rejects the malformed key, but if
`origin/master` exists, `Repo.submodule_update(to_latest_revision=True)` can
now switch the submodule to `master` and advance it. Use the default only for an
absent key and report an explicit valueless key as invalid in both listing and
rereads.
- [P3] Drop redundant commits from the branch reread test — test/test_submodule.py:2111-2112
  The fixture already commits `.gitmodules` and the gitlink, and
`set_parent_commit(repo.head.commit)` rereads the working-tree file,
not the committed blob. The six add/commit pairs added 96 Git launches
in a local CLI run; all six cases still passed without them. Reuse
the fixture's committed state and leave the per-case edits unstaged.

Assisted-by: GPT 6.2 Sol
Co-authored-by: GPT 6.2 Sol <codex@openai.com>
@Byron
Byron force-pushed the fix/submodule-branch-reread branch from 5e6d51f to f0df324 Compare November 25, 2026 07:59
@Byron

Byron commented Nov 25, 2026

Copy link
Copy Markdown
Member

Possible follow-up: Resolve branch = . in GitPython’s submodule branch accessors

Codex

Git uses submodule.<name>.branch = . to select the submodule’s remote branch with the same name as the superproject’s current branch. GitPython resolves this during a latest-revision update, but Submodule.branch_name still returns . and Submodule.branch_path returns refs/heads/.. Git rejects both as branch identifiers. This affects callers that use those properties to look up or track a branch. Git documentation

Keep the configured value separate from the resolved branch. With branch = . and the superproject on dev, the accessors should return dev and refs/heads/dev. They should follow subsequent superproject branch switches, including after set_parent_commit() clears the cache. With a detached superproject HEAD, document and raise ValueError when an accessor needs to resolve an inherited branch. An ordinary update should still check out the recorded gitlink commit.

RootModule.update() should compare the configured values when detecting changes between revisions. For example, a change from branch = dev to branch = . matters even when both select dev today. Resolve the effective branch when setting up tracking, and preserve the precedence of .git/config overrides during remote updates.

Cover branch switches, cache rereads, detached HEAD, and a change from an explicit branch to inheritance on both backends.

@JROtto5

JROtto5 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

(Claude agent, through the contributor's account.)

Agreed, and I'd keep it out of this PR: here both read paths now give the same configured value (refs/heads/.), so the resolution only has to happen in one place. I'll open it as a follow-up PR once this one is merged, with the cases you listed tested on both backends.

@Byron
Byron merged commit 9232dc5 into gitpython-developers:main Nov 25, 2026
64 checks passed
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.

3 participants