Skip to content

Commit e3adb9b

Browse files
committed
fix(remote): resolve fetched tags whose names look like full refs
`FetchInfo._from_line` turns the verbose output of `git fetch` into the `FetchInfo` results returned by `Remote.fetch` and `Remote.pull`. It took any displayed destination starting with `refs/` as a full reference name. Git displays local references through `prettify_refname`, which strips `refs/heads/`, `refs/tags/` and `refs/remotes/`, so the tag `refs/tags/refs/heads/main` is displayed as `refs/heads/main`. Such a tag name is valid and reaches a clone through ordinary tag auto-following from any remote. Before this change, fetching from a remote that carries the tag `refs/heads/main` produced a `FetchInfo` flagged `NEW_TAG` whose `ref` was the local branch `refs/heads/main`: `info.commit` returned the local branch commit instead of the fetched tag's commit, and code acting on new tags through `info.ref` (inspecting or deleting them) acted on the user's branch. A tag named `refs/remotes/origin/main` was reported as the remote-tracking branch in the same way. Both backends were affected, since this parses Git CLI output. The displayed name is now matched against the refspec mappings and then against existing references by their displayed form, and taken as a full name only when no reference displays as it. Results from refspec mappings are no longer overridden either, so a refspec that stores into `refs/tags/refs/heads/x` resolves to that reference. References outside the three shortened namespaces, such as `refs/notes/x`, display as themselves and resolve as before, as do deleted references that no longer exist. Adds a `tag-named-like-branch` case to `tests/fixtures/remote-cli.sh` and `test_fetch_resolves_tag_named_like_a_full_branch_ref` in `test/test_remote_cli.py`, which fails before the change (`'refs/heads/main' == 'refs/tags/refs/heads/main'`) and passes after. `test/test_remote.py` and `test/test_remote_cli.py` pass with `GIT_PYTHON_BACKEND=cli` and `gix` (`-m "not net"`; the `net` cases were skipped on this Windows machine), and `ruff check`, `ruff format --check` and `mypy` are clean. Agent-Model: claude-opus-5-5
1 parent 60a2715 commit e3adb9b

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎git/remote.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,12 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs:
410410
continue
411411
if cls._display_ref(local) == destination:
412412
candidates.add(local)
413-
if destination.startswith("refs/"):
414-
candidates = {destination}
415413
if not candidates:
416414
candidates = {path for path in refs if cls._display_ref(path) == destination}
415+
# Git shortens refs/tags/refs/heads/main to refs/heads/main, so a
416+
# displayed name under refs/ is only a full name if nothing shortens to it.
417+
if not candidates and destination.startswith("refs/"):
418+
candidates = {destination}
417419
if len(candidates) > 1 and ("tag" in operation or control == "t"):
418420
candidates = {path for path in candidates if path.startswith("refs/tags/")}
419421
if len(candidates) != 1:

‎test/test_remote_cli.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ def checked_open(path, *args, **kwargs):
5353
clone.close()
5454

5555

56+
@pytest.mark.parametrize("remote_fixture", [("tag-named-like-branch",)], indirect=True)
57+
def test_fetch_resolves_tag_named_like_a_full_branch_ref(remote_fixture):
58+
# Git displays refs/tags/refs/heads/main as refs/heads/main.
59+
with Repo(remote_fixture / "source") as source, Repo(remote_fixture / "clone") as clone:
60+
branch = clone.heads.main.commit
61+
[info] = [info for info in clone.remote().fetch() if info.flags & FetchInfo.NEW_TAG]
62+
assert info.ref.path == "refs/tags/refs/heads/main"
63+
assert info.commit == source.head.commit
64+
assert clone.heads.main.commit == branch
65+
66+
5667
@pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True)
5768
def test_quoted_remote_and_submodule_names(remote_fixture):
5869
tmp_path = remote_fixture

‎tests/fixtures/remote-cli.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ case "$1" in
99
git -C clone remote set-url origin ../source
1010
git -C source commit --allow-empty -m second
1111
;;
12+
tag-named-like-branch)
13+
git clone source clone
14+
git -C clone remote set-url origin ../source
15+
git -C source commit --allow-empty -m second
16+
git -C source tag refs/heads/main
17+
;;
1218
quoted)
1319
git init --initial-branch=main parent
1420
;;

0 commit comments

Comments
 (0)