mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
file-contents-sorter should add newline at end of files missing newlines
Make an explicit 'else' path for readability
This commit is contained in:
parent
5dd1819e8b
commit
7102e0c8a3
2 changed files with 14 additions and 13 deletions
|
|
@ -18,18 +18,19 @@ FAIL = 1
|
|||
|
||||
|
||||
def sort_file_contents(f):
|
||||
before = [line.strip(b'\n\r') for line in f if line.strip()]
|
||||
after = sorted(before)
|
||||
|
||||
if before == after:
|
||||
return PASS
|
||||
before = list(f)
|
||||
after = sorted([line.strip(b'\n\r') for line in before if line.strip()])
|
||||
|
||||
before_string = b''.join(before)
|
||||
after_string = b'\n'.join(after) + b'\n'
|
||||
|
||||
f.seek(0)
|
||||
f.write(after_string)
|
||||
f.truncate()
|
||||
return FAIL
|
||||
if before_string == after_string:
|
||||
return PASS
|
||||
else:
|
||||
f.seek(0)
|
||||
f.write(after_string)
|
||||
f.truncate()
|
||||
return FAIL
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue