Skip to content

Commit 04de4f8

Browse files
committed
Using first line to determine EOL
1 parent 647959c commit 04de4f8

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pre_commit_hooks/end_of_file_fixer.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ def fix_file(file_obj: IO[bytes]) -> int:
2020
last_character = file_obj.read(1)
2121
# last_character will be '' for an empty file
2222
if last_character not in {LF, CR} and last_character != b'':
23-
# Check if file uses CRLF endings
23+
# Look at first line to determine line ending
2424
file_obj.seek(0, os.SEEK_SET)
25-
content = file_obj.read()
26-
ending = CRLF if CRLF in content else LF
25+
first_line = file_obj.readline()
26+
if CRLF in first_line:
27+
ending = CRLF
28+
elif CR in first_line:
29+
ending = CR
30+
else:
31+
ending = LF
2732
# Needs this seek for windows, otherwise IOError
2833
file_obj.seek(0, os.SEEK_END)
2934
file_obj.write(ending)

tests/end_of_file_fixer_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'),
2020
(b'foo\r\n', 0, b'foo\r\n'),
2121
(b'foo\r\nbar', 1, b'foo\r\nbar\r\n'),
22+
(b'foo\rbar', 1, b'foo\rbar\r'),
2223
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n'),
2324
(b'foo\r', 0, b'foo\r'),
2425
(b'foo\r\r\r\r', 1, b'foo\r'),

0 commit comments

Comments
 (0)