mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 03:26:53 +00:00
parent
203735ecf7
commit
647959c4b4
2 changed files with 2 additions and 6 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue