Skip to content

Commit 507448f

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

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎git/remote.py‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,21 +394,39 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs:
394394
if destination == "FETCH_HEAD":
395395
ref = SymbolicReference(repo, destination)
396396
else:
397+
# Git's "new" status describes the source namespace.
398+
if flags & cls.NEW_TAG:
399+
wildcard_sources = ["refs/tags/" + source]
400+
elif flags & cls.NEW_HEAD:
401+
wildcard_sources = ["refs/heads/" + source]
402+
else:
403+
wildcard_sources = ["refs/remotes/" + source]
404+
if operation != "[new ref]":
405+
wildcard_sources.extend(("refs/heads/" + source, "refs/tags/" + source))
406+
if cls._display_ref(source) == source:
407+
wildcard_sources.append(source)
408+
397409
candidates = set()
398410
for spec in refspecs:
399411
if spec.startswith("^") or ":" not in spec:
400412
continue
401413
remote, local = spec.lstrip("+").split(":", 1)
402414
if "*" in remote:
403-
prefix, suffix = cls._display_ref(remote).split("*", 1)
404-
if not source.startswith(prefix) or not source.endswith(suffix):
405-
continue
406-
middle = source[len(prefix) : len(source) - len(suffix) if suffix else None]
407-
local = local.replace("*", middle)
408-
elif source not in (cls._display_ref(remote), cls._display_ref("refs/" + remote)):
409-
continue
410-
if cls._display_ref(local) == destination:
415+
prefix, suffix = remote.split("*", 1)
416+
for path in wildcard_sources:
417+
if not path.startswith(prefix) or not path.endswith(suffix):
418+
continue
419+
middle = path[len(prefix) : len(path) - len(suffix) if suffix else None]
420+
mapped = local.replace("*", middle)
421+
if cls._display_ref(mapped) == destination:
422+
candidates.add(mapped)
423+
elif (
424+
source in (cls._display_ref(remote), cls._display_ref("refs/" + remote))
425+
and cls._display_ref(local) == destination
426+
):
411427
candidates.add(local)
428+
if (control == "=" or old_commit is not None) and len(candidates) > 1:
429+
candidates.intersection_update(refs)
412430
if not candidates:
413431
candidates = {path for path in refs if cls._display_ref(path) == destination}
414432
# Git shortens refs/tags/refs/heads/main to refs/heads/main, so a

‎test/test_remote_cli.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ def test_fetch_resolves_explicit_destination_that_looks_like_a_local_tag(remote_
7373
assert clone.tags["refs/archive/v1"].commit == clone.head.commit
7474

7575

76+
@pytest.mark.parametrize("remote_fixture", [("notes-and-tag-wildcards",)], indirect=True)
77+
def test_fetch_resolves_notes_with_a_tag_wildcard(remote_fixture):
78+
with Repo(remote_fixture / "source") as source, Repo(remote_fixture / "clone") as clone:
79+
remote = clone.remote()
80+
for flags in (0, FetchInfo.HEAD_UPTODATE, FetchInfo.FAST_FORWARD):
81+
if flags == FetchInfo.FAST_FORWARD:
82+
source.git.update_ref("refs/notes/commits", "refs/fixture/updated-notes")
83+
[info] = [info for info in remote.fetch() if info.remote_ref_path == "refs/notes/commits"]
84+
assert info.ref.path == "refs/notes/commits"
85+
assert info.commit == source.commit("refs/notes/commits")
86+
assert info.flags == flags
87+
88+
7689
@pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True)
7790
def test_quoted_remote_and_submodule_names(remote_fixture):
7891
tmp_path = remote_fixture

‎tests/fixtures/remote-cli.sh‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ case "$1" in
2222
git -C source commit --allow-empty -m second
2323
git -C source tag v1
2424
;;
25+
notes-and-tag-wildcards)
26+
git clone source clone
27+
git -C clone remote set-url origin ../source
28+
git -C clone config --unset-all remote.origin.fetch
29+
git -C clone config --add remote.origin.fetch '+refs/notes/*:refs/notes/*'
30+
git -C clone config --add remote.origin.fetch '+refs/tags/*:refs/tags/*'
31+
git -C source notes add -m 'a note'
32+
initial_notes=$(git -C source rev-parse refs/notes/commits)
33+
git -C source notes append -m 'more notes'
34+
git -C source update-ref refs/fixture/updated-notes refs/notes/commits
35+
git -C source update-ref refs/notes/commits "$initial_notes"
36+
;;
2537
quoted)
2638
git init --initial-branch=main parent
2739
;;

0 commit comments

Comments
 (0)