Skip to content

Commit b3c1810

Browse files
committed
Let subprocess write temp file on Windows
On Windows, a file created by NamedTemporaryFile cannot be reopened while already open, under most circumstances. This applies to attempts to open it both from the original process and from other processes. This differs from the behavior on other operating systems, and it works this way to help ensure the file can be automatically deleted (since having a file open on Windows usually prevents the file from being deleted, unlike on other OSes). However, this can cause problems when NamedTemporaryFile is used to produce a safe filename for opening with an external process, as IndexFile.from_tree does. On Windows, the git subprocess can't open and write to the file. This breaks IndexFile.from_tree on Windows, causing gitpython-developers#1630 (because IndexFile.reset uses IndexFile.from_tree, and the Repo.index property returns an IndexFile instance, so Repo.index.reset fails), and causes a number of other breakages. This commit is an initial fix for the problem, but likely not the best fix. On Windows, it closes the temporary file created by NamedTemporaryFile, so the file can be opened by name, including by a git subprocess. It is less than ideal because it at least appears to create a race condition: without furher changes not yet made here, closing the file actually deletes it, which conveniently lets the name be reused, but because the file does not exist, the filename might end up being reused before it is recreated. The following 8 tests go from failing to passing due to this change on Windows (as listed in the summary at the end of pytest output): - test/test_docs.py:210 Tutorials.test_references_and_objects - test/test_fun.py:37 TestFun.test_aggressive_tree_merge - test/test_index.py:781 TestIndex.test_compare_write_tree - test/test_index.py:294 TestIndex.test_index_file_diffing - test/test_index.py:182 TestIndex.test_index_file_from_tree - test/test_index.py:232 TestIndex.test_index_merge_tree - test/test_index.py:428 TestIndex.test_index_mutation - test/test_refs.py:218 TestRefs.test_head_reset
1 parent 74cc671 commit b3c1810

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

Diff for: git/index/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
362362
# works - /tmp/ dirs could be on another device.
363363
with ExitStack() as stack:
364364
tmp_index = stack.enter_context(tempfile.NamedTemporaryFile(dir=repo.git_dir))
365+
if os.name == "nt":
366+
tmp_index.close()
365367
arg_list.append("--index-output=%s" % tmp_index.name)
366368
arg_list.extend(treeish)
367369

0 commit comments

Comments
 (0)