Skip to content

Commit 01abf35

Browse files
codexByron
authored andcommitted
fix(submodule): follow symbolic HEAD aliases for branch inheritance
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
1 parent 6caf1e1 commit 01abf35

5 files changed

Lines changed: 39 additions & 10 deletions

File tree

‎doc/gix-backend.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ and predate the current fallback; they do not establish native coverage.
388388
| Tree enumeration, `ls_tree` | Native tree entries with modes, names, and object IDs | Other command options; entries with modes Git canonicalizes or that do not round-trip through native tree encoding; GIX-36 |
389389
| Reference object reads | One exact native lookup for direct, symbolic and absent references | Partial names and native decoding errors |
390390
| Reference-name validation | Native full-name validation without opening a repository | Standalone names outside the native grammar; GIX-20 |
391-
| Managed `symbolic_ref` | Nonrecursive target lookup | Mutation, command-level missing-reference diagnostics |
391+
| Managed `symbolic_ref` | Nonrecursive target lookup | Mutation, command-level missing-reference diagnostics, recursive `HEAD` lookup (GIX-44) |
392392
| Reference enumeration, `for_each_ref` | Sorted reference names and literal prefixes, preserving symbolic aliases | Dangling symbolic refs, root refs, glob patterns, other formats/options; GIX-25 |
393393
| Managed `config --get KEY` | Merged repository config snapshot | Files/streams, enumeration, mutation; GIX-12 |
394394
| Submodule enumeration and cached fields | Dedicated native `.gitmodules` parser over the existing worktree/blob source, with raw path/URL/branch values | Non-UTF-8 subsection names, other/duplicate sections, ambiguous brackets, missing/implicit fields and parser errors; GIX-12. Lines other than comments, bare headers and single settings; GIX-35 |
@@ -1017,6 +1017,7 @@ identify the historical fixture where needed.
10171017
| GIX-41 | Bug (open) | With Git 2.54.0 and GixPython 0.4.0, `tree-modes.sh` writes an entry with either an empty mode field or the literal `0`. Both are decoded by `Tree.iter()` as mode zero and kind `commit`. Git rejects the empty field with `malformed mode in tree entry` and lists the literal zero as a gitlink. `test_ambiguous_tree_entry_mode_uses_git` verifies both results for SHA-1 and SHA-256. | Reject a missing mode or expose the distinction between a missing mode and an encoded zero. `_ls_tree()` defers mode zero to Git for validation and formatting. |
10181018
| GIX-42 | Bug (open) | With Git 2.54.0 and GixPython 0.4.0, `commit.sh negative-date` stores `-1 +0200 1700000000 -0500` in the author, committer and tagger identities. Git's `split_ident_line()` requires unsigned timestamp digits, so `git show --format=%ad --date=raw` reports no date and GitPython's CLI path returns `(0, 0)`. Native signature accessors expose `(-1, +7200)` as seconds and offset east of UTC. | Provide Git-compatible negative timestamp handling. `object_metadata()` retains CLI for negative dates. `test_deserialization_negative_timestamp_matches_git` verifies both object kinds. |
10191019
| GIX-43 | Bug (open) | With Git 2.54.0 and GixPython 0.4.0, `commit.sh spaced-email` stores spaces inside the email brackets ahead of a date with trailing fields. Git's `%ae` and `%ce` preserve these bytes, but native commit author and committer accessors trim them. The native tagger accessor preserves the spaces. | Expose a signature identity that preserves the stored fields with Git-compatible delimiters. `object_metadata()` uses the existing CLI fallback if the native identity fails the raw header comparison. `test_deserialization_preserves_email_whitespace` verifies author, committer and tagger properties. |
1020+
| GIX-44 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, the `branch-dot-alias` case of `submodule-cases.sh` sets `HEAD` to `refs/heads/dev`, which points to `refs/heads/master`. `Repository.head_name()` and `Repository.head_ref().name()` return `refs/heads/dev`. `git symbolic-ref HEAD` returns `refs/heads/master`, and `git submodule update --remote` selects `origin/master` for `branch = .`. `test_update_to_latest_revision_follows_superproject_branch_alias` verifies branch selection on clone and later updates. Redirect `dev` to `refs/heads/missing` and Git returns that terminal name, while `Reference.follow_to_object()` raises a missing-ref error. | Expose the terminal symbolic target with Git-compatible handling of unborn, detached and invalid references. `Submodule._branch_for_update()` uses recursive `git symbolic-ref --quiet -- HEAD` through the CLI fallback. |
10201021

10211022
GIX-14 also covers configured worktree symlink resolution. On macOS with
10221023
GixPython 0.3.0, CPython 3.12.14 and Git 2.54.0, set a submodule's

‎git/_backend.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ def reference_info(command: Any, path: str) -> Any:
392392

393393

394394
def _symbolic_ref(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes:
395+
if args == ["--quiet", "--", "HEAD"]:
396+
raise _Unsupported("recursive HEAD resolution (GIX-44)")
395397
if len(args) != 4 or args[:3] != ["--quiet", "--no-recurse", "--"]:
396398
raise _Unsupported("reference mutation or options")
397399
reference = repo.try_find_reference(safe_encode(args[-1]))

‎git/objects/submodule/base.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,14 +1845,18 @@ def _branch_for_update(self, *, to_latest_revision: bool = False) -> Tuple["Head
18451845
branch = mkhead(self.repo, self.branch_path if override is None else override)
18461846
if branch.name != ".":
18471847
return branch, override is not None
1848-
if not self.repo.head.is_detached:
1849-
return self.repo.active_branch, True
1850-
if to_latest_revision:
1851-
raise ValueError(
1852-
f"Cannot update submodule {self.name!r} to the latest revision with branch = . "
1853-
"because the superproject HEAD is detached"
1854-
)
1855-
return branch, False
1848+
try:
1849+
target = self.repo.git._call_process_safe("symbolic_ref", "--quiet", "--", "HEAD")
1850+
except GitCommandError as exc:
1851+
if exc.status != 1:
1852+
raise
1853+
if to_latest_revision:
1854+
raise ValueError(
1855+
f"Cannot update submodule {self.name!r} to the latest revision with branch = . "
1856+
"because the superproject HEAD is detached"
1857+
) from exc
1858+
return branch, False
1859+
return git.Head(self.repo, target), True
18561860

18571861
@property
18581862
def url(self) -> str:

‎test/test_submodule.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,24 @@ def test_update_to_latest_revision_follows_superproject_branch(self):
19611961
assert submodule.module().active_branch.name == "dev"
19621962
assert submodule.module().head.commit == source_repo.heads.dev.commit
19631963

1964+
@patch_git_config("protocol.file.allow", "always")
1965+
def test_update_to_latest_revision_follows_superproject_branch_alias(self):
1966+
# The submodule URL points to this private fixture's source.
1967+
rwdir = self._fixture("branch-dot-alias", execute=True)
1968+
with git.Repo(rwdir / "source") as source, git.Repo(rwdir / "clone") as parent:
1969+
assert parent.git.symbolic_ref("--no-recurse", "HEAD") == "refs/heads/dev"
1970+
assert parent.git.symbolic_ref("HEAD") == "refs/heads/master"
1971+
submodule = parent.submodules[0]
1972+
submodule.update(to_latest_revision=True)
1973+
with submodule.module() as module:
1974+
assert module.active_branch.name == "master"
1975+
assert module.head.commit == source.heads.master.commit
1976+
1977+
remote_tip = source.index.commit("Advance master again", skip_hooks=True)
1978+
submodule.update(to_latest_revision=True)
1979+
with submodule.module() as module:
1980+
assert module.head.commit == remote_tip
1981+
19641982
@patch_git_config("protocol.file.allow", "always")
19651983
def test_update_to_latest_revision_after_superproject_switch(self):
19661984
# The submodule URL points to this private fixture's source.

‎tests/fixtures/submodule-cases.sh‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ case "$scenario" in
4646
deinit-nonempty) printf '%s' 'user content' > parent/module/file ;;
4747
esac
4848
;;
49-
branch-dot)
49+
branch-dot|branch-dot-alias)
5050
git init -q -b master source
5151
git -C source commit --allow-empty -qm initial
5252
git -C source branch dev
@@ -60,6 +60,10 @@ case "$scenario" in
6060
git -C parent commit -qam 'Track the superproject branch'
6161
git -C parent branch dev
6262
git clone -q -b dev parent clone
63+
if test "$scenario" = branch-dot-alias; then
64+
git -C clone branch master refs/remotes/origin/master
65+
git -C clone symbolic-ref refs/heads/dev refs/heads/master
66+
fi
6367
;;
6468
add-empty) git init -q -b master empty-repo ;;
6569
relative)

0 commit comments

Comments
 (0)