diff --git a/git/index/base.py b/git/index/base.py index 1c56a219b..0f863b42c 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -351,7 +351,7 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile # tmp file created in git home directory to be sure renaming # works - /tmp/ dirs could be on another device - tmp_index = tempfile.mktemp("", "", repo.git_dir) + tmp_index = tempfile.NamedTemporaryFile(dir=repo.git_dir, delete=True).name arg_list.append("--index-output=%s" % tmp_index) arg_list.extend(treeish) diff --git a/git/index/util.py b/git/index/util.py index bfc7fadd6..b837706de 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -40,7 +40,8 @@ class TemporaryFileSwap(object): def __init__(self, file_path: PathLike) -> None: self.file_path = file_path - self.tmp_file_path = str(self.file_path) + tempfile.mktemp("", "", "") + self.tmp_file_path = str(self.file_path) + osp.basename(tempfile.mkstemp("", "", "")[1]) + # it may be that the source does not exist try: os.rename(self.file_path, self.tmp_file_path)