From e3adb9b7e61d48e0a7fb8c33b1e3f72984f700db Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Tue, 17 Nov 2026 22:47:10 +0530 Subject: [PATCH 1/5] 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 --- git/remote.py | 6 ++++-- test/test_remote_cli.py | 11 +++++++++++ tests/fixtures/remote-cli.sh | 6 ++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/git/remote.py b/git/remote.py index d5ab4914e..01922d1e7 100644 --- a/git/remote.py +++ b/git/remote.py @@ -410,10 +410,12 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: continue if cls._display_ref(local) == destination: candidates.add(local) - if destination.startswith("refs/"): - candidates = {destination} if not candidates: candidates = {path for path in refs if cls._display_ref(path) == destination} + # Git shortens refs/tags/refs/heads/main to refs/heads/main, so a + # displayed name under refs/ is only a full name if nothing shortens to it. + if not candidates and destination.startswith("refs/"): + candidates = {destination} if len(candidates) > 1 and ("tag" in operation or control == "t"): candidates = {path for path in candidates if path.startswith("refs/tags/")} if len(candidates) != 1: diff --git a/test/test_remote_cli.py b/test/test_remote_cli.py index 3bdd0efdc..eb2d65d9e 100644 --- a/test/test_remote_cli.py +++ b/test/test_remote_cli.py @@ -53,6 +53,17 @@ def checked_open(path, *args, **kwargs): clone.close() +@pytest.mark.parametrize("remote_fixture", [("tag-named-like-branch",)], indirect=True) +def test_fetch_resolves_tag_named_like_a_full_branch_ref(remote_fixture): + # Git displays refs/tags/refs/heads/main as refs/heads/main. + with Repo(remote_fixture / "source") as source, Repo(remote_fixture / "clone") as clone: + branch = clone.heads.main.commit + [info] = [info for info in clone.remote().fetch() if info.flags & FetchInfo.NEW_TAG] + assert info.ref.path == "refs/tags/refs/heads/main" + assert info.commit == source.head.commit + assert clone.heads.main.commit == branch + + @pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True) def test_quoted_remote_and_submodule_names(remote_fixture): tmp_path = remote_fixture diff --git a/tests/fixtures/remote-cli.sh b/tests/fixtures/remote-cli.sh index 8aec9cb55..5097b9b36 100644 --- a/tests/fixtures/remote-cli.sh +++ b/tests/fixtures/remote-cli.sh @@ -9,6 +9,12 @@ case "$1" in git -C clone remote set-url origin ../source git -C source commit --allow-empty -m second ;; + tag-named-like-branch) + git clone source clone + git -C clone remote set-url origin ../source + git -C source commit --allow-empty -m second + git -C source tag refs/heads/main + ;; quoted) git init --initial-branch=main parent ;; From c0c72622d97f8fb26937348a8f9c9067445c597e Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 24 Nov 2026 12:59:51 +0100 Subject: [PATCH 2/5] fix(remote): honor explicit fetch destinations with abbreviated sources Git resolves `tags/v1` in a fetch refspec to `refs/tags/v1` and prints the source as `v1`. The result decoder missed that mapping and reported a pre-existing tag named `refs/archive/v1` in place of the explicitly requested destination `refs/archive/v1`. Compare exact refspec sources with the display forms of both the provided source and its `refs/`-qualified form. This lets the explicit mapping resolve the destination before consulting the local inventory. A scripted fixture reproduces the collision and checks the destination and both commits. The non-network remote tests passed on each backend with 38 tests, and Ruff and Bash syntax checks passed. Agent-Model: unknown --- git/remote.py | 5 ++--- test/test_remote_cli.py | 9 +++++++++ tests/fixtures/remote-cli.sh | 7 +++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/git/remote.py b/git/remote.py index 01922d1e7..a2eee6e5e 100644 --- a/git/remote.py +++ b/git/remote.py @@ -399,14 +399,13 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: if spec.startswith("^") or ":" not in spec: continue remote, local = spec.lstrip("+").split(":", 1) - remote = cls._display_ref(remote) if "*" in remote: - prefix, suffix = remote.split("*", 1) + prefix, suffix = cls._display_ref(remote).split("*", 1) if not source.startswith(prefix) or not source.endswith(suffix): continue middle = source[len(prefix) : len(source) - len(suffix) if suffix else None] local = local.replace("*", middle) - elif remote != source: + elif source not in (cls._display_ref(remote), cls._display_ref("refs/" + remote)): continue if cls._display_ref(local) == destination: candidates.add(local) diff --git a/test/test_remote_cli.py b/test/test_remote_cli.py index eb2d65d9e..d3ed88f19 100644 --- a/test/test_remote_cli.py +++ b/test/test_remote_cli.py @@ -64,6 +64,15 @@ def test_fetch_resolves_tag_named_like_a_full_branch_ref(remote_fixture): assert clone.heads.main.commit == branch +@pytest.mark.parametrize("remote_fixture", [("abbreviated-tag-source",)], indirect=True) +def test_fetch_resolves_explicit_destination_that_looks_like_a_local_tag(remote_fixture): + with Repo(remote_fixture / "source") as source, Repo(remote_fixture / "clone") as clone: + [info] = clone.remote().fetch("tags/v1:refs/archive/v1", no_tags=True) + assert info.ref.path == "refs/archive/v1" + assert info.commit == source.head.commit + assert clone.tags["refs/archive/v1"].commit == clone.head.commit + + @pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True) def test_quoted_remote_and_submodule_names(remote_fixture): tmp_path = remote_fixture diff --git a/tests/fixtures/remote-cli.sh b/tests/fixtures/remote-cli.sh index 5097b9b36..8919e6c6e 100644 --- a/tests/fixtures/remote-cli.sh +++ b/tests/fixtures/remote-cli.sh @@ -15,6 +15,13 @@ case "$1" in git -C source commit --allow-empty -m second git -C source tag refs/heads/main ;; + abbreviated-tag-source) + git clone source clone + git -C clone remote set-url origin ../source + git -C clone tag refs/archive/v1 + git -C source commit --allow-empty -m second + git -C source tag v1 + ;; quoted) git init --initial-branch=main parent ;; From 507448fd8e3839508bedbd084675ef13e154f9c1 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 24 Nov 2026 13:01:16 +0100 Subject: [PATCH 3/5] fix(remote): preserve notes results alongside tag wildcard refspecs Git reports `refs/notes/commits` as a full name, but applying wildcard refspecs to the displayed text also invented the tag destination `refs/tags/refs/notes/commits`. The ambiguity dropped the notes result even though Git had fetched it successfully. Match wildcard refspecs against full source paths consistent with Git's display and use the source namespace from Git's new-ref status when available. For unchanged and updated refs, discard ambiguous candidates that do not appear in the local ref inventory. The scripted regression failed on the new notes result before the fix. The remote CLI tests now pass on both backend selections and cover new, unchanged, and fast-forwarded notes. Ruff and Bash syntax checks passed. Agent-Model: unknown --- git/remote.py | 34 ++++++++++++++++++++++++++-------- test/test_remote_cli.py | 13 +++++++++++++ tests/fixtures/remote-cli.sh | 12 ++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/git/remote.py b/git/remote.py index a2eee6e5e..42d5e5beb 100644 --- a/git/remote.py +++ b/git/remote.py @@ -394,21 +394,39 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: if destination == "FETCH_HEAD": ref = SymbolicReference(repo, destination) else: + # Git's "new" status describes the source namespace. + if flags & cls.NEW_TAG: + wildcard_sources = ["refs/tags/" + source] + elif flags & cls.NEW_HEAD: + wildcard_sources = ["refs/heads/" + source] + else: + wildcard_sources = ["refs/remotes/" + source] + if operation != "[new ref]": + wildcard_sources.extend(("refs/heads/" + source, "refs/tags/" + source)) + if cls._display_ref(source) == source: + wildcard_sources.append(source) + candidates = set() for spec in refspecs: if spec.startswith("^") or ":" not in spec: continue remote, local = spec.lstrip("+").split(":", 1) if "*" in remote: - prefix, suffix = cls._display_ref(remote).split("*", 1) - if not source.startswith(prefix) or not source.endswith(suffix): - continue - middle = source[len(prefix) : len(source) - len(suffix) if suffix else None] - local = local.replace("*", middle) - elif source not in (cls._display_ref(remote), cls._display_ref("refs/" + remote)): - continue - if cls._display_ref(local) == destination: + prefix, suffix = remote.split("*", 1) + for path in wildcard_sources: + if not path.startswith(prefix) or not path.endswith(suffix): + continue + middle = path[len(prefix) : len(path) - len(suffix) if suffix else None] + mapped = local.replace("*", middle) + if cls._display_ref(mapped) == destination: + candidates.add(mapped) + elif ( + source in (cls._display_ref(remote), cls._display_ref("refs/" + remote)) + and cls._display_ref(local) == destination + ): candidates.add(local) + if (control == "=" or old_commit is not None) and len(candidates) > 1: + candidates.intersection_update(refs) if not candidates: candidates = {path for path in refs if cls._display_ref(path) == destination} # Git shortens refs/tags/refs/heads/main to refs/heads/main, so a diff --git a/test/test_remote_cli.py b/test/test_remote_cli.py index d3ed88f19..98d6718b4 100644 --- a/test/test_remote_cli.py +++ b/test/test_remote_cli.py @@ -73,6 +73,19 @@ def test_fetch_resolves_explicit_destination_that_looks_like_a_local_tag(remote_ assert clone.tags["refs/archive/v1"].commit == clone.head.commit +@pytest.mark.parametrize("remote_fixture", [("notes-and-tag-wildcards",)], indirect=True) +def test_fetch_resolves_notes_with_a_tag_wildcard(remote_fixture): + with Repo(remote_fixture / "source") as source, Repo(remote_fixture / "clone") as clone: + remote = clone.remote() + for flags in (0, FetchInfo.HEAD_UPTODATE, FetchInfo.FAST_FORWARD): + if flags == FetchInfo.FAST_FORWARD: + source.git.update_ref("refs/notes/commits", "refs/fixture/updated-notes") + [info] = [info for info in remote.fetch() if info.remote_ref_path == "refs/notes/commits"] + assert info.ref.path == "refs/notes/commits" + assert info.commit == source.commit("refs/notes/commits") + assert info.flags == flags + + @pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True) def test_quoted_remote_and_submodule_names(remote_fixture): tmp_path = remote_fixture diff --git a/tests/fixtures/remote-cli.sh b/tests/fixtures/remote-cli.sh index 8919e6c6e..28aed56d9 100644 --- a/tests/fixtures/remote-cli.sh +++ b/tests/fixtures/remote-cli.sh @@ -22,6 +22,18 @@ case "$1" in git -C source commit --allow-empty -m second git -C source tag v1 ;; + notes-and-tag-wildcards) + git clone source clone + git -C clone remote set-url origin ../source + git -C clone config --unset-all remote.origin.fetch + git -C clone config --add remote.origin.fetch '+refs/notes/*:refs/notes/*' + git -C clone config --add remote.origin.fetch '+refs/tags/*:refs/tags/*' + git -C source notes add -m 'a note' + initial_notes=$(git -C source rev-parse refs/notes/commits) + git -C source notes append -m 'more notes' + git -C source update-ref refs/fixture/updated-notes refs/notes/commits + git -C source update-ref refs/notes/commits "$initial_notes" + ;; quoted) git init --initial-branch=main parent ;; From d92786e3fc951206dea56545a213df69c8753b83 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 24 Nov 2026 13:02:30 +0100 Subject: [PATCH 4/5] perf(remote): index displayed ref names once per fetch With `--prefetch`, Git writes destinations under `refs/prefetch/`. Those paths do not match the configured tracking refspec, so parsing each result used to scan and format the whole local ref inventory. Index the inventory by displayed name once and reuse the matching bucket for each result. Preserve every full path in each bucket so the decoder can still handle display-name collisions. The scripted prefetch regression detected repeated formatting before the change and passes on both backend selections. A local CLI prefetch of 4,001 unchanged branches took a median of 0.47 seconds over five runs, down from 4.77 seconds before indexing. Ruff and mypy passed. Agent-Model: unknown --- git/remote.py | 19 +++++++++++++++---- test/test_remote_cli.py | 12 ++++++++++++ tests/fixtures/remote-cli.sh | 6 ++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/git/remote.py b/git/remote.py index 42d5e5beb..4b470b5f0 100644 --- a/git/remote.py +++ b/git/remote.py @@ -22,6 +22,7 @@ NoReturn, Optional, Sequence, + Set, TypeVar, Union, cast, @@ -371,7 +372,13 @@ def _display_ref(path: str) -> str: return path @classmethod - def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: Sequence[str] = ()) -> "FetchInfo": + def _from_line( + cls, + repo: "Repo", + line: str, + refspecs: Sequence[str] = (), + refs_by_display: Optional[Dict[str, Set[str]]] = None, + ) -> "FetchInfo": """Decode Git's full verbose fetch output without reading FETCH_HEAD.""" match = cls._re_fetch_result.match(line) if match is None: @@ -425,10 +432,11 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: and cls._display_ref(local) == destination ): candidates.add(local) + matching_refs = refs_by_display.get(destination, ()) if refs_by_display is not None else () if (control == "=" or old_commit is not None) and len(candidates) > 1: - candidates.intersection_update(refs) + candidates.intersection_update(matching_refs) if not candidates: - candidates = {path for path in refs if cls._display_ref(path) == destination} + candidates.update(matching_refs) # Git shortens refs/tags/refs/heads/main to refs/heads/main, so a # displayed name under refs/ is only a full name if nothing shortens to it. if not candidates and destination.startswith("refs/"): @@ -839,6 +847,9 @@ def _get_fetch_info_from_stderr( continue refs = self.repo.git._call_process_safe("for_each_ref", "--format=%(refname)").split("\n") + refs_by_display: Dict[str, Set[str]] = {} + for path in refs: + refs_by_display.setdefault(FetchInfo._display_ref(path), set()).add(path) try: configured = self.config_reader.config.get_values(self._config_section_name(), "fetch") except (cp.NoSectionError, cp.NoOptionError): @@ -846,7 +857,7 @@ def _get_fetch_info_from_stderr( specs = [*refspecs, *configured] for line in fetch_info_lines: try: - output.append(FetchInfo._from_line(self.repo, line, specs, refs)) + output.append(FetchInfo._from_line(self.repo, line, specs, refs_by_display)) except ValueError as exc: _logger.warning("Cannot decode fetch result %r: %s", line, exc) # A source-only refspec reports both FETCH_HEAD and any opportunistic diff --git a/test/test_remote_cli.py b/test/test_remote_cli.py index 98d6718b4..1e5ea4b41 100644 --- a/test/test_remote_cli.py +++ b/test/test_remote_cli.py @@ -86,6 +86,18 @@ def test_fetch_resolves_notes_with_a_tag_wildcard(remote_fixture): assert info.flags == flags +@pytest.mark.parametrize("remote_fixture", [("prefetch",)], indirect=True) +def test_prefetch_indexes_displayed_refs_once(remote_fixture): + paths = {"refs/prefetch/remotes/origin/main", "refs/prefetch/remotes/origin/second"} + with Repo(remote_fixture / "clone") as clone: + with mock.patch.object(FetchInfo, "_display_ref", wraps=FetchInfo._display_ref) as display_ref: + result = clone.remote().fetch(prefetch=True) + assert {info.ref.path for info in result} == paths + assert all(info.flags == FetchInfo.HEAD_UPTODATE for info in result) + displayed = [args[0] for args, _ in display_ref.call_args_list if args[0] in paths] + assert len(displayed) == len(set(displayed)) + + @pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True) def test_quoted_remote_and_submodule_names(remote_fixture): tmp_path = remote_fixture diff --git a/tests/fixtures/remote-cli.sh b/tests/fixtures/remote-cli.sh index 28aed56d9..9f0cf4f2e 100644 --- a/tests/fixtures/remote-cli.sh +++ b/tests/fixtures/remote-cli.sh @@ -34,6 +34,12 @@ case "$1" in git -C source update-ref refs/fixture/updated-notes refs/notes/commits git -C source update-ref refs/notes/commits "$initial_notes" ;; + prefetch) + git -C source branch second + git clone source clone + git -C clone remote set-url origin ../source + git -C clone fetch --prefetch + ;; quoted) git init --initial-branch=main parent ;; From 2e64c483dc0ee9bbe6f98b1870d9534e98ceb66a Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 24 Nov 2026 16:19:11 +0100 Subject: [PATCH 5/5] review A rubber-stamp for tests, but looked at the code. I can only assume this is better than it was before, and there are more tests now as well