Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #326: Handle CRLF in end-of-file-fixer #327

Merged
merged 3 commits into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pre_commit_hooks/end_of_file_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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
Expand Down
2 changes: 2 additions & 0 deletions tests/end_of_file_fixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
(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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, it would be better if it kept the \r\n at the end -- is it difficult to adjust the logic to do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can probably do something like:

if eof.count(b'\r') == len(eof):
    # append b'\r' and truncate
elif eof.count(b'\n') < len(eof):
    # append b'\r\n' and truncate
else:
    # append b'\n' and truncate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick response on this. I'll get it updated.

)


Expand Down