From b639aca53effcba6978bd37c829745b6a84699cf Mon Sep 17 00:00:00 2001 From: mostynb Date: Wed, 7 Jan 2015 12:31:23 -0800 Subject: add a presubmit check to avoid adding files with windows line endings BUG=420796,385395 Review URL: https://codereview.chromium.org/786953005 Cr-Commit-Position: refs/heads/master@{#310353} --- PRESUBMIT.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'PRESUBMIT.py') diff --git a/PRESUBMIT.py b/PRESUBMIT.py index 7c0b3fb..08b98c1 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -1403,6 +1403,7 @@ def _CommonChecks(input_api, output_api): results.extend(_CheckParseErrors(input_api, output_api)) results.extend(_CheckForIPCRules(input_api, output_api)) results.extend(_CheckForCopyrightedCode(input_api, output_api)) + results.extend(_CheckForWindowsLineEndings(input_api, output_api)) if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): results.extend(input_api.canned_checks.RunUnitTestsInDirectory( @@ -1588,6 +1589,40 @@ def _CheckForIPCRules(input_api, output_api): return [] +def _CheckForWindowsLineEndings(input_api, output_api): + """Check source code and known ascii text files for Windows style line + endings. + """ + known_text_files = r'.*\.(txt|html|htm|mhtml|py)$' + + file_inclusion_pattern = ( + known_text_files, + r'.+%s' % _IMPLEMENTATION_EXTENSIONS + ) + + filter = lambda f: input_api.FilterSourceFile( + f, white_list=file_inclusion_pattern, black_list=None) + files = [f.LocalPath() for f in + input_api.AffectedSourceFiles(filter)] + + problems = [] + + for file in files: + fp = open(file, 'r') + for line in fp: + if line.endswith('\r\n'): + problems.append(file) + break + fp.close() + + if problems: + return [output_api.PresubmitPromptWarning('Are you sure that you want ' + 'these files to contain Windows style line endings?\n' + + '\n'.join(problems))] + + return [] + + def CheckChangeOnUpload(input_api, output_api): results = [] results.extend(_CommonChecks(input_api, output_api)) -- cgit v1.1