Skip to content

Commit abd3d0e

Browse files
committed
Fix CRLF lines
1 parent e01bc2c commit abd3d0e

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pre_commit_hooks/end_of_file_fixer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def fix_file(file_obj):
2121
file_obj.write(b'\n')
2222
return 1
2323

24-
while last_character == b'\n':
24+
while last_character == b'\n' or last_character == 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
@@ -39,8 +39,9 @@ def fix_file(file_obj):
3939
# there are extraneous newlines at the ned of the file. Then backtrack and
4040
# trim the end off.
4141
if len(file_obj.read(2)) == 2:
42-
file_obj.seek(-1, os.SEEK_CUR)
42+
file_obj.seek(-2, os.SEEK_CUR)
4343
file_obj.truncate()
44+
file_obj.write(b'\n')
4445
return 1
4546

4647
return 0

tests/end_of_file_fixer_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
(b'foo', 1, b'foo\n'),
1616
(b'foo\n\n\n', 1, b'foo\n'),
1717
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'),
18+
(b'foo\r\n', 1, b'foo\n'),
19+
(b'foo\r\n\r\n', 1, b'foo\n'),
1820
)
1921

2022

0 commit comments

Comments
 (0)