Skip to content

Commit 0816ef5

Browse files
authored
Merge pull request #2360 from Keerthana-64/fetch-tag-full-ref-name
fix(remote): un fetched tags ko resolve karna jinke naam full refs jaise dikhte hain
2 parents 1003977 + 2e64c48 commit 0816ef5

3 files changed

Lines changed: 119 additions & 13 deletions

File tree

‎git/remote.py‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
NoReturn,
2323
Optional,
2424
Sequence,
25+
Set,
2526
TypeVar,
2627
Union,
2728
cast,
@@ -371,7 +372,13 @@ def _display_ref(path: str) -> str:
371372
return path
372373

373374
@classmethod
374-
def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: Sequence[str] = ()) -> "FetchInfo":
375+
def _from_line(
376+
cls,
377+
repo: "Repo",
378+
line: str,
379+
refspecs: Sequence[str] = (),
380+
refs_by_display: Optional[Dict[str, Set[str]]] = None,
381+
) -> "FetchInfo":
375382
"""Decode Git's full verbose fetch output without reading FETCH_HEAD."""
376383
match = cls._re_fetch_result.match(line)
377384
if match is None:
@@ -394,26 +401,46 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs:
394401
if destination == "FETCH_HEAD":
395402
ref = SymbolicReference(repo, destination)
396403
else:
404+
# Git's "new" status describes the source namespace.
405+
if flags & cls.NEW_TAG:
406+
wildcard_sources = ["refs/tags/" + source]
407+
elif flags & cls.NEW_HEAD:
408+
wildcard_sources = ["refs/heads/" + source]
409+
else:
410+
wildcard_sources = ["refs/remotes/" + source]
411+
if operation != "[new ref]":
412+
wildcard_sources.extend(("refs/heads/" + source, "refs/tags/" + source))
413+
if cls._display_ref(source) == source:
414+
wildcard_sources.append(source)
415+
397416
candidates = set()
398417
for spec in refspecs:
399418
if spec.startswith("^") or ":" not in spec:
400419
continue
401420
remote, local = spec.lstrip("+").split(":", 1)
402-
remote = cls._display_ref(remote)
403421
if "*" in remote:
404422
prefix, suffix = remote.split("*", 1)
405-
if not source.startswith(prefix) or not source.endswith(suffix):
406-
continue
407-
middle = source[len(prefix) : len(source) - len(suffix) if suffix else None]
408-
local = local.replace("*", middle)
409-
elif remote != source:
410-
continue
411-
if cls._display_ref(local) == destination:
423+
for path in wildcard_sources:
424+
if not path.startswith(prefix) or not path.endswith(suffix):
425+
continue
426+
middle = path[len(prefix) : len(path) - len(suffix) if suffix else None]
427+
mapped = local.replace("*", middle)
428+
if cls._display_ref(mapped) == destination:
429+
candidates.add(mapped)
430+
elif (
431+
source in (cls._display_ref(remote), cls._display_ref("refs/" + remote))
432+
and cls._display_ref(local) == destination
433+
):
412434
candidates.add(local)
413-
if destination.startswith("refs/"):
414-
candidates = {destination}
435+
matching_refs = refs_by_display.get(destination, ()) if refs_by_display is not None else ()
436+
if (control == "=" or old_commit is not None) and len(candidates) > 1:
437+
candidates.intersection_update(matching_refs)
415438
if not candidates:
416-
candidates = {path for path in refs if cls._display_ref(path) == destination}
439+
candidates.update(matching_refs)
440+
# Git shortens refs/tags/refs/heads/main to refs/heads/main, so a
441+
# displayed name under refs/ is only a full name if nothing shortens to it.
442+
if not candidates and destination.startswith("refs/"):
443+
candidates = {destination}
417444
if len(candidates) > 1 and ("tag" in operation or control == "t"):
418445
candidates = {path for path in candidates if path.startswith("refs/tags/")}
419446
if len(candidates) != 1:
@@ -820,14 +847,17 @@ def _get_fetch_info_from_stderr(
820847
continue
821848

822849
refs = self.repo.git._call_process_safe("for_each_ref", "--format=%(refname)").split("\n")
850+
refs_by_display: Dict[str, Set[str]] = {}
851+
for path in refs:
852+
refs_by_display.setdefault(FetchInfo._display_ref(path), set()).add(path)
823853
try:
824854
configured = self.config_reader.config.get_values(self._config_section_name(), "fetch")
825855
except (cp.NoSectionError, cp.NoOptionError):
826856
configured = []
827857
specs = [*refspecs, *configured]
828858
for line in fetch_info_lines:
829859
try:
830-
output.append(FetchInfo._from_line(self.repo, line, specs, refs))
860+
output.append(FetchInfo._from_line(self.repo, line, specs, refs_by_display))
831861
except ValueError as exc:
832862
_logger.warning("Cannot decode fetch result %r: %s", line, exc)
833863
# A source-only refspec reports both FETCH_HEAD and any opportunistic

‎test/test_remote_cli.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,51 @@ 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+
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+
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+
89+
@pytest.mark.parametrize("remote_fixture", [("prefetch",)], indirect=True)
90+
def test_prefetch_indexes_displayed_refs_once(remote_fixture):
91+
paths = {"refs/prefetch/remotes/origin/main", "refs/prefetch/remotes/origin/second"}
92+
with Repo(remote_fixture / "clone") as clone:
93+
with mock.patch.object(FetchInfo, "_display_ref", wraps=FetchInfo._display_ref) as display_ref:
94+
result = clone.remote().fetch(prefetch=True)
95+
assert {info.ref.path for info in result} == paths
96+
assert all(info.flags == FetchInfo.HEAD_UPTODATE for info in result)
97+
displayed = [args[0] for args, _ in display_ref.call_args_list if args[0] in paths]
98+
assert len(displayed) == len(set(displayed))
99+
100+
56101
@pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True)
57102
def test_quoted_remote_and_submodule_names(remote_fixture):
58103
tmp_path = remote_fixture

‎tests/fixtures/remote-cli.sh‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@ 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+
;;
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+
;;
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+
;;
37+
prefetch)
38+
git -C source branch second
39+
git clone source clone
40+
git -C clone remote set-url origin ../source
41+
git -C clone fetch --prefetch
42+
;;
1243
quoted)
1344
git init --initial-branch=main parent
1445
;;

0 commit comments

Comments
 (0)