@@ -40,7 +40,7 @@ class Object(LazyMixin):
4040 Implements an Object which may be Blobs, Trees, Commits and Tags
4141 """
4242 TYPES = ("blob" , "tree" , "commit" , "tag" )
43- __slots__ = ("repo" , "id" , "size" )
43+ __slots__ = ("repo" , "id" , "size" , "_data_cached" )
4444 type = None # to be set by subclass
4545
4646 def __init__ (self , repo , id , size = None ):
@@ -61,6 +61,7 @@ def __init__(self, repo, id, size=None):
6161 self .repo = repo
6262 self .id = id
6363 self .size = size
64+ self ._data_cached = type (None )
6465
6566 def __bake__ (self ):
6667 """
@@ -103,6 +104,20 @@ def __repr__(self):
103104 """
104105 return '<git.%s "%s">' % (self .__class__ .__name__ , self .id )
105106
107+ @property
108+ def data (self ):
109+ """
110+ The binary contents of this object.
111+
112+ Returns
113+ str
114+
115+ NOTE
116+ The data will be cached after the first access.
117+ """
118+ self ._data_cached = ( self ._data_cached is not type (None ) and self ._data_cached ) or self .repo .git .cat_file (self .id , p = True , with_raw_output = True )
119+ return self ._data_cached
120+
106121 @classmethod
107122 def get_type_by_name (cls , object_type_name ):
108123 """
@@ -132,6 +147,45 @@ def get_type_by_name(cls, object_type_name):
132147 raise ValueError ("Cannot handle unknown object type: %s" % object_type_name )
133148
134149
150+ class IndexObject (Object ):
151+ """
152+ Base for all objects that can be part of the index file , namely Tree, Blob and
153+ SubModule objects
154+ """
155+ __slots__ = ("path" , "mode" )
156+
157+ def __init__ (self , repo , id , mode = None , path = None , size = None ):
158+ """
159+ Initialize a newly instanced IndexObject
160+ ``repo``
161+ is the Repo we are located in
162+
163+ ``id`` : string
164+ is the git object id as hex sha
165+
166+ ``mode`` : int
167+ is the file mode as int, use the stat module to evaluate the infomration
168+
169+ ``path`` : str
170+ is the path to the file in the file system, relative to the git repository root, i.e.
171+ file.ext or folder/other.ext
172+
173+ ``size`` : int
174+ size of the object data in bytes
175+ """
176+ super (IndexObject , self ).__init__ (repo , id , size )
177+ self .mode = mode
178+ self .path = path
179+
180+ @property
181+ def basename (self ):
182+ """
183+ Returns
184+ The basename of the IndexObject's file path
185+ """
186+ return os .path .basename (self .path )
187+
188+
135189class Ref (object ):
136190 """
137191 Represents a named reference to any object
0 commit comments