diff --git a/pre_commit_hooks/end_of_file_fixer.py b/pre_commit_hooks/end_of_file_fixer.py index 6bb452e..2f397eb 100644 --- a/pre_commit_hooks/end_of_file_fixer.py +++ b/pre_commit_hooks/end_of_file_fixer.py @@ -20,13 +20,10 @@ def fix_file(file_obj: IO[bytes]) -> int: last_character = file_obj.read(1) # last_character will be '' for an empty file if last_character not in {LF, CR} and last_character != b'': - # Check for consistent CRLF usage + # Check if file uses CRLF endings file_obj.seek(0, os.SEEK_SET) content = file_obj.read() - lf_count = content.count(LF) - crlf_count = content.count(CRLF) - # Use CRLF only if all line endings are CRLF - ending = CRLF if crlf_count > 0 and crlf_count == lf_count else LF + ending = CRLF if CRLF in content else LF # Needs this seek for windows, otherwise IOError file_obj.seek(0, os.SEEK_END) file_obj.write(ending) diff --git a/tests/end_of_file_fixer_test.py b/tests/end_of_file_fixer_test.py index 00544d8..05fdade 100644 --- a/tests/end_of_file_fixer_test.py +++ b/tests/end_of_file_fixer_test.py @@ -19,7 +19,6 @@ TESTS = ( (b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'), (b'foo\r\n', 0, b'foo\r\n'), (b'foo\r\nbar', 1, b'foo\r\nbar\r\n'), - (b'foo\nbar\r\nbaz', 1, b'foo\nbar\r\nbaz\n'), (b'foo\r\n\r\n\r\n', 1, b'foo\r\n'), (b'foo\r', 0, b'foo\r'), (b'foo\r\r\r\r', 1, b'foo\r'),