36
36
37
37
# typing-------------------------------------------------------
38
38
39
- from typing import Any , Callable , Optional , TYPE_CHECKING , Union , overload
39
+ from typing import Any , Callable , Dict , Optional , TYPE_CHECKING , Union , cast , overload
40
40
41
41
from git .types import PathLike , Literal
42
42
43
43
if TYPE_CHECKING :
44
44
from git .repo .base import Repo
45
45
from git .objects .commit import Commit
46
+ from git .objects .blob import Blob
47
+ from git .objects .tree import Tree
48
+ from git .objects .tag import TagObject
46
49
50
+ flagKeyLiteral = Literal [' ' , '!' , '+' , '-' , '*' , '=' , 't' ]
47
51
# -------------------------------------------------------------
48
52
49
53
log = logging .getLogger ('git.remote' )
@@ -131,7 +135,7 @@ class PushInfo(object):
131
135
'=' : UP_TO_DATE ,
132
136
'!' : ERROR }
133
137
134
- def __init__ (self , flags : int , local_ref : Union [SymbolicReference , None ], remote_ref_string : str , remote ,
138
+ def __init__ (self , flags : int , local_ref : Union [SymbolicReference , None ], remote_ref_string : str , remote : 'Remote' ,
135
139
old_commit : Optional [str ] = None , summary : str = '' ) -> None :
136
140
""" Initialize a new instance
137
141
local_ref: HEAD | Head | RemoteReference | TagReference | Reference | SymbolicReference | None """
@@ -143,7 +147,7 @@ def __init__(self, flags: int, local_ref: Union[SymbolicReference, None], remote
143
147
self .summary = summary
144
148
145
149
@property
146
- def old_commit (self ) -> Optional [ bool ]:
150
+ def old_commit (self ) -> Union [ str , SymbolicReference , 'Commit' , 'TagObject' , 'Blob' , 'Tree' , None ]:
147
151
return self ._old_commit_sha and self ._remote .repo .commit (self ._old_commit_sha ) or None
148
152
149
153
@property
@@ -246,7 +250,7 @@ class FetchInfo(object):
246
250
'=' : HEAD_UPTODATE ,
247
251
' ' : FAST_FORWARD ,
248
252
'-' : TAG_UPDATE ,
249
- }
253
+ } # type: Dict[flagKeyLiteral, int]
250
254
251
255
@classmethod
252
256
def refresh (cls ) -> Literal [True ]:
@@ -297,7 +301,7 @@ def commit(self) -> 'Commit':
297
301
return self .ref .commit
298
302
299
303
@classmethod
300
- def _from_line (cls , repo , line , fetch_line ):
304
+ def _from_line (cls , repo : Repo , line : str , fetch_line ) -> 'FetchInfo' :
301
305
"""Parse information from the given line as returned by git-fetch -v
302
306
and return a new FetchInfo object representing this information.
303
307
@@ -319,7 +323,9 @@ def _from_line(cls, repo, line, fetch_line):
319
323
raise ValueError ("Failed to parse line: %r" % line )
320
324
321
325
# parse lines
322
- control_character , operation , local_remote_ref , remote_local_ref , note = match .groups ()
326
+ control_character , operation , local_remote_ref , remote_local_ref_str , note = match .groups ()
327
+ control_character = cast (flagKeyLiteral , control_character ) # can do this neater once 3.5 dropped
328
+
323
329
try :
324
330
_new_hex_sha , _fetch_operation , fetch_note = fetch_line .split ("\t " )
325
331
ref_type_name , fetch_note = fetch_note .split (' ' , 1 )
@@ -359,7 +365,7 @@ def _from_line(cls, repo, line, fetch_line):
359
365
# the fetch result is stored in FETCH_HEAD which destroys the rule we usually
360
366
# have. In that case we use a symbolic reference which is detached
361
367
ref_type = None
362
- if remote_local_ref == "FETCH_HEAD" :
368
+ if remote_local_ref_str == "FETCH_HEAD" :
363
369
ref_type = SymbolicReference
364
370
elif ref_type_name == "tag" or is_tag_operation :
365
371
# the ref_type_name can be branch, whereas we are still seeing a tag operation. It happens during
@@ -387,21 +393,21 @@ def _from_line(cls, repo, line, fetch_line):
387
393
# by the 'ref/' prefix. Otherwise even a tag could be in refs/remotes, which is when it will have the
388
394
# 'tags/' subdirectory in its path.
389
395
# We don't want to test for actual existence, but try to figure everything out analytically.
390
- ref_path = None
391
- remote_local_ref = remote_local_ref .strip ()
392
- if remote_local_ref .startswith (Reference ._common_path_default + "/" ):
396
+ ref_path = None # type: Optional[PathLike]
397
+ remote_local_ref_str = remote_local_ref_str .strip ()
398
+ if remote_local_ref_str .startswith (Reference ._common_path_default + "/" ):
393
399
# always use actual type if we get absolute paths
394
400
# Will always be the case if something is fetched outside of refs/remotes (if its not a tag)
395
- ref_path = remote_local_ref
401
+ ref_path = remote_local_ref_str
396
402
if ref_type is not TagReference and not \
397
- remote_local_ref .startswith (RemoteReference ._common_path_default + "/" ):
403
+ remote_local_ref_str .startswith (RemoteReference ._common_path_default + "/" ):
398
404
ref_type = Reference
399
405
# END downgrade remote reference
400
- elif ref_type is TagReference and 'tags/' in remote_local_ref :
406
+ elif ref_type is TagReference and 'tags/' in remote_local_ref_str :
401
407
# even though its a tag, it is located in refs/remotes
402
- ref_path = join_path (RemoteReference ._common_path_default , remote_local_ref )
408
+ ref_path = join_path (RemoteReference ._common_path_default , remote_local_ref_str )
403
409
else :
404
- ref_path = join_path (ref_type ._common_path_default , remote_local_ref )
410
+ ref_path = join_path (ref_type ._common_path_default , remote_local_ref_str )
405
411
# END obtain refpath
406
412
407
413
# even though the path could be within the git conventions, we make
0 commit comments