From abd3d0ec3f91848f021bf75bff797803e837d09b Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Sat, 13 Oct 2018 07:26:53 -0400 Subject: [PATCH 1/3] Fix CRLF lines --- pre_commit_hooks/end_of_file_fixer.py | 5 +++-- tests/end_of_file_fixer_test.py | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pre_commit_hooks/end_of_file_fixer.py b/pre_commit_hooks/end_of_file_fixer.py index 4fe82b7..9b94ab3 100644 --- a/pre_commit_hooks/end_of_file_fixer.py +++ b/pre_commit_hooks/end_of_file_fixer.py @@ -21,7 +21,7 @@ def fix_file(file_obj): file_obj.write(b'\n') return 1 - while last_character == b'\n': + while last_character == b'\n' or last_character == b'\r': # Deal with the beginning of the file if file_obj.tell() == 1: # If we've reached the beginning of the file and it is all @@ -39,8 +39,9 @@ def fix_file(file_obj): # there are extraneous newlines at the ned of the file. Then backtrack and # trim the end off. if len(file_obj.read(2)) == 2: - file_obj.seek(-1, os.SEEK_CUR) + file_obj.seek(-2, os.SEEK_CUR) file_obj.truncate() + file_obj.write(b'\n') return 1 return 0 diff --git a/tests/end_of_file_fixer_test.py b/tests/end_of_file_fixer_test.py index ae899d2..0766636 100644 --- a/tests/end_of_file_fixer_test.py +++ b/tests/end_of_file_fixer_test.py @@ -15,6 +15,8 @@ TESTS = ( (b'foo', 1, b'foo\n'), (b'foo\n\n\n', 1, b'foo\n'), (b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'), + (b'foo\r\n', 1, b'foo\n'), + (b'foo\r\n\r\n', 1, b'foo\n'), ) From 2ab5832ce1914a85155c334c56a6946881e9dfac Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Sat, 13 Oct 2018 19:00:22 -0400 Subject: [PATCH 2/3] Preserve CRLF if file already ends that way --- pre_commit_hooks/end_of_file_fixer.py | 16 ++++++++-------- tests/end_of_file_fixer_test.py | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pre_commit_hooks/end_of_file_fixer.py b/pre_commit_hooks/end_of_file_fixer.py index 9b94ab3..37ebbb5 100644 --- a/pre_commit_hooks/end_of_file_fixer.py +++ b/pre_commit_hooks/end_of_file_fixer.py @@ -35,14 +35,14 @@ def fix_file(file_obj): last_character = file_obj.read(1) # Our current position is at the end of the file just before any amount of - # newlines. If we read two characters and get two newlines back we know - # there are extraneous newlines at the ned of the file. Then backtrack and - # trim the end off. - if len(file_obj.read(2)) == 2: - file_obj.seek(-2, os.SEEK_CUR) - file_obj.truncate() - file_obj.write(b'\n') - return 1 + # newlines. If we find extraneous newlines, then backtrack and trim them. + position = file_obj.tell() + remaining = file_obj.read() + for sequence in [b'\n', b'\r\n']: + if remaining.startswith(sequence) and len(remaining) > len(sequence): + file_obj.seek(position + len(sequence)) + file_obj.truncate() + return 1 return 0 diff --git a/tests/end_of_file_fixer_test.py b/tests/end_of_file_fixer_test.py index 0766636..deedeab 100644 --- a/tests/end_of_file_fixer_test.py +++ b/tests/end_of_file_fixer_test.py @@ -15,8 +15,8 @@ TESTS = ( (b'foo', 1, b'foo\n'), (b'foo\n\n\n', 1, b'foo\n'), (b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'), - (b'foo\r\n', 1, b'foo\n'), - (b'foo\r\n\r\n', 1, b'foo\n'), + (b'foo\r\n', 0, b'foo\r\n'), + (b'foo\r\n\r\n\r\n', 1, b'foo\r\n'), ) From e694a6cfc2c04f0724846b3158e1bd20e7ce050c Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Sat, 13 Oct 2018 19:44:02 -0400 Subject: [PATCH 3/3] Incorporate patch to support isolated CR --- pre_commit_hooks/end_of_file_fixer.py | 10 ++++++---- tests/end_of_file_fixer_test.py | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pre_commit_hooks/end_of_file_fixer.py b/pre_commit_hooks/end_of_file_fixer.py index 37ebbb5..5ab1b7b 100644 --- a/pre_commit_hooks/end_of_file_fixer.py +++ b/pre_commit_hooks/end_of_file_fixer.py @@ -15,13 +15,13 @@ def fix_file(file_obj): return 0 last_character = file_obj.read(1) # last_character will be '' for an empty file - if last_character != b'\n' and last_character != b'': + if last_character not in {b'\n', b'\r'} and last_character != b'': # Needs this seek for windows, otherwise IOError file_obj.seek(0, os.SEEK_END) file_obj.write(b'\n') return 1 - while last_character == b'\n' or last_character == b'\r': + while last_character in {b'\n', b'\r'}: # Deal with the beginning of the file if file_obj.tell() == 1: # If we've reached the beginning of the file and it is all @@ -38,8 +38,10 @@ def fix_file(file_obj): # newlines. If we find extraneous newlines, then backtrack and trim them. position = file_obj.tell() remaining = file_obj.read() - for sequence in [b'\n', b'\r\n']: - if remaining.startswith(sequence) and len(remaining) > len(sequence): + for sequence in (b'\n', b'\r\n', b'\r'): + if remaining == sequence: + return 0 + elif remaining.startswith(sequence): file_obj.seek(position + len(sequence)) file_obj.truncate() return 1 diff --git a/tests/end_of_file_fixer_test.py b/tests/end_of_file_fixer_test.py index deedeab..f8710af 100644 --- a/tests/end_of_file_fixer_test.py +++ b/tests/end_of_file_fixer_test.py @@ -17,6 +17,8 @@ TESTS = ( (b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'), (b'foo\r\n', 0, b'foo\r\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'), )