Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: PRESUBMIT.py

Issue 43017: Add end of file newline checks to PRESUBMIT.py. (Closed)
Patch Set: Only check for \n if the file isn't empty. Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | PRESUBMIT_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Top-level presubmit script for Chromium. 6 """Top-level presubmit script for Chromium.
7 7
8 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for 8 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
9 details on the presubmit API built into gcl. 9 details on the presubmit API built into gcl.
10 """ 10 """
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 return (LocalChecks(input_api, output_api, max_cols=0) + 53 return (LocalChecks(input_api, output_api, max_cols=0) +
54 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)) 54 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
55 55
56 56
57 def LocalChecks(input_api, output_api, max_cols=80): 57 def LocalChecks(input_api, output_api, max_cols=80):
58 """Reports an error if for any source file in SOURCE_FILE_EXTENSIONS: 58 """Reports an error if for any source file in SOURCE_FILE_EXTENSIONS:
59 - uses CR (or CRLF) 59 - uses CR (or CRLF)
60 - contains a TAB 60 - contains a TAB
61 - has a line that ends with whitespace 61 - has a line that ends with whitespace
62 - contains a line >|max_cols| cols unless |max_cols| is 0. 62 - contains a line >|max_cols| cols unless |max_cols| is 0.
63 - File does not end in a newline, or ends in more than one.
63 64
64 Note that the whole file is checked, not only the changes. 65 Note that the whole file is checked, not only the changes.
65 """ 66 """
66 cr_files = [] 67 cr_files = []
68 eof_files = []
67 results = [] 69 results = []
68 excluded_paths = [input_api.re.compile(x) for x in EXCLUDED_PATHS] 70 excluded_paths = [input_api.re.compile(x) for x in EXCLUDED_PATHS]
69 files = input_api.AffectedFiles() 71 files = input_api.AffectedFiles()
70 for f in files: 72 for f in files:
71 path = f.LocalPath() 73 path = f.LocalPath()
72 root, ext = input_api.os_path.splitext(path) 74 root, ext = input_api.os_path.splitext(path)
73 # Look for unsupported extensions. 75 # Look for unsupported extensions.
74 if not ext in SOURCE_FILE_EXTENSIONS: 76 if not ext in SOURCE_FILE_EXTENSIONS:
75 continue 77 continue
76 # Look for excluded paths. 78 # Look for excluded paths.
77 found = False 79 found = False
78 for item in excluded_paths: 80 for item in excluded_paths:
79 if item.match(path): 81 if item.match(path):
80 found = True 82 found = True
81 break 83 break
82 if found: 84 if found:
83 continue 85 continue
84 86
85 # Need to read the file ourselves since AffectedFile.NewContents() 87 # Need to read the file ourselves since AffectedFile.NewContents()
86 # will normalize line endings. 88 # will normalize line endings.
87 contents = _ReadFile(path) 89 contents = _ReadFile(path)
88 if '\r' in contents: 90 if '\r' in contents:
89 cr_files.append(path) 91 cr_files.append(path)
90 92
93 # Check that the file ends in one and only one newline character.
94 if len(contents) > 0 and (contents[-1:] != "\n" or contents[-2:-1] == "\n"):
95 eof_files.append(path)
96
91 local_errors = [] 97 local_errors = []
92 # Remove EOL character. 98 # Remove EOL character.
93 lines = contents.splitlines() 99 lines = contents.splitlines()
94 line_num = 1 100 line_num = 1
95 for line in lines: 101 for line in lines:
96 if line.endswith(' '): 102 if line.endswith(' '):
97 local_errors.append(output_api.PresubmitError( 103 local_errors.append(output_api.PresubmitError(
98 '%s, line %s ends with whitespaces.' % 104 '%s, line %s ends with whitespaces.' %
99 (path, line_num))) 105 (path, line_num)))
100 # Accept lines with http:// to exceed the max_cols rule. 106 # Accept lines with http:// to exceed the max_cols rule.
(...skipping 10 matching lines...) Expand all
111 if len(local_errors) == 6: 117 if len(local_errors) == 6:
112 local_errors.pop() 118 local_errors.pop()
113 local_errors.append(output_api.PresubmitError("... and more.")) 119 local_errors.append(output_api.PresubmitError("... and more."))
114 break 120 break
115 results.extend(local_errors) 121 results.extend(local_errors)
116 122
117 if cr_files: 123 if cr_files:
118 results.append(output_api.PresubmitError( 124 results.append(output_api.PresubmitError(
119 'Found CR (or CRLF) line ending in these files, please use only LF:', 125 'Found CR (or CRLF) line ending in these files, please use only LF:',
120 items=cr_files)) 126 items=cr_files))
127 if eof_files:
128 results.append(output_api.PresubmitError(
129 'These files should end one (and only one) newline character:',
130 items=eof_files))
121 return results 131 return results
OLDNEW
« no previous file with comments | « no previous file | PRESUBMIT_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698