##// END OF EJS Templates
py3: wrap file object to write patch in native eol preserving byte-ness
Yuya Nishihara -
r36855:472c68cd default
parent child Browse files
Show More
@@ -1102,11 +1102,11 b' all lines of the hunk are removed, then '
1102 the hunk is left unchanged.
1102 the hunk is left unchanged.
1103 """)
1103 """)
1104 (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
1104 (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
1105 suffix=".diff", text=True)
1105 suffix=".diff")
1106 ncpatchfp = None
1106 ncpatchfp = None
1107 try:
1107 try:
1108 # Write the initial patch
1108 # Write the initial patch
1109 f = os.fdopen(patchfd, r"w")
1109 f = util.nativeeolwriter(os.fdopen(patchfd, r'wb'))
1110 chunk.header.write(f)
1110 chunk.header.write(f)
1111 chunk.write(f)
1111 chunk.write(f)
1112 f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
1112 f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
@@ -2426,6 +2426,22 b' bytecount = unitcountfn('
2426 (1, 1, _('%.0f bytes')),
2426 (1, 1, _('%.0f bytes')),
2427 )
2427 )
2428
2428
2429 class transformingwriter(object):
2430 """Writable file wrapper to transform data by function"""
2431
2432 def __init__(self, fp, encode):
2433 self._fp = fp
2434 self._encode = encode
2435
2436 def close(self):
2437 self._fp.close()
2438
2439 def flush(self):
2440 self._fp.flush()
2441
2442 def write(self, data):
2443 return self._fp.write(self._encode(data))
2444
2429 # Matches a single EOL which can either be a CRLF where repeated CR
2445 # Matches a single EOL which can either be a CRLF where repeated CR
2430 # are removed or a LF. We do not care about old Macintosh files, so a
2446 # are removed or a LF. We do not care about old Macintosh files, so a
2431 # stray CR is an error.
2447 # stray CR is an error.
@@ -2437,12 +2453,17 b' def tolf(s):'
2437 def tocrlf(s):
2453 def tocrlf(s):
2438 return _eolre.sub('\r\n', s)
2454 return _eolre.sub('\r\n', s)
2439
2455
2456 def _crlfwriter(fp):
2457 return transformingwriter(fp, tocrlf)
2458
2440 if pycompat.oslinesep == '\r\n':
2459 if pycompat.oslinesep == '\r\n':
2441 tonativeeol = tocrlf
2460 tonativeeol = tocrlf
2442 fromnativeeol = tolf
2461 fromnativeeol = tolf
2462 nativeeolwriter = _crlfwriter
2443 else:
2463 else:
2444 tonativeeol = pycompat.identity
2464 tonativeeol = pycompat.identity
2445 fromnativeeol = pycompat.identity
2465 fromnativeeol = pycompat.identity
2466 nativeeolwriter = pycompat.identity
2446
2467
2447 def escapestr(s):
2468 def escapestr(s):
2448 # call underlying function of s.encode('string_escape') directly for
2469 # call underlying function of s.encode('string_escape') directly for
General Comments 0
You need to be logged in to leave comments. Login now