Whole file

pre-commit/pre-commit-hooks

The author described this change as Fix bug with the file-contents-sorter hook when processing file that does not end in a newline. It counts as a record because the checks below fail on the code as it stood at 50871f83c and pass on 7cfec24f7, with nothing else changed between the two runs.

Fix saved2017-06-25
Sharing licenceMIT · LICENSE
Change size+9 10

What the code was meant to do, written into the code itself as a save note

Fix bug with the file-contents-sorter hook when processing file that does not end in a newline

The change

1818
1919
2020 def sort_file_contents(f):
21- before = tuple(f)
21+ before = [line.strip(b'\n\r') for line in f if line.strip()]
2222 after = sorted(before)
2323
24- before_string = b''.join(before)
25- after_string = b''.join(after)
26-
27- if before_string == after_string:
24+ if before == after:
2825 return PASS
29- else:
30- f.seek(0)
31- f.write(after_string)
32- f.truncate()
33- return FAIL
26+
27+ after_string = b'\n'.join(after) + b'\n'
28+
29+ f.seek(0)
30+ f.write(after_string)
31+ f.truncate()
32+ return FAIL
3433
3534
3635 def main(argv=None):

The check that tells the two apart

failpass·tests/file_contents_sorter_test.py::test_integration[extra\n\n\nwhitespace\n-0-extra\n\n\nwhitespace\n]
failpass·tests/file_contents_sorter_test.py::test_integration[newline\nmissing-1-missing\nnewline\n]
failpass·tests/file_contents_sorter_test.py::test_integration[whitespace\n\n\nextra\n-1-extra\nwhitespace\n]

Check file tests/file_contents_sorter_test.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it50871f83ccf5faf881fa3fbbcfb140432d990af0
Broken version dated2017-06-25
Modulepre_commit_hooks.file_contents_sorter
Units changedsort_file_contents
Fingerprint45b8446b7c9bcdbf
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in pre-commit/pre-commit-hooks