Skip to content

Commit e9c4d6d

Browse files
authored
fix(pygit2) migrate ls_remotes to list_heads (#453)
1 parent 802269b commit e9c4d6d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/scmrepo/git/backend/pygit2/__init__.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,26 @@ def _get_remote(self, url: str) -> Iterator["Remote"]:
693693
raise NotImplementedError
694694
yield remote
695695

696+
@staticmethod
697+
def _get_remote_refs(remote: "Remote", callbacks) -> dict[str, "Oid"]:
698+
"""Get remote refs using the appropriate pygit2 API.
699+
700+
pygit2 1.19.0 deprecated ls_remotes() in favor of list_heads(),
701+
but 1.19.0+ requires Python 3.11+.
702+
"""
703+
if hasattr(remote, "list_heads"):
704+
result: dict[str, Oid] = {}
705+
for head in remote.list_heads(callbacks=callbacks, proxy=True):
706+
assert head.name is not None
707+
result[head.name] = head.oid
708+
return result
709+
return {
710+
head["name"]: head["oid"]
711+
for head in remote.ls_remotes( # type: ignore[attr-defined]
712+
callbacks=callbacks, proxy=True
713+
)
714+
}
715+
696716
def fetch_refspecs(
697717
self,
698718
url: str,
@@ -731,14 +751,9 @@ def _default_status(
731751
SCMError(f"Git failed to fetch ref from '{url}'"),
732752
):
733753
with RemoteCallbacks(progress=progress) as cb:
734-
remote_refs: dict[str, Oid] = (
735-
{
736-
head["name"]: head["oid"]
737-
for head in remote.ls_remotes(callbacks=cb, proxy=True)
738-
}
739-
if not force
740-
else {}
741-
)
754+
remote_refs: dict[str, Oid] = {}
755+
if not force:
756+
remote_refs = self._get_remote_refs(remote, cb)
742757
remote.fetch(
743758
refspecs=refspecs, callbacks=cb, message="fetch", proxy=True
744759
)

0 commit comments

Comments
 (0)