Skip to content

Commit de5abb5

Browse files
philip-mcgrawgitster
authored andcommitted
git-p4: auto-delete named temporary file
Avoid double-open exceptions on Windows platform when calculating for lfs compressed size threshold (git-p4.largeFileCompressedThreshold) comparisons. Take new approach using the NamedTemporaryFile() file-like object as input to the ZipFile() which auto-deletes after implicit close leaving with scope. Original code had double-open exception on Windows platform because file still open from NamedTemporaryFile() using generated filename instead of object. Thanks to Andrey for patiently suggesting several iterations on this change for avoiding exceptions! Also print error details after resulting IOError to make debugging cause of exception less mysterious when it has nothing to do with "git version recent enough." Signed-off-by: Philip.McGraw <[email protected]> Reviewed-by: Andrey Mazo <[email protected]> Acked-by: Luke Diamand <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c90b652 commit de5abb5

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

git-p4.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,13 +1160,11 @@ def exceedsLargeFileThreshold(self, relPath, contents):
11601160
if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
11611161
return False
11621162
contentTempFile = self.generateTempFile(contents)
1163-
compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1164-
zf = zipfile.ZipFile(compressedContentFile.name, mode='w')
1165-
zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1166-
zf.close()
1167-
compressedContentsSize = zf.infolist()[0].compress_size
1163+
compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1164+
with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1165+
zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1166+
compressedContentsSize = zf.infolist()[0].compress_size
11681167
os.remove(contentTempFile)
1169-
os.remove(compressedContentFile.name)
11701168
if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
11711169
return True
11721170
return False
@@ -3525,8 +3523,9 @@ def importHeadRevision(self, revision):
35253523
self.updateOptionDict(details)
35263524
try:
35273525
self.commit(details, self.extractFilesFromCommit(details), self.branch)
3528-
except IOError:
3526+
except IOError as err:
35293527
print("IO error with git fast-import. Is your git version recent enough?")
3528+
print("IO error details: {}".format(err))
35303529
print(self.gitError.read())
35313530

35323531
def openStreams(self):

0 commit comments

Comments
 (0)