fixes · gitpython-developers/GitPython@a3859ee
@@ -353,26 +353,22 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
353353354354# tmp file created in git home directory to be sure renaming
355355# works - /tmp/ dirs could be on another device
356-tmp_index = tempfile.mktemp("", "", repo.git_dir)
357-arg_list.append("--index-output=%s" % tmp_index)
358-arg_list.extend(treeish)
359-360-# move current index out of the way - otherwise the merge may fail
361-# as it considers existing entries. moving it essentially clears the index.
362-# Unfortunately there is no 'soft' way to do it.
363-# The TemporaryFileSwap assure the original file get put back
364-try:
365-with ExitStack() as stack:
366-if repo.git_dir:
367-stack.enter_context(TemporaryFileSwap(join_path_native(repo.git_dir, "index")))
368-repo.git.read_tree(*arg_list, **kwargs)
369-index = cls(repo, tmp_index)
370-index.entries # force it to read the file as we will delete the temp-file
371-return index
372-finally:
373-if osp.exists(tmp_index):
374-os.remove(tmp_index)
375-# END index merge handling
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")))
367+repo.git.read_tree(*arg_list, **kwargs)
368+index = cls(repo, tmp_index.name)
369+index.entries # force it to read the file as we will delete the temp-file
370+return index
371+# END index merge handling
376372377373# UTILITIES
378374@unbare_repo