Skip to content

Commit 2c9792a

Browse files
committed
Deprecate Remote.ls_remotes in favor of Remote.list_heads
1 parent 5c8ad80 commit 2c9792a

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

pygit2/remotes.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
class RemoteHead:
5656
"""
5757
Description of a reference advertised by a remote server,
58-
given out on `Remote.ls_remotes` calls.
58+
given out on `Remote.list_heads` calls.
5959
"""
6060

6161
local: bool
@@ -80,19 +80,6 @@ def __init__(self, c_struct: Any) -> None:
8080
self.name = maybe_string(c_struct.name)
8181
self.symref_target = maybe_string(c_struct.symref_target)
8282

83-
def __getitem__(self, item: str) -> Any:
84-
"""
85-
DEPRECATED: Backwards compatibility with legacy user code
86-
that expects this object to be a dictionary with string keys.
87-
"""
88-
warnings.warn(
89-
'ls_remotes no longer returns a dict. '
90-
'Update your code to read from fields instead '
91-
'(e.g. result["name"] --> result.name)',
92-
DeprecationWarning,
93-
)
94-
return getattr(self, item)
95-
9683

9784
class PushUpdate:
9885
"""
@@ -259,7 +246,7 @@ def fetch(
259246

260247
return TransferProgress(C.git_remote_stats(self._remote))
261248

262-
def ls_remotes(
249+
def list_heads(
263250
self,
264251
callbacks: RemoteCallbacks | None = None,
265252
proxy: str | None | bool = None,
@@ -294,6 +281,30 @@ def ls_remotes(
294281

295282
return results
296283

284+
def ls_remotes(
285+
self,
286+
callbacks: RemoteCallbacks | None = None,
287+
proxy: str | None | bool = None,
288+
connect: bool = True,
289+
) -> list[dict[str, Any]]:
290+
"""
291+
Deprecated interface to list_heads
292+
"""
293+
warnings.warn('Use list_heads', DeprecationWarning)
294+
295+
heads = self.list_heads(callbacks, proxy, connect)
296+
297+
return [
298+
{
299+
'local': h.local,
300+
'oid': h.oid,
301+
'loid': h.loid,
302+
'name': h.name,
303+
'symref_target': h.symref_target,
304+
}
305+
for h in heads
306+
]
307+
297308
def prune(self, callbacks: RemoteCallbacks | None = None) -> None:
298309
"""Perform a prune against this remote."""
299310
with git_remote_callbacks(callbacks) as payload:

test/test_remote.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,41 +193,48 @@ def test_remote_list(testrepo: Repository) -> None:
193193

194194

195195
@utils.requires_network
196-
def test_ls_remotes(testrepo: Repository) -> None:
196+
def test_list_heads(testrepo: Repository) -> None:
197197
assert 1 == len(testrepo.remotes)
198198
remote = testrepo.remotes[0]
199199

200-
refs = remote.ls_remotes()
200+
refs = remote.list_heads()
201201
assert refs
202202

203203
# Check that a known ref is returned.
204204
assert next(iter(r for r in refs if r.name == 'refs/tags/v0.28.2'))
205205

206206

207207
@utils.requires_network
208-
def test_ls_remotes_backwards_compatibility(testrepo: Repository) -> None:
208+
def test_ls_remotes_deprecated(testrepo: Repository) -> None:
209209
assert 1 == len(testrepo.remotes)
210210
remote = testrepo.remotes[0]
211-
refs = remote.ls_remotes()
212-
ref = refs[0]
213211

214-
for field in ('name', 'oid', 'loid', 'local', 'symref_target'):
215-
new_value = getattr(ref, field)
216-
with pytest.warns(DeprecationWarning, match='no longer returns a dict'):
217-
old_value = ref[field]
218-
assert new_value == old_value
212+
new_refs = remote.list_heads()
213+
214+
with pytest.warns(DeprecationWarning, match='Use list_heads'):
215+
old_refs = remote.ls_remotes()
216+
217+
assert new_refs
218+
assert old_refs
219+
220+
for new, old in zip(new_refs, old_refs, strict=True):
221+
assert new.name == old['name']
222+
assert new.oid == old['oid']
223+
assert new.loid == old['loid']
224+
assert new.local == old['local']
225+
assert new.symref_target == old['symref_target']
219226

220227

221228
@utils.requires_network
222-
def test_ls_remotes_without_implicit_connect(testrepo: Repository) -> None:
229+
def test_list_heads_without_implicit_connect(testrepo: Repository) -> None:
223230
assert 1 == len(testrepo.remotes)
224231
remote = testrepo.remotes[0]
225232

226233
with pytest.raises(pygit2.GitError, match='this remote has never connected'):
227-
remote.ls_remotes(connect=False)
234+
remote.list_heads(connect=False)
228235

229236
remote.connect()
230-
refs = remote.ls_remotes(connect=False)
237+
refs = remote.list_heads(connect=False)
231238
assert refs
232239

233240
# Check that a known ref is returned.
@@ -328,7 +335,7 @@ def update_tips(self, name: str, old: pygit2.Oid, new: pygit2.Oid) -> None:
328335

329336

330337
@utils.requires_network
331-
def test_ls_remotes_certificate_check() -> None:
338+
def test_list_heads_certificate_check() -> None:
332339
url = 'https://github.com/pygit2/empty.git'
333340

334341
class MyCallbacks(pygit2.RemoteCallbacks):
@@ -350,7 +357,7 @@ def certificate_check(
350357
remote = git.remotes.create_anonymous(url)
351358

352359
callbacks = MyCallbacks()
353-
refs = remote.ls_remotes(callbacks=callbacks)
360+
refs = remote.list_heads(callbacks=callbacks)
354361

355362
# Sanity check that we indeed got some refs.
356363
assert len(refs) > 0

0 commit comments

Comments
 (0)