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.
Projectpre-commit/pre-commit-hooks
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
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | def sort_file_contents(f): | |
| 21 | - | before = tuple(f) | |
| 21 | + | before = [line.strip(b'\n\r') for line in f if line.strip()] | |
| 22 | 22 | after = sorted(before) | |
| 23 | 23 | ||
| 24 | - | before_string = b''.join(before) | |
| 25 | - | after_string = b''.join(after) | |
| 26 | - | ||
| 27 | - | if before_string == after_string: | |
| 24 | + | if before == after: | |
| 28 | 25 | 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 | |
| 34 | 33 | ||
| 35 | 34 | ||
| 36 | 35 | def main(argv=None): |
The check that tells the two apart
fail→pass·tests/file_contents_sorter_test.py::test_integration[extra\n\n\nwhitespace\n-0-extra\n\n\nwhitespace\n]
fail→pass·tests/file_contents_sorter_test.py::test_integration[newline\nmissing-1-missing\nnewline\n]
fail→pass·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
- 2024-04-20Continues processing JSONs even if hook fails (fixes #1038)
- 2020-09-27Fix #518, provide --enforce-all option to check_added_large_files
- 2020-07-30Fix parsing of git output with unusual characters
- 2019-10-25fix-whitespace: Added test for custom charsets
- 2019-05-15Handle crlf endings in fix-encoding-pragma
- 2017-09-27Fix mixed-line-endings --fix=... when whole file is a different ending