Skip to content
This repository has been archived by the owner on Jan 22, 2018. It is now read-only.

Fix utf16 encoded files handling on Windows #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions git-p4
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
#

import optparse, sys, os, marshal, subprocess, shlex
import optparse, sys, os, marshal, subprocess, shlex, stat
import tempfile, os.path, time, platform
import re
import cStringIO
Expand Down Expand Up @@ -780,10 +780,22 @@ class P4FileReader:

if header['type'].startswith('utf16'):
# Don't even try to convert utf16. Ask p4 to write the file directly.
tmpFile = tempfile.NamedTemporaryFile()
P4Helper().p4_system("print -o \"%s\" \"%s\"" % (tmpFile.name, escapeStringP4(header['depotFile'])))
text = open(tmpFile.name).read()
tmpFile.close()

# Create temporary file and disable automatic removal to be able to close it.
# Closing is necessary because the p4 need to write some data into this file
# but Windows-based systems can not open an already opened temporary file
# created by NamedTemporaryFile function.
tmpFile = tempfile.NamedTemporaryFile(delete=False)
try:
tmpFile.close()
P4Helper().p4_system("print -o \"%s\" \"%s\"" % (tmpFile.name, escapeStringP4(header['depotFile'])))
text = open(tmpFile.name).read()
finally:
# The temporary file may become read-only after `p4 print`,
# but os.remove requires write access.
if not os.access(tmpFile.name, os.W_OK):
os.chmod(tmpFile.name, stat.S_IWUSR)
os.remove(tmpFile.name)
else:
text = textBuffer.getvalue()
textBuffer.close()
Expand Down