33#
44# This module is part of GitPython and is released under
55# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
7+ from git .exc import WorkTreeRepositoryUnsupported
68from git .util import LazyMixin , join_path_native , stream_copy , bin_to_hex
79
810import gitdb .typ as dbtyp
911import os .path as osp
10- from typing import Optional # noqa: F401 unused import
1112
1213from .util import get_object_type_by_name
1314
1415
15- _assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r"
16+ # typing ------------------------------------------------------------------
17+
18+ from typing import Any , TYPE_CHECKING , Optional , Union
19+
20+ from git .types import PathLike
21+
22+ if TYPE_CHECKING :
23+ from git .repo import Repo
24+ from gitdb .base import OStream
25+ from .tree import Tree
26+ from .blob import Blob
27+ from .tag import TagObject
28+ from .commit import Commit
29+
30+ # --------------------------------------------------------------------------
31+
32+
33+ _assertion_msg_format = "Created object %r whose python type %r disagrees with the acutual git object type %r"
1634
1735__all__ = ("Object" , "IndexObject" )
1836
@@ -27,7 +45,7 @@ class Object(LazyMixin):
2745 __slots__ = ("repo" , "binsha" , "size" )
2846 type = None # type: Optional[str] # to be set by subclass
2947
30- def __init__ (self , repo , binsha ):
48+ def __init__ (self , repo : 'Repo' , binsha : bytes ):
3149 """Initialize an object by identifying it by its binary sha.
3250 All keyword arguments will be set on demand if None.
3351
@@ -40,7 +58,7 @@ def __init__(self, repo, binsha):
4058 assert len (binsha ) == 20 , "Require 20 byte binary sha, got %r, len = %i" % (binsha , len (binsha ))
4159
4260 @classmethod
43- def new (cls , repo , id ): # @ReservedAssignment
61+ def new (cls , repo : 'Repo' , id ): # @ReservedAssignment
4462 """
4563 :return: New Object instance of a type appropriate to the object type behind
4664 id. The id of the newly created object will be a binsha even though
@@ -53,7 +71,7 @@ def new(cls, repo, id): # @ReservedAssignment
5371 return repo .rev_parse (str (id ))
5472
5573 @classmethod
56- def new_from_sha (cls , repo , sha1 ) :
74+ def new_from_sha (cls , repo : 'Repo' , sha1 : bytes ) -> Union [ 'Commit' , 'TagObject' , 'Tree' , 'Blob' ] :
5775 """
5876 :return: new object instance of a type appropriate to represent the given
5977 binary sha1
@@ -67,52 +85,52 @@ def new_from_sha(cls, repo, sha1):
6785 inst .size = oinfo .size
6886 return inst
6987
70- def _set_cache_ (self , attr ) :
88+ def _set_cache_ (self , attr : str ) -> None :
7189 """Retrieve object information"""
7290 if attr == "size" :
7391 oinfo = self .repo .odb .info (self .binsha )
74- self .size = oinfo .size
92+ self .size = oinfo .size # type: int
7593 # assert oinfo.type == self.type, _assertion_msg_format % (self.binsha, oinfo.type, self.type)
7694 else :
7795 super (Object , self )._set_cache_ (attr )
7896
79- def __eq__ (self , other ) :
97+ def __eq__ (self , other : Any ) -> bool :
8098 """:return: True if the objects have the same SHA1"""
8199 if not hasattr (other , 'binsha' ):
82100 return False
83101 return self .binsha == other .binsha
84102
85- def __ne__ (self , other ) :
103+ def __ne__ (self , other : Any ) -> bool :
86104 """:return: True if the objects do not have the same SHA1 """
87105 if not hasattr (other , 'binsha' ):
88106 return True
89107 return self .binsha != other .binsha
90108
91- def __hash__ (self ):
109+ def __hash__ (self ) -> int :
92110 """:return: Hash of our id allowing objects to be used in dicts and sets"""
93111 return hash (self .binsha )
94112
95- def __str__ (self ):
113+ def __str__ (self ) -> str :
96114 """:return: string of our SHA1 as understood by all git commands"""
97115 return self .hexsha
98116
99- def __repr__ (self ):
117+ def __repr__ (self ) -> str :
100118 """:return: string with pythonic representation of our object"""
101119 return '<git.%s "%s">' % (self .__class__ .__name__ , self .hexsha )
102120
103121 @property
104- def hexsha (self ):
122+ def hexsha (self ) -> str :
105123 """:return: 40 byte hex version of our 20 byte binary sha"""
106124 # b2a_hex produces bytes
107125 return bin_to_hex (self .binsha ).decode ('ascii' )
108126
109127 @property
110- def data_stream (self ):
128+ def data_stream (self ) -> 'OStream' :
111129 """ :return: File Object compatible stream to the uncompressed raw data of the object
112130 :note: returned streams must be read in order"""
113131 return self .repo .odb .stream (self .binsha )
114132
115- def stream_data (self , ostream ) :
133+ def stream_data (self , ostream : 'OStream' ) -> 'Object' :
116134 """Writes our data directly to the given output stream
117135 :param ostream: File object compatible stream object.
118136 :return: self"""
@@ -130,7 +148,9 @@ class IndexObject(Object):
130148 # for compatibility with iterable lists
131149 _id_attribute_ = 'path'
132150
133- def __init__ (self , repo , binsha , mode = None , path = None ):
151+ def __init__ (self ,
152+ repo : 'Repo' , binsha : bytes , mode : Union [None , int ] = None , path : Union [None , PathLike ] = None
153+ ) -> None :
134154 """Initialize a newly instanced IndexObject
135155
136156 :param repo: is the Repo we are located in
@@ -150,14 +170,14 @@ def __init__(self, repo, binsha, mode=None, path=None):
150170 if path is not None :
151171 self .path = path
152172
153- def __hash__ (self ):
173+ def __hash__ (self ) -> int :
154174 """
155175 :return:
156176 Hash of our path as index items are uniquely identifiable by path, not
157177 by their data !"""
158178 return hash (self .path )
159179
160- def _set_cache_ (self , attr ) :
180+ def _set_cache_ (self , attr : str ) -> None :
161181 if attr in IndexObject .__slots__ :
162182 # they cannot be retrieved lateron ( not without searching for them )
163183 raise AttributeError (
@@ -168,16 +188,19 @@ def _set_cache_(self, attr):
168188 # END handle slot attribute
169189
170190 @property
171- def name (self ):
191+ def name (self ) -> str :
172192 """:return: Name portion of the path, effectively being the basename"""
173193 return osp .basename (self .path )
174194
175195 @property
176- def abspath (self ):
196+ def abspath (self ) -> PathLike :
177197 """
178198 :return:
179199 Absolute path to this index object in the file system ( as opposed to the
180200 .path field which is a path relative to the git repository ).
181201
182202 The returned path will be native to the system and contains '\' on windows. """
183- return join_path_native (self .repo .working_tree_dir , self .path )
203+ if self .repo .working_tree_dir is not None :
204+ return join_path_native (self .repo .working_tree_dir , self .path )
205+ else :
206+ raise WorkTreeRepositoryUnsupported ("Working_tree_dir was None or empty" )
0 commit comments