Skip to content

Commit a761768

Browse files
committed
Improve tools/line_endings.py to show counts and locations of mismatched line endings.
1 parent d2a7e64 commit a761768

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

tools/line_endings.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@ def check_line_endings(filename, print_errors=True):
2424

2525
has_dos_line_endings = False
2626
has_unix_line_endings = False
27+
dos_line_ending_example = ''
28+
dos_line_ending_count = 0
29+
unix_line_ending_example = ''
30+
unix_line_ending_count = 0
2731
if '\r\n' in data:
32+
dos_line_ending_example = data[max(0, data.find('\r\n') - 50):min(len(data), data.find('\r\n')+50)].replace('\r', '\\r').replace('\n', '\\n')
33+
dos_line_ending_count = data.count('\r\n')
2834
has_dos_line_endings = True
2935
data = data.replace('\r\n', 'A') # Replace all DOS line endings with some other character, and continue testing what's left.
3036
if '\n' in data:
37+
unix_line_ending_example = data[max(0, data.find('\n') - 50):min(len(data), data.find('\n')+50)].replace('\r', '\\r').replace('\n', '\\n')
38+
unix_line_ending_count = data.count('\n')
3139
has_unix_line_endings = True
3240
if '\r' in data:
33-
if print_errors: print >> sys.stderr, 'File \'' + filename + '\' contains OLD OSX line endings "\\r"'
41+
old_osx_line_ending_example = data[max(0, data.find('\r') - 50):min(len(data), data.find('\r')+50)].replace('\r', '\\r').replace('\n', '\\n')
42+
if print_errors:
43+
print >> sys.stderr, 'File \'' + filename + '\' contains OLD OSX line endings "\\r"'
44+
print >> sys.stderr, "Content around an OLD OSX line ending location: '" + old_osx_line_ending_example + "'"
3445
return 1 # Return a non-zero process exit code since we don't want to use the old OSX (9.x) line endings anywhere.
3546
if has_dos_line_endings and has_unix_line_endings:
36-
if print_errors: print >> sys.stderr, 'File \'' + filename + '\' contains both DOS "\\r\\n" and UNIX "\\n" line endings!'
47+
if print_errors:
48+
print >> sys.stderr, 'File \'' + filename + '\' contains both DOS "\\r\\n" and UNIX "\\n" line endings! (' + str(dos_line_ending_count) + ' DOS line endings, ' + str(unix_line_ending_count) + ' UNIX line endings)'
49+
print >> sys.stderr, "Content around a DOS line ending location: '" + dos_line_ending_example + "'"
50+
print >> sys.stderr, "Content around an UNIX line ending location: '" + unix_line_ending_example + "'"
3751
return 1 # Mixed line endings
3852
else: return 0
3953

0 commit comments

Comments
 (0)