@@ -57,7 +57,11 @@ class _TemporaryFileSwap(object):
57
57
def __init__ (self , file_path ):
58
58
self .file_path = file_path
59
59
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
61
65
62
66
def __del__ (self ):
63
67
if os .path .isfile (self .tmp_file_path ):
@@ -556,6 +560,7 @@ def from_tree(cls, repo, *treeish, **kwargs):
556
560
repo .git .read_tree (* arg_list , ** kwargs )
557
561
index = cls (repo , tmp_index )
558
562
index .entries # force it to read the file as we will delete the temp-file
563
+ del (index_handler ) # release as soon as possible
559
564
finally :
560
565
if os .path .exists (tmp_index ):
561
566
os .remove (tmp_index )
@@ -763,9 +768,6 @@ def write_tree(self, missing_ok=False):
763
768
Returns
764
769
Tree object representing this index
765
770
"""
766
- index_path = self ._index_path ()
767
- tmp_index_mover = _TemporaryFileSwap (index_path )
768
-
769
771
# IMPORTANT: If we have TREE extension data, it will actually
770
772
# ignore the index and write the stored tree instead. Hence we
771
773
# temporarily forget about it, and in fact I don't know what git
@@ -776,9 +778,14 @@ def write_tree(self, missing_ok=False):
776
778
self ._extension_data = ''
777
779
# END extension data special handling
778
780
781
+ index_path = self ._index_path ()
782
+ tmp_index_mover = _TemporaryFileSwap (index_path )
783
+
779
784
self .write (index_path )
780
785
tree_sha = self .repo .git .write_tree (missing_ok = missing_ok )
781
786
787
+ del (tmp_index_mover ) # as soon as possible
788
+
782
789
if stored_ext_data :
783
790
self ._extension_data = stored_ext_data
784
791
# END reset stored exstension data
0 commit comments