4
4
# This module is part of GitPython and is released under
5
5
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6
6
7
+ from contextlib import ExitStack
7
8
import datetime
8
9
import glob
9
10
from io import BytesIO
@@ -352,27 +353,22 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
352
353
353
354
# tmp file created in git home directory to be sure renaming
354
355
# works - /tmp/ dirs could be on another device
355
- tmp_index = tempfile . mktemp ( "" , "" , repo . git_dir )
356
- arg_list . append ( "--index-output=%s" % tmp_index )
357
- arg_list .extend ( treeish )
358
-
359
- # move current index out of the way - otherwise the merge may fail
360
- # as it considers existing entries. moving it essentially clears the index.
361
- # Unfortunately there is no 'soft' way to do it .
362
- # The TemporaryFileSwap assure the original file get put back
363
- if repo . git_dir :
364
- index_handler = TemporaryFileSwap ( join_path_native ( repo . git_dir , "index" ))
365
- try :
356
+ with ExitStack () as stack :
357
+ tmp_index = stack . enter_context ( tempfile . NamedTemporaryFile ( dir = repo . git_dir ) )
358
+ arg_list .append ( "--index-output=%s" % tmp_index . name )
359
+ arg_list . extend ( treeish )
360
+
361
+ # move current index out of the way - otherwise the merge may fail
362
+ # as it considers existing entries. moving it essentially clears the index .
363
+ # Unfortunately there is no 'soft' way to do it.
364
+ # The TemporaryFileSwap assure the original file get put back
365
+
366
+ stack . enter_context ( TemporaryFileSwap ( join_path_native ( repo . git_dir , "index" )))
366
367
repo .git .read_tree (* arg_list , ** kwargs )
367
- index = cls (repo , tmp_index )
368
+ index = cls (repo , tmp_index . name )
368
369
index .entries # force it to read the file as we will delete the temp-file
369
- del index_handler # release as soon as possible
370
- finally :
371
- if osp .exists (tmp_index ):
372
- os .remove (tmp_index )
373
- # END index merge handling
374
-
375
- return index
370
+ return index
371
+ # END index merge handling
376
372
377
373
# UTILITIES
378
374
@unbare_repo
@@ -1156,7 +1152,6 @@ def checkout(
1156
1152
unknown_lines = []
1157
1153
1158
1154
def handle_stderr (proc : "Popen[bytes]" , iter_checked_out_files : Iterable [PathLike ]) -> None :
1159
-
1160
1155
stderr_IO = proc .stderr
1161
1156
if not stderr_IO :
1162
1157
return None # return early if stderr empty
0 commit comments