Skip to content

Commit e694a6c

Browse files
committed
Incorporate patch to support isolated CR
1 parent 2ab5832 commit e694a6c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pre_commit_hooks/end_of_file_fixer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ def fix_file(file_obj):
1515
return 0
1616
last_character = file_obj.read(1)
1717
# last_character will be '' for an empty file
18-
if last_character != b'\n' and last_character != b'':
18+
if last_character not in {b'\n', b'\r'} and last_character != b'':
1919
# Needs this seek for windows, otherwise IOError
2020
file_obj.seek(0, os.SEEK_END)
2121
file_obj.write(b'\n')
2222
return 1
2323

24-
while last_character == b'\n' or last_character == b'\r':
24+
while last_character in {b'\n', b'\r'}:
2525
# Deal with the beginning of the file
2626
if file_obj.tell() == 1:
2727
# If we've reached the beginning of the file and it is all
@@ -38,8 +38,10 @@ def fix_file(file_obj):
3838
# newlines. If we find extraneous newlines, then backtrack and trim them.
3939
position = file_obj.tell()
4040
remaining = file_obj.read()
41-
for sequence in [b'\n', b'\r\n']:
42-
if remaining.startswith(sequence) and len(remaining) > len(sequence):
41+
for sequence in (b'\n', b'\r\n', b'\r'):
42+
if remaining == sequence:
43+
return 0
44+
elif remaining.startswith(sequence):
4345
file_obj.seek(position + len(sequence))
4446
file_obj.truncate()
4547
return 1

tests/end_of_file_fixer_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'),
1818
(b'foo\r\n', 0, b'foo\r\n'),
1919
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n'),
20+
(b'foo\r', 0, b'foo\r'),
21+
(b'foo\r\r\r\r', 1, b'foo\r'),
2022
)
2123

2224

0 commit comments

Comments
 (0)