From f0bcfdd702019e5b54c49b847ea3fca15cbb75c4 Mon Sep 17 00:00:00 2001 From: Bruce Dawson Date: Fri, 21 May 2021 18:10:23 +0000 Subject: [PATCH] Optionally preserve line endings CheckForWindowsLineEndings is a Chromium presubmit to make sure that \r\n line endings (Windows line endings) don't get committed. Among other things these line endings mess up the license checks, leading to cryptic errors. The problem is that ChangedContents() was stripping line endings, making any checking for them futile. This change adds an option to preserve line endings, to be used by Chromium's CheckForWindowsLineEndings. It's not clear how long this has been broken. See also crrev.com/c/2911914 which must land after this. Bug: 801033 Change-Id: I7cdb9b3581788624f9eca081da43bb070ee412a1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2910488 Reviewed-by: Dirk Pranke Commit-Queue: Bruce Dawson --- presubmit_support.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index cf27cba9f..90cac72e8 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -1008,7 +1008,7 @@ class AffectedFile(object): pass # File not found? That's fine; maybe it was deleted. return self._cached_new_contents[:] - def ChangedContents(self): + def ChangedContents(self, keeplinebreaks=False): """Returns a list of tuples (line number, line text) of all new lines. This relies on the scm diff output describing each changed code section @@ -1016,20 +1016,27 @@ class AffectedFile(object): ^@@ , , @@$ """ - if self._cached_changed_contents is not None: + # Don't return cached results when line breaks are requested. + if not keeplinebreaks and self._cached_changed_contents is not None: return self._cached_changed_contents[:] - self._cached_changed_contents = [] + result = [] line_num = 0 - for line in self.GenerateScmDiff().splitlines(): + # The keeplinebreaks parameter to splitlines must be True or else the + # CheckForWindowsLineEndings presubmit will be a NOP. + for line in self.GenerateScmDiff().splitlines(keeplinebreaks): m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) if m: line_num = int(m.groups(1)[0]) continue if line.startswith('+') and not line.startswith('++'): - self._cached_changed_contents.append((line_num, line[1:])) + result.append((line_num, line[1:])) if not line.startswith('-'): line_num += 1 + # Don't cache results with line breaks. + if keeplinebreaks: + return result; + self._cached_changed_contents = result return self._cached_changed_contents[:] def __str__(self):