◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
35 changes: 15 additions & 20 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

import datetime
import glob
from io import BytesIO
Expand Down Expand Up @@ -352,27 +353,22 @@ 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)
arg_list.append("--index-output=%s" % tmp_index)
arg_list.extend(treeish)

# move current index out of the way - otherwise the merge may fail
# as it considers existing entries. moving it essentially clears the index.
# Unfortunately there is no 'soft' way to do it.
# The TemporaryFileSwap assure the original file get put back
if repo.git_dir:
index_handler = TemporaryFileSwap(join_path_native(repo.git_dir, "index"))
try:
repo.git.read_tree(*arg_list, **kwargs)
index = cls(repo, tmp_index)
index.entries # force it to read the file as we will delete the temp-file
del index_handler # release as soon as possible
finally:
if osp.exists(tmp_index):
os.remove(tmp_index)
# END index merge handling

return index

# UTILITIES
@unbare_repo
Expand Down Expand Up @@ -1156,7 +1152,6 @@ def checkout(
unknown_lines = []

def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]) -> None:

stderr_IO = proc.stderr
if not stderr_IO:
return None # return early if stderr empty
Expand Down
16 changes: 13 additions & 3 deletions git/index/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import struct
import tempfile

from git.compat import is_win

Expand All @@ -11,7 +12,7 @@

# typing ----------------------------------------------------------------------

from typing import Any, Callable, TYPE_CHECKING

from git.types import PathLike, _T

Expand Down @@ -47,12 +48,21 @@ def __init__(self, file_path: PathLike) -> None:
except OSError:
pass

def __del__(self) -> None:
if osp.isfile(self.tmp_file_path):
if is_win and osp.exists(self.file_path):
os.remove(self.file_path)
os.rename(self.tmp_file_path, self.file_path)
# END temp file exists


# { Decorators
Expand Down
Toggle all file notes Toggle all file annotations