1515import os
1616import stat
1717from git .objects import Blob , Tree
18- from git .utils import SHA1Writer
18+ from git .utils import SHA1Writer , LazyMixin
19+ from git .diff import Diffable
1920
2021class _TemporaryFileSwap (object ):
2122 """
@@ -139,7 +140,7 @@ def from_blob(cls, blob):
139140 return IndexEntry ((time , time , 0 , 0 , blob .mode , 0 , 0 , blob .size , blob .id , 0 , blob .path ))
140141
141142
142- class Index (object ):
143+ class Index (LazyMixin ):
143144 """
144145 Implements an Index that can be manipulated using a native implementation in
145146 order to save git command function calls wherever possible.
@@ -160,13 +161,28 @@ class Index(object):
160161 def __init__ (self , repo , stream = None ):
161162 """
162163 Initialize this Index instance, optionally from the given ``stream``
164+
165+ If a stream is not given, the stream will be initialized from the current
166+ repository's index on demand.
163167 """
164168 self .repo = repo
165- self .entries = dict ()
166169 self .version = self ._VERSION
167170 self ._extension_data = ''
168171 if stream is not None :
169172 self ._read_from_stream (stream )
173+ # END read from stream immediatly
174+
175+ def _set_cache_ (self , attr ):
176+ if attr == "entries" :
177+ # read the current index
178+ fp = open (os .path .join (self .repo .path , "index" ), "r" )
179+ try :
180+ self ._read_from_stream (fp )
181+ finally :
182+ fp .close ()
183+ # END read from default index on demand
184+ else :
185+ super (Index , self )._set_cache_ (attr )
170186
171187 @classmethod
172188 def _read_entry (cls , stream ):
@@ -197,13 +213,10 @@ def _read_header(cls, stream):
197213 def _read_from_stream (self , stream ):
198214 """
199215 Initialize this instance with index values read from the given stream
200-
201- Note
202- We explicitly do not clear the entries dict here to allow for reading
203- multiple chunks from multiple streams into the same Index instance
204216 """
205217 self .version , num_entries = self ._read_header (stream )
206218 count = 0
219+ self .entries = dict ()
207220 while count < num_entries :
208221 entry = self ._read_entry (stream )
209222 self .entries [(entry .path , entry .stage )] = entry
@@ -298,7 +311,7 @@ def write(self, stream):
298311 Write the current state to the given stream
299312
300313 ``stream``
301- File-like object
314+ File-like object.
302315
303316 Returns
304317 self
0 commit comments