1616BadName
1717)
1818
19- from .log import RefLog
19+ from .log import RefLog , RefLogEntry
2020
2121# typing ------------------------------------------------------------------
2222
2626if TYPE_CHECKING :
2727from git .repo import Repo
2828from git .refs import Reference ,Head ,TagReference ,RemoteReference
29+ from git .config import GitConfigParser
30+ from git .objects .commit import Actor
2931
3032T_References = TypeVar ('T_References' ,bound = 'SymbolicReference' )
3133
@@ -229,11 +231,13 @@ def set_commit(self, commit: Union[Commit, 'SymbolicReference', str],
229231invalid_type = False
230232if isinstance (commit ,Object ):
231233invalid_type = commit .type != Commit .type
234+ commit = cast ('Commit' ,commit )
232235elif isinstance (commit ,SymbolicReference ):
233236invalid_type = commit .object .type != Commit .type
234237else :
235238try :
236- invalid_type = self .repo .rev_parse (commit ).type != Commit .type
239+ commit = self .repo .rev_parse (commit )
240+ invalid_type = commit .type != Commit .type
237241except (BadObject ,BadName )as e :
238242raise ValueError ("Invalid object: %s" % commit )from e
239243# END handle exception
@@ -249,7 +253,9 @@ def set_commit(self, commit: Union[Commit, 'SymbolicReference', str],
249253# return self
250254return None
251255
252- def set_object (self ,object ,logmsg = None ):# @ReservedAssignment
256+ def set_object (self ,object :Union [Commit_ish ,'SymbolicReference' ],
257+ logmsg :Union [str ,None ]= None
258+ )-> 'SymbolicReference' :# @ReservedAssignment
253259"""Set the object we point to, possibly dereference our symbolic reference first.
254260 If the reference does not exist, it will be created
255261
@@ -276,8 +282,8 @@ def set_object(self, object, logmsg=None): # @ReservedAssignment
276282# set the commit on our reference
277283return self ._get_reference ().set_object (object ,logmsg )
278284
279- commit = property (_get_commit ,set_commit ,doc = "Query or set commits directly" )
280- object = property (_get_object ,set_object ,doc = "Return the object our ref currently refers to" )
285+ commit = cast ( 'Commit' , property (_get_commit ,set_commit ,doc = "Query or set commits directly" ) )
286+ object = property (_get_object ,set_object ,doc = "Return the object our ref currently refers to" )# type: ignore
281287
282288def _get_reference (self
283289 )-> Union ['Head' ,'RemoteReference' ,'TagReference' ,'Reference' ]:
@@ -290,7 +296,7 @@ def _get_reference(self
290296return self .from_path (self .repo ,target_ref_path )
291297
292298def set_reference (self ,ref :Union [str ,Commit_ish ,'SymbolicReference' ],logmsg :Union [str ,None ]= None
293- )-> None :
299+ )-> 'SymbolicReference' :
294300"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
295301 Otherwise an Object, given as Object instance or refspec, is assumed and if valid,
296302 will be set which effectively detaches the refererence if it was a purely
@@ -331,7 +337,7 @@ def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg
331337raise TypeError ("Require commit, got %r" % obj )
332338# END verify type
333339
334- oldbinsha = None
340+ oldbinsha : bytes = b''
335341if logmsg is not None :
336342try :
337343oldbinsha = self .commit .binsha
@@ -357,15 +363,15 @@ def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg
357363if logmsg is not None :
358364self .log_append (oldbinsha ,logmsg )
359365
360- return None
366+ return self
361367
362368@property
363369def reference (self )-> Union ['Head' ,'RemoteReference' ,'TagReference' ,'Reference' ]:
364370return self ._get_reference ()
365371
366372@reference .setter
367373def reference (self ,ref :Union [str ,Commit_ish ,'SymbolicReference' ],logmsg :Union [str ,None ]= None
368- )-> None :
374+ )-> 'SymbolicReference' :
369375return self .set_reference (ref = ref ,logmsg = logmsg )
370376
371377def is_valid (self )-> bool :
@@ -392,7 +398,7 @@ def is_detached(self):
392398except TypeError :
393399return True
394400
395- def log (self ):
401+ def log (self )-> 'RefLog' :
396402"""
397403 :return: RefLog for this reference. Its last entry reflects the latest change
398404 applied to this reference
@@ -401,7 +407,8 @@ def log(self):
401407 instead of calling this method repeatedly. It should be considered read-only."""
402408return RefLog .from_file (RefLog .path (self ))
403409
404- def log_append (self ,oldbinsha ,message ,newbinsha = None ):
410+ def log_append (self ,oldbinsha :bytes ,message :Union [str ,None ],
411+ newbinsha :Union [bytes ,None ]= None )-> 'RefLogEntry' :
405412"""Append a logentry to the logfile of this ref
406413
407414 :param oldbinsha: binary sha this ref used to point to
@@ -413,15 +420,19 @@ def log_append(self, oldbinsha, message, newbinsha=None):
413420# correct to allow overriding the committer on a per-commit level.
414421# See https://github.com/gitpython-developers/GitPython/pull/146
415422try :
416- committer_or_reader = self .commit .committer
423+ committer_or_reader : Union [ 'Actor' , 'GitConfigParser' ] = self .commit .committer
417424except ValueError :
418425committer_or_reader = self .repo .config_reader ()
419426# end handle newly cloned repositories
420- return RefLog .append_entry (committer_or_reader ,RefLog .path (self ),oldbinsha ,
421- (newbinsha is None and self .commit .binsha )or newbinsha ,
422- message )
427+ if newbinsha is None :
428+ newbinsha = self .commit .binsha
429+
430+ if message is None :
431+ message = ''
432+
433+ return RefLog .append_entry (committer_or_reader ,RefLog .path (self ),oldbinsha ,newbinsha ,message )
423434
424- def log_entry (self ,index ) :
435+ def log_entry (self ,index : int ) -> RefLogEntry :
425436""":return: RefLogEntry at the given index
426437 :param index: python list compatible positive or negative index
427438
@@ -540,7 +551,7 @@ def _create(cls: Type[T_References], repo: 'Repo', path: PathLike, resolve: bool
540551
541552@classmethod
542553def create (cls :Type [T_References ],repo :'Repo' ,path :PathLike ,
543- reference :Union [Commit_ish , str ,'SymbolicReference' ]= 'SymbolicReference' ,
554+ reference :Union [str ,'SymbolicReference' ]= 'SymbolicReference' ,
544555logmsg :Union [str ,None ]= None ,force :bool = False ,** kwargs :Any )-> T_References :
545556"""Create a new symbolic reference, hence a reference pointing , to another reference.
546557
@@ -553,7 +564,7 @@ def create(cls: Type[T_References], repo: 'Repo', path: PathLike,
553564
554565 :param reference:
555566 The reference to which the new symbolic reference should point to.
556- If it is a commit'ish, the symbolic ref will be detached.
567+ If it is aref to a commit'ish, the symbolic ref will be detached.
557568
558569 :param force:
559570 if True, force creation even if a symbolic reference with that name already exists.