From 00790d319fb17066fdd85238e93d42ba4c2f18f2 Mon Sep 17 00:00:00 2001 From: Bruce Dawson Date: Tue, 19 Apr 2022 19:35:46 +0000 Subject: [PATCH] Fix \r\r\n line endings in presubmit output When running "git cl presubmit --all" some of the presubmit messages were ending up double-spaced if the output is sent to a file and then loaded into notepad or Visual Studio. Visual Studio would complain about inconsistent line endings, and it turns out that this was all caused by some lines having \r\r\n line endings - notepad and Visual Studio interpret that as two line endings. The problem is that the stream that WinUnicodeOutput writes to does \n to \r\n translation. If the text being printed already has \r\n line endings then we get doubled-up \r characters. The fix is to replace all \r\n sequences with \n before calling write. This reduces the line count of the output by almost 2300 lines (more than 25% of the total) and makes it much more readable. Bug: 1309977 Change-Id: Ie5475087badc3d3146e4f2ba41d30c9817dd375a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3589498 Reviewed-by: Aravind Vasudevan Commit-Queue: Bruce Dawson --- fix_encoding.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fix_encoding.py b/fix_encoding.py index 1d84750e7..b14494f39 100644 --- a/fix_encoding.py +++ b/fix_encoding.py @@ -276,6 +276,11 @@ class WinUnicodeOutput(WinUnicodeOutputBase): if sys.version_info.major == 3 and isinstance(text, bytes): # Replace characters that cannot be printed instead of failing. text = text.decode(self.encoding, 'replace') + # When redirecting to a file or process any \n characters will be replaced + # with \r\n. If the text to be printed already has \r\n line endings then + # \r\r\n line endings will be generated, leading to double-spacing of some + # output. Normalizing line endings to \n avoids this problem. + text = text.replace('\r\n', '\n') self._stream.write(text) except Exception as e: complain('%s.write: %r' % (self.name, e))