Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc09a71e

Browse files
authored
Merge pull requestgitpython-developers#1606 from r-darwish/no-del
Don't rely on __del__
2 parents741edb5 +a3859ee commitc09a71e

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

‎git/index/base.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

7+
fromcontextlibimportExitStack
78
importdatetime
89
importglob
910
fromioimportBytesIO
@@ -352,27 +353,22 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
352353

353354
# tmp file created in git home directory to be sure renaming
354355
# 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 theindex.
361-
# Unfortunately there is no 'soft' way to do it.
362-
# The TemporaryFileSwap assure the original file get put back
363-
ifrepo.git_dir:
364-
index_handler=TemporaryFileSwap(join_path_native(repo.git_dir,"index"))
365-
try:
356+
withExitStack()asstack:
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 themerge 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")))
366367
repo.git.read_tree(*arg_list,**kwargs)
367-
index=cls(repo,tmp_index)
368+
index=cls(repo,tmp_index.name)
368369
index.entries# force it to read the file as we will delete the temp-file
369-
delindex_handler# release as soon as possible
370-
finally:
371-
ifosp.exists(tmp_index):
372-
os.remove(tmp_index)
373-
# END index merge handling
374-
375-
returnindex
370+
returnindex
371+
# END index merge handling
376372

377373
# UTILITIES
378374
@unbare_repo
@@ -1156,7 +1152,6 @@ def checkout(
11561152
unknown_lines= []
11571153

11581154
defhandle_stderr(proc:"Popen[bytes]",iter_checked_out_files:Iterable[PathLike])->None:
1159-
11601155
stderr_IO=proc.stderr
11611156
ifnotstderr_IO:
11621157
returnNone# return early if stderr empty

‎git/index/util.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importos
44
importstruct
55
importtempfile
6+
fromtypesimportTracebackType
67

78
fromgit.compatimportis_win
89

@@ -11,7 +12,7 @@
1112

1213
# typing ----------------------------------------------------------------------
1314

14-
fromtypingimportAny,Callable,TYPE_CHECKING
15+
fromtypingimportAny,Callable,TYPE_CHECKING,Optional,Type
1516

1617
fromgit.typesimportPathLike,_T
1718

@@ -47,12 +48,21 @@ def __init__(self, file_path: PathLike) -> None:
4748
exceptOSError:
4849
pass
4950

50-
def__del__(self)->None:
51+
def__enter__(self)->"TemporaryFileSwap":
52+
returnself
53+
54+
def__exit__(
55+
self,
56+
exc_type:Optional[Type[BaseException]],
57+
exc_val:Optional[BaseException],
58+
exc_tb:Optional[TracebackType],
59+
)->bool:
5160
ifosp.isfile(self.tmp_file_path):
5261
ifis_winandosp.exists(self.file_path):
5362
os.remove(self.file_path)
5463
os.rename(self.tmp_file_path,self.file_path)
55-
# END temp file exists
64+
65+
returnFalse
5666

5767

5868
# { Decorators

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp