25
25
26
26
if TYPE_CHECKING :
27
27
from git .repo import Repo
28
- from git .refs import Reference , Head , HEAD , TagReference , RemoteReference
28
+ from git .refs import Reference , Head , TagReference , RemoteReference
29
29
30
30
T_References = TypeVar ('T_References' , bound = 'SymbolicReference' )
31
31
@@ -60,6 +60,7 @@ class SymbolicReference(object):
60
60
def __init__ (self , repo : 'Repo' , path : PathLike , check_path : bool = False ) -> None :
61
61
self .repo = repo
62
62
self .path = str (path )
63
+ self .ref = self ._get_reference ()
63
64
64
65
def __str__ (self ) -> str :
65
66
return self .path
@@ -279,7 +280,7 @@ def set_object(self, object, logmsg=None): # @ReservedAssignment
279
280
object = property (_get_object , set_object , doc = "Return the object our ref currently refers to" )
280
281
281
282
def _get_reference (self
282
- ) -> Union ['HEAD' , ' Head' , 'RemoteReference' , 'TagReference' , 'Reference' , 'SymbolicReference ' ]:
283
+ ) -> Union ['Head' , 'RemoteReference' , 'TagReference' , 'Reference' ]:
283
284
""":return: Reference Object we point to
284
285
:raise TypeError: If this symbolic reference is detached, hence it doesn't point
285
286
to a reference, but to a commit"""
@@ -288,7 +289,8 @@ def _get_reference(self
288
289
raise TypeError ("%s is a detached symbolic reference as it points to %r" % (self , sha ))
289
290
return self .from_path (self .repo , target_ref_path )
290
291
291
- def set_reference (self , ref , logmsg = None ):
292
+ def set_reference (self , ref : Union [str , Commit_ish , 'SymbolicReference' ], logmsg : Union [str , None ] = None
293
+ ) -> None :
292
294
"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
293
295
Otherwise an Object, given as Object instance or refspec, is assumed and if valid,
294
296
will be set which effectively detaches the refererence if it was a purely
@@ -355,12 +357,16 @@ def set_reference(self, ref, logmsg=None):
355
357
if logmsg is not None :
356
358
self .log_append (oldbinsha , logmsg )
357
359
358
- return self
360
+ return None
359
361
360
- # aliased reference
361
- reference : Union [Commit_ish , 'Head' , 'Reference' ] = property ( # type: ignore
362
- _get_reference , set_reference , doc = "Returns the Reference we point to" )
363
- ref = reference
362
+ @ property
363
+ def reference (self ) -> Union ['Head' , 'RemoteReference' , 'TagReference' , 'Reference' ]:
364
+ return self ._get_reference ()
365
+
366
+ @ reference .setter
367
+ def reference (self , ref : Union [str , Commit_ish , 'SymbolicReference' ], logmsg : Union [str , None ] = None
368
+ ) -> None :
369
+ return self .set_reference (ref = ref , logmsg = logmsg )
364
370
365
371
def is_valid (self ) -> bool :
366
372
"""
@@ -374,7 +380,7 @@ def is_valid(self) -> bool:
374
380
else :
375
381
return True
376
382
377
- @property
383
+ @ property
378
384
def is_detached (self ):
379
385
"""
380
386
:return:
@@ -424,7 +430,7 @@ def log_entry(self, index):
424
430
In that case, it will be faster than the ``log()`` method"""
425
431
return RefLog .entry_at (RefLog .path (self ), index )
426
432
427
- @classmethod
433
+ @ classmethod
428
434
def to_full_path (cls , path : Union [PathLike , 'SymbolicReference' ]) -> str :
429
435
"""
430
436
:return: string with a full repository-relative path which can be used to initialize
@@ -439,7 +445,7 @@ def to_full_path(cls, path: Union[PathLike, 'SymbolicReference']) -> str:
439
445
full_ref_path = '%s/%s' % (cls ._common_path_default , path )
440
446
return full_ref_path
441
447
442
- @classmethod
448
+ @ classmethod
443
449
def delete (cls , repo : 'Repo' , path : PathLike ) -> None :
444
450
"""Delete the reference at the given path
445
451
@@ -497,8 +503,10 @@ def delete(cls, repo: 'Repo', path: PathLike) -> None:
497
503
os .remove (reflog_path )
498
504
# END remove reflog
499
505
500
- @classmethod
501
- def _create (cls , repo , path , resolve , reference , force , logmsg = None ):
506
+ @ classmethod
507
+ def _create (cls : Type [T_References ], repo : 'Repo' , path : PathLike , resolve : bool ,
508
+ reference : Union [str , 'SymbolicReference' ],
509
+ force : bool , logmsg : Union [str , None ] = None ) -> T_References :
502
510
"""internal method used to create a new symbolic reference.
503
511
If resolve is False, the reference will be taken as is, creating
504
512
a proper symbolic reference. Otherwise it will be resolved to the
@@ -531,8 +539,9 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
531
539
return ref
532
540
533
541
@classmethod
534
- def create (cls , repo : 'Repo' , path : PathLike , reference : Union [Commit_ish , str ] = 'SymbolicReference' ,
535
- logmsg : Union [str , None ] = None , force : bool = False , ** kwargs : Any ):
542
+ def create (cls : Type [T_References ], repo : 'Repo' , path : PathLike ,
543
+ reference : Union [Commit_ish , str , 'SymbolicReference' ] = 'SymbolicReference' ,
544
+ logmsg : Union [str , None ] = None , force : bool = False , ** kwargs : Any ) -> T_References :
536
545
"""Create a new symbolic reference, hence a reference pointing , to another reference.
537
546
538
547
:param repo:
@@ -669,7 +678,7 @@ def iter_items(cls, repo: 'Repo', common_path: Union[PathLike, None] = None, *ar
669
678
return (r for r in cls ._iter_items (repo , common_path ) if r .__class__ == SymbolicReference or not r .is_detached )
670
679
671
680
@classmethod
672
- def from_path (cls , repo , path ) :
681
+ def from_path (cls , repo : 'Repo' , path : PathLike ) -> Union [ 'Head' , 'RemoteReference' , 'TagReference' , 'Reference' ] :
673
682
"""
674
683
:param path: full .git-directory-relative path name to the Reference to instantiate
675
684
:note: use to_full_path() if you only have a partial path of a known Reference Type
0 commit comments