Skip to content

Commit 7f0e596

Browse files
Pete Wyckoffgitster
authored andcommitted
git p4: scrub crlf for utf16 files on windows
Files of type utf16 are handled with "p4 print" instead of the normal "p4 -G print" interface due to how the latter does not produce correct output. See 55aa571 (git-p4: handle utf16 filetype properly, 2011-09-17) for details. On windows, though, "p4 print" can not be told which line endings to use, as there is no underlying client, and always chooses crlf, even for utf16 files. Convert the \r\n into \n when importing utf16 files. The fix for this is complex, in that the problem is a property of the NT version of p4. There are old versions of p4 that were compiled directly for cygwin that should not be subjected to text replacement. The right check here, then, is to look at the p4 version, not the OS version. Note also that on cygwin, platform.system() is "CYGWIN_NT-5.1" or similar, not "Windows". Add a function to memoize the p4 version string and use it to check for "/NT", indicating the Windows build of p4. Signed-off-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bb5ea62 commit 7f0e596

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

git-p4.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ def p4_system(cmd):
170170
expand = isinstance(real_cmd, basestring)
171171
subprocess.check_call(real_cmd, shell=expand)
172172

173+
_p4_version_string = None
174+
def p4_version_string():
175+
"""Read the version string, showing just the last line, which
176+
hopefully is the interesting version bit.
177+
178+
$ p4 -V
179+
Perforce - The Fast Software Configuration Management System.
180+
Copyright 1995-2011 Perforce Software. All rights reserved.
181+
Rev. P4/NTX86/2011.1/393975 (2011/12/16).
182+
"""
183+
global _p4_version_string
184+
if not _p4_version_string:
185+
a = p4_read_pipe_lines(["-V"])
186+
_p4_version_string = a[-1].rstrip()
187+
return _p4_version_string
188+
173189
def p4_integrate(src, dest):
174190
p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
175191

@@ -1973,7 +1989,6 @@ def __init__(self):
19731989
self.syncWithOrigin = True
19741990
self.importIntoRemotes = True
19751991
self.maxChanges = ""
1976-
self.isWindows = (platform.system() == "Windows")
19771992
self.keepRepoPath = False
19781993
self.depotPaths = None
19791994
self.p4BranchesInGit = []
@@ -2118,7 +2133,14 @@ def streamOneP4File(self, file, contents):
21182133
# operations. utf16 is converted to ascii or utf8, perhaps.
21192134
# But ascii text saved as -t utf16 is completely mangled.
21202135
# Invoke print -o to get the real contents.
2136+
#
2137+
# On windows, the newlines will always be mangled by print, so put
2138+
# them back too. This is not needed to the cygwin windows version,
2139+
# just the native "NT" type.
2140+
#
21212141
text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']])
2142+
if p4_version_string().find("/NT") >= 0:
2143+
text = text.replace("\r\n", "\n")
21222144
contents = [ text ]
21232145

21242146
if type_base == "apple":

0 commit comments

Comments
 (0)