1515import os
1616import stat
1717from git .objects import Blob , Tree
18- from git .utils import SHA1Writer , LazyMixin
18+ from git .utils import SHA1Writer , LazyMixin , ConcurrentWriteOperation
1919from git .diff import Diffable
2020
2121class _TemporaryFileSwap (object ):
@@ -175,7 +175,7 @@ def __init__(self, repo, stream = None):
175175 def _set_cache_ (self , attr ):
176176 if attr == "entries" :
177177 # read the current index
178- fp = open (os . path . join ( self .repo . path , "index" ), "r" )
178+ fp = open (self ._index_path ( ), "r" )
179179 try :
180180 self ._read_from_stream (fp )
181181 finally :
@@ -184,6 +184,9 @@ def _set_cache_(self, attr):
184184 else :
185185 super (Index , self )._set_cache_ (attr )
186186
187+ def _index_path (self ):
188+ return os .path .join (self .repo .path , "index" )
189+
187190 @classmethod
188191 def _read_entry (cls , stream ):
189192 """Return: One entry of the given stream"""
@@ -306,19 +309,28 @@ def _write_cache_entry(cls, stream, entry):
306309 real_size = ((stream .tell () - beginoffset + 8 ) & ~ 7 )
307310 stream .write ("\0 " * ((beginoffset + real_size ) - stream .tell ()))
308311
309- def write (self , stream ):
312+ def write (self , stream = None ):
310313 """
311- Write the current state to the given stream
314+ Write the current state to the given stream or to the default repository
315+ index.
312316
313317 ``stream``
314- File-like object.
318+ File-like object or None.
319+ If None, the default repository index will be overwritten.
315320
316321 Returns
317322 self
318323
319324 Note
320325 Index writing based on the dulwich implementation
321326 """
327+ write_op = None
328+ if stream is None :
329+ write_op = ConcurrentWriteOperation (self ._index_path ())
330+ stream = write_op ._begin_writing ()
331+ # stream = open(self._index_path()
332+ # END stream handling
333+
322334 stream = SHA1Writer (stream )
323335
324336 # header
@@ -338,6 +350,9 @@ def write(self, stream):
338350 # write the sha over the content
339351 stream .write_sha ()
340352
353+ if write_op is not None :
354+ write_op ._end_writing ()
355+
341356
342357 @classmethod
343358 def from_tree (cls , repo , * treeish , ** kwargs ):
@@ -504,7 +519,7 @@ def write_tree(self):
504519 Returns
505520 Tree object representing this index
506521 """
507- index_path = os . path . join ( self .repo . path , "index" )
522+ index_path = self ._index_path ( )
508523 tmp_index_mover = _TemporaryFileSwap (index_path )
509524
510525 self .to_file (self , index_path )
0 commit comments