22Helper classes for reading cached objects from Git's Object Database.
33"""
44
5+ from __future__ import annotations
6+
57import hashlib
68import re
79import os
@@ -48,18 +50,18 @@ class Oid(bytes):
4850
4951 __slots__ = ()
5052
51- def __new__ (cls , b : bytes ) -> " Oid" :
53+ def __new__ (cls , b : bytes ) -> Oid :
5254 if len (b ) != 20 :
5355 raise ValueError ("Expected 160-bit SHA1 hash" )
5456 return super ().__new__ (cls , b ) # type: ignore
5557
5658 @classmethod
57- def fromhex (cls , instr : str ) -> " Oid" :
59+ def fromhex (cls , instr : str ) -> Oid :
5860 """Parse an ``Oid`` from a hexadecimal string"""
5961 return Oid (bytes .fromhex (instr ))
6062
6163 @classmethod
62- def null (cls ) -> " Oid" :
64+ def null (cls ) -> Oid :
6365 """An ``Oid`` consisting of entirely 0s"""
6466 return cls (b"\0 " * 20 )
6567
@@ -68,7 +70,7 @@ def short(self) -> str:
6870 return str (self )[:12 ]
6971
7072 @classmethod
71- def for_object (cls , tag : str , body : bytes ) -> " Oid" :
73+ def for_object (cls , tag : str , body : bytes ) -> Oid :
7274 """Hash an object with the given type tag and body to determine its Oid"""
7375 hasher = hashlib .sha1 ()
7476 hasher .update (tag .encode () + b" " + str (len (body )).encode () + b"\0 " + body )
@@ -149,7 +151,7 @@ class Repository:
149151 default_committer : Signature
150152 """committer used by default for new commits"""
151153
152- index : " Index"
154+ index : Index
153155 """current index state"""
154156
155157 sign_commits : bool
@@ -158,7 +160,7 @@ class Repository:
158160 gpg : bytes
159161 """path to GnuPG binary"""
160162
161- _objects : Dict [int , Dict [Oid , " GitObj" ]]
163+ _objects : Dict [int , Dict [Oid , GitObj ]]
162164 _catfile : Popen
163165 _tempdir : Optional [TemporaryDirectory ]
164166
@@ -257,7 +259,7 @@ def int_config(self, config: str, default: T) -> Union[int, T]:
257259 except CalledProcessError :
258260 return default
259261
260- def __enter__ (self ) -> " Repository" :
262+ def __enter__ (self ) -> Repository :
261263 return self
262264
263265 def __exit__ (
@@ -287,12 +289,12 @@ def git_path(self, path: Union[str, Path]) -> Path:
287289
288290 def new_commit (
289291 self ,
290- tree : " Tree" ,
291- parents : Sequence [" Commit" ],
292+ tree : Tree ,
293+ parents : Sequence [Commit ],
292294 message : bytes ,
293295 author : Optional [Signature ] = None ,
294296 committer : Optional [Signature ] = None ,
295- ) -> " Commit" :
297+ ) -> Commit :
296298 """Directly create an in-memory commit object, without persisting it.
297299 If a commit object with these properties already exists, it will be
298300 returned instead."""
@@ -343,7 +345,7 @@ def sign_buffer(self, buffer: bytes) -> bytes:
343345 signature += b" " + line + b"\n "
344346 return signature
345347
346- def new_tree (self , entries : Mapping [bytes , " Entry" ]) -> " Tree" :
348+ def new_tree (self , entries : Mapping [bytes , Entry ]) -> Tree :
347349 """Directly create an in-memory tree object, without persisting it.
348350 If a tree object with these entries already exists, it will be
349351 returned instead."""
@@ -361,7 +363,7 @@ def entry_key(pair: Tuple[bytes, Entry]) -> bytes:
361363 body += cast (bytes , entry .mode .value ) + b" " + name + b"\0 " + entry .oid
362364 return Tree (self , body )
363365
364- def get_obj (self , ref : Union [Oid , str ]) -> " GitObj" :
366+ def get_obj (self , ref : Union [Oid , str ]) -> GitObj :
365367 """Get the identified git object from this repository. If given an
366368 :class:`Oid`, the cache will be checked before asking git."""
367369 if isinstance (ref , Oid ):
@@ -414,40 +416,40 @@ def get_obj(self, ref: Union[Oid, str]) -> "GitObj":
414416 assert obj .oid == oid , "miscomputed oid"
415417 return obj
416418
417- def get_commit (self , ref : Union [Oid , str ]) -> " Commit" :
419+ def get_commit (self , ref : Union [Oid , str ]) -> Commit :
418420 """Like :py:meth:`get_obj`, but returns a :class:`Commit`"""
419421 obj = self .get_obj (ref )
420422 if isinstance (obj , Commit ):
421423 return obj
422424 raise ValueError (f"{ type (obj ).__name__ } { ref } is not a Commit!" )
423425
424- def get_tree (self , ref : Union [Oid , str ]) -> " Tree" :
426+ def get_tree (self , ref : Union [Oid , str ]) -> Tree :
425427 """Like :py:meth:`get_obj`, but returns a :class:`Tree`"""
426428 obj = self .get_obj (ref )
427429 if isinstance (obj , Tree ):
428430 return obj
429431 raise ValueError (f"{ type (obj ).__name__ } { ref } is not a Tree!" )
430432
431- def get_blob (self , ref : Union [Oid , str ]) -> " Blob" :
433+ def get_blob (self , ref : Union [Oid , str ]) -> Blob :
432434 """Like :py:meth:`get_obj`, but returns a :class:`Blob`"""
433435 obj = self .get_obj (ref )
434436 if isinstance (obj , Blob ):
435437 return obj
436438 raise ValueError (f"{ type (obj ).__name__ } { ref } is not a Blob!" )
437439
438- def get_obj_ref (self , ref : str ) -> " Reference[GitObj]" :
440+ def get_obj_ref (self , ref : str ) -> Reference [GitObj ]:
439441 """Get a :class:`Reference` to a :class:`GitObj`"""
440442 return Reference (GitObj , self , ref )
441443
442- def get_commit_ref (self , ref : str ) -> " Reference[Commit]" :
444+ def get_commit_ref (self , ref : str ) -> Reference [Commit ]:
443445 """Get a :class:`Reference` to a :class:`Commit`"""
444446 return Reference (Commit , self , ref )
445447
446- def get_tree_ref (self , ref : str ) -> " Reference[Tree]" :
448+ def get_tree_ref (self , ref : str ) -> Reference [Tree ]:
447449 """Get a :class:`Reference` to a :class:`Tree`"""
448450 return Reference (Tree , self , ref )
449451
450- def get_blob_ref (self , ref : str ) -> " Reference[Blob]" :
452+ def get_blob_ref (self , ref : str ) -> Reference [Blob ]:
451453 """Get a :class:`Reference` to a :class:`Blob`"""
452454 return Reference (Blob , self , ref )
453455
@@ -573,11 +575,11 @@ def _parse_body(self) -> None:
573575 elif key == b"gpgsig" :
574576 self .gpgsig = value
575577
576- def tree (self ) -> " Tree" :
578+ def tree (self ) -> Tree :
577579 """``tree`` object corresponding to this commit"""
578580 return self .repo .get_tree (self .tree_oid )
579581
580- def parent_tree (self ) -> " Tree" :
582+ def parent_tree (self ) -> Tree :
581583 """``tree`` object corresponding to the first parent of this commit,
582584 or the null tree if this is a root commit"""
583585 if self .is_root :
@@ -589,11 +591,11 @@ def is_root(self) -> bool:
589591 """Whether this commit has no parents"""
590592 return not self .parent_oids
591593
592- def parents (self ) -> Sequence [" Commit" ]:
594+ def parents (self ) -> Sequence [Commit ]:
593595 """List of parent commits"""
594596 return [self .repo .get_commit (parent ) for parent in self .parent_oids ]
595597
596- def parent (self ) -> " Commit" :
598+ def parent (self ) -> Commit :
597599 """Helper method to get the single parent of a commit. Raises
598600 :class:`ValueError` if the incorrect number of parents are
599601 present."""
@@ -609,7 +611,7 @@ def summary(self) -> str:
609611 )
610612 return " " .join (summary_paragraph .splitlines ())
611613
612- def rebase (self , parent : Optional [" Commit" ]) -> " Commit" :
614+ def rebase (self , parent : Optional [Commit ]) -> Commit :
613615 """Create a new commit with the same changes, except with ``parent``
614616 as its parent. If ``parent`` is ``None``, this becomes a root commit."""
615617 from .merge import rebase # pylint: disable=import-outside-toplevel
@@ -618,12 +620,12 @@ def rebase(self, parent: Optional["Commit"]) -> "Commit":
618620
619621 def update (
620622 self ,
621- tree : Optional [" Tree" ] = None ,
622- parents : Optional [Sequence [" Commit" ]] = None ,
623+ tree : Optional [Tree ] = None ,
624+ parents : Optional [Sequence [Commit ]] = None ,
623625 message : Optional [bytes ] = None ,
624626 author : Optional [Signature ] = None ,
625627 recommit : bool = False ,
626- ) -> " Commit" :
628+ ) -> Commit :
627629 """Create a new commit with specific properties updated or replaced"""
628630 # Compute parameters used to create the new object.
629631 if tree is None :
@@ -683,7 +685,7 @@ class Mode(Enum):
683685 def is_file (self ) -> bool :
684686 return self in (Mode .REGULAR , Mode .EXEC )
685687
686- def comparable_to (self , other : " Mode" ) -> bool :
688+ def comparable_to (self , other : Mode ) -> bool :
687689 return self == other or (self .is_file () and other .is_file ())
688690
689691
@@ -706,7 +708,7 @@ def __init__(self, repo: Repository, mode: Mode, oid: Oid) -> None:
706708 self .mode = mode
707709 self .oid = oid
708710
709- def blob (self ) -> " Blob" :
711+ def blob (self ) -> Blob :
710712 """Get the data for this entry as a :class:`Blob`"""
711713 if self .mode in (Mode .REGULAR , Mode .EXEC ):
712714 return self .repo .get_blob (self .oid )
@@ -718,7 +720,7 @@ def symlink(self) -> bytes:
718720 return self .repo .get_blob (self .oid ).body
719721 return b"<non-symlink>"
720722
721- def tree (self ) -> " Tree" :
723+ def tree (self ) -> Tree :
722724 """Get the data for this entry as a :class:`Tree`"""
723725 if self .mode == Mode .DIR :
724726 return self .repo .get_tree (self .oid )
@@ -760,7 +762,7 @@ def _persist_deps(self) -> None:
760762 for entry in self .entries .values ():
761763 entry .persist ()
762764
763- def to_index (self , path : Path , skip_worktree : bool = False ) -> " Index" :
765+ def to_index (self , path : Path , skip_worktree : bool = False ) -> Index :
764766 """Read tree into a temporary index. If skip_workdir is ``True``, every
765767 entry in the index will have its "Skip Workdir" bit set."""
766768
0 commit comments