Skip to content

Commit c0c7262

Browse files
codexByron
authored andcommitted
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
1 parent e3adb9b commit c0c7262

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎git/remote.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,13 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs:
399399
if spec.startswith("^") or ":" not in spec:
400400
continue
401401
remote, local = spec.lstrip("+").split(":", 1)
402-
remote = cls._display_ref(remote)
403402
if "*" in remote:
404-
prefix, suffix = remote.split("*", 1)
403+
prefix, suffix = cls._display_ref(remote).split("*", 1)
405404
if not source.startswith(prefix) or not source.endswith(suffix):
406405
continue
407406
middle = source[len(prefix) : len(source) - len(suffix) if suffix else None]
408407
local = local.replace("*", middle)
409-
elif remote != source:
408+
elif source not in (cls._display_ref(remote), cls._display_ref("refs/" + remote)):
410409
continue
411410
if cls._display_ref(local) == destination:
412411
candidates.add(local)

‎test/test_remote_cli.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def test_fetch_resolves_tag_named_like_a_full_branch_ref(remote_fixture):
6464
assert clone.heads.main.commit == branch
6565

6666

67+
@pytest.mark.parametrize("remote_fixture", [("abbreviated-tag-source",)], indirect=True)
68+
def test_fetch_resolves_explicit_destination_that_looks_like_a_local_tag(remote_fixture):
69+
with Repo(remote_fixture / "source") as source, Repo(remote_fixture / "clone") as clone:
70+
[info] = clone.remote().fetch("tags/v1:refs/archive/v1", no_tags=True)
71+
assert info.ref.path == "refs/archive/v1"
72+
assert info.commit == source.head.commit
73+
assert clone.tags["refs/archive/v1"].commit == clone.head.commit
74+
75+
6776
@pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True)
6877
def test_quoted_remote_and_submodule_names(remote_fixture):
6978
tmp_path = remote_fixture

‎tests/fixtures/remote-cli.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ case "$1" in
1515
git -C source commit --allow-empty -m second
1616
git -C source tag refs/heads/main
1717
;;
18+
abbreviated-tag-source)
19+
git clone source clone
20+
git -C clone remote set-url origin ../source
21+
git -C clone tag refs/archive/v1
22+
git -C source commit --allow-empty -m second
23+
git -C source tag v1
24+
;;
1825
quoted)
1926
git init --initial-branch=main parent
2027
;;

0 commit comments

Comments
 (0)