From eb76f193f37363a5644a9079733f2ef01a1cb1cb Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Tue, 23 Jan 2024 01:55:14 +0900 Subject: [PATCH] Implement Newline Check in the Presubmit This will prevent committing source files with CRLF newlines. Bug: None Change-Id: I43c1d9a192a445a27f75b336e9ff6e45e012866b Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/335760 Auto-Submit: Daniel.L (Byoungchan) Lee Reviewed-by: Mirko Bonadei Commit-Queue: Mirko Bonadei Cr-Commit-Position: refs/heads/main@{#41598} --- PRESUBMIT.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/PRESUBMIT.py b/PRESUBMIT.py index e5f28b70d1..5c9c88dc8b 100755 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -1067,6 +1067,8 @@ def CommonChecks(input_api, output_api): results.extend( CheckNewlineAtTheEndOfProtoFiles( input_api, output_api, source_file_filter=non_third_party_sources)) + results.extend( + CheckLFNewline(input_api, output_api, non_third_party_sources)) results.extend( CheckNoStreamUsageIsAdded(input_api, output_api, non_third_party_sources)) @@ -1322,6 +1324,20 @@ def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api, return results +def CheckLFNewline(input_api, output_api, source_file_filter): + """Checks that all files have LF newlines.""" + error_msg = 'File {} must use LF newlines.' + results = [] + file_filter = lambda x: input_api.FilterSourceFile( + x, files_to_check=(r'.+', )) and source_file_filter(x) + for f in input_api.AffectedSourceFiles(file_filter): + file_path = f.LocalPath() + with open(file_path, 'rb') as f: + if b'\r\n' in f.read(): + results.append( + output_api.PresubmitError(error_msg.format(file_path))) + return results + def _ExtractAddRulesFromParsedDeps(parsed_deps): """Extract the rules that add dependencies from a parsed DEPS file.