25
25
26
26
from __future__ import annotations
27
27
28
- from typing import TYPE_CHECKING , Any , Generator , Iterator , Literal , TypedDict
28
+ import warnings
29
+ from typing import TYPE_CHECKING , Any , Generator , Iterator , Literal
29
30
30
31
# Import from pygit2
31
32
from pygit2 import RemoteCallbacks
50
51
from .repository import BaseRepository
51
52
52
53
53
- class LsRemotesDict (TypedDict ):
54
+ class RemoteHead :
55
+ """
56
+ Description of a reference advertised by a remote server,
57
+ given out on `Remote.ls_remotes` calls.
58
+ """
59
+
54
60
local : bool
55
- loid : None | Oid
61
+ """Available locally"""
62
+
63
+ oid : Oid
64
+
65
+ loid : Oid | None
66
+
56
67
name : str | None
68
+
57
69
symref_target : str | None
58
- oid : Oid
70
+ """
71
+ If the server sent a symref mapping for this ref, this will
72
+ point to the target.
73
+ """
74
+
75
+ def __init__ (self , c_struct : Any ) -> None :
76
+ self .local = bool (c_struct .local )
77
+ if self .local :
78
+ self .loid = Oid (raw = bytes (ffi .buffer (c_struct .loid .id )[:]))
79
+ else :
80
+ self .loid = None
81
+
82
+ self .oid = Oid (raw = bytes (ffi .buffer (c_struct .oid .id )[:]))
83
+ self .name = maybe_string (c_struct .name )
84
+ self .symref_target = maybe_string (c_struct .symref_target )
85
+
86
+ def __getitem__ (self , item : str ) -> Any :
87
+ """
88
+ DEPRECATED: Backwards compatibility with legacy user code
89
+ that expects this object to be a dictionary with string keys.
90
+ """
91
+ warnings .warn (
92
+ 'ls_remotes no longer returns a dict. '
93
+ 'Update your code to read from fields instead '
94
+ '(e.g. result["name"] --> result.name)' ,
95
+ DeprecationWarning ,
96
+ )
97
+ return getattr (self , item )
59
98
60
99
61
100
class PushUpdate :
@@ -228,10 +267,10 @@ def ls_remotes(
228
267
callbacks : RemoteCallbacks | None = None ,
229
268
proxy : str | None | bool = None ,
230
269
connect : bool = True ,
231
- ) -> list [LsRemotesDict ]:
270
+ ) -> list [RemoteHead ]:
232
271
"""
233
- Return a list of dicts that maps to `git_remote_head` from a
234
- `ls_remotes` call .
272
+ Get the list of references with which the server responds to a new
273
+ connection .
235
274
236
275
Parameters:
237
276
@@ -247,32 +286,14 @@ def ls_remotes(
247
286
if connect :
248
287
self .connect (callbacks = callbacks , proxy = proxy )
249
288
250
- refs = ffi .new ('git_remote_head ***' )
251
- refs_len = ffi .new ('size_t *' )
289
+ refs_ptr = ffi .new ('git_remote_head ***' )
290
+ size_ptr = ffi .new ('size_t *' )
252
291
253
- err = C .git_remote_ls (refs , refs_len , self ._remote )
292
+ err = C .git_remote_ls (refs_ptr , size_ptr , self ._remote )
254
293
check_error (err )
255
294
256
- results = []
257
- for i in range (int (refs_len [0 ])):
258
- ref = refs [0 ][i ]
259
- local = bool (ref .local )
260
- if local :
261
- loid = Oid (raw = bytes (ffi .buffer (ref .loid .id )[:]))
262
- else :
263
- loid = None
264
-
265
- remote = LsRemotesDict (
266
- {
267
- 'local' : local ,
268
- 'loid' : loid ,
269
- 'name' : maybe_string (ref .name ),
270
- 'symref_target' : maybe_string (ref .symref_target ),
271
- 'oid' : Oid (raw = bytes (ffi .buffer (ref .oid .id )[:])),
272
- }
273
- )
274
-
275
- results .append (remote )
295
+ num_refs = int (size_ptr [0 ])
296
+ results = [RemoteHead (refs_ptr [0 ][i ]) for i in range (num_refs )]
276
297
277
298
return results
278
299
0 commit comments