@@ -57,7 +57,11 @@ class _TemporaryFileSwap(object):
5757 def __init__ (self , file_path ):
5858 self .file_path = file_path
5959 self .tmp_file_path = self .file_path + tempfile .mktemp ('' ,'' ,'' )
60- os .rename (self .file_path , self .tmp_file_path )
60+ # it may be that the source does not exist
61+ try :
62+ os .rename (self .file_path , self .tmp_file_path )
63+ except OSError :
64+ pass
6165
6266 def __del__ (self ):
6367 if os .path .isfile (self .tmp_file_path ):
@@ -556,6 +560,7 @@ def from_tree(cls, repo, *treeish, **kwargs):
556560 repo .git .read_tree (* arg_list , ** kwargs )
557561 index = cls (repo , tmp_index )
558562 index .entries # force it to read the file as we will delete the temp-file
563+ del (index_handler ) # release as soon as possible
559564 finally :
560565 if os .path .exists (tmp_index ):
561566 os .remove (tmp_index )
@@ -763,9 +768,6 @@ def write_tree(self, missing_ok=False):
763768 Returns
764769 Tree object representing this index
765770 """
766- index_path = self ._index_path ()
767- tmp_index_mover = _TemporaryFileSwap (index_path )
768-
769771 # IMPORTANT: If we have TREE extension data, it will actually
770772 # ignore the index and write the stored tree instead. Hence we
771773 # temporarily forget about it, and in fact I don't know what git
@@ -776,9 +778,14 @@ def write_tree(self, missing_ok=False):
776778 self ._extension_data = ''
777779 # END extension data special handling
778780
781+ index_path = self ._index_path ()
782+ tmp_index_mover = _TemporaryFileSwap (index_path )
783+
779784 self .write (index_path )
780785 tree_sha = self .repo .git .write_tree (missing_ok = missing_ok )
781786
787+ del (tmp_index_mover ) # as soon as possible
788+
782789 if stored_ext_data :
783790 self ._extension_data = stored_ext_data
784791 # END reset stored exstension data
0 commit comments