Whole file
pre-commit/pre-commit-hooks
The author described this change as “Fix #518, provide --enforce-all option to check_added_large_files”. It counts as a record because the check below fails on the code as it stood at 31d41ff29 and passes on 012bb0691, with nothing else changed between the two runs.
Projectpre-commit/pre-commit-hooks
Fix saved2020-09-27
Sharing licenceMIT · LICENSE
Change size+21 −4
What the code was meant to do, written into the code itself as a save note
Fix #518, provide --enforce-all option to check_added_large_files
The change
| 21 | 21 | return set(json.loads(lfs_ret)['files']) | |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | - | def find_large_added_files(filenames: Sequence[str], maxkb: int) -> int: | |
| 24 | + | def find_large_added_files( | |
| 25 | + | filenames: Sequence[str], | |
| 26 | + | maxkb: int, | |
| 27 | + | *, | |
| 28 | + | enforce_all: bool = False, | |
| 29 | + | ) -> int: | |
| 25 | 30 | # Find all added files that are also in the list of files pre-commit tells | |
| 26 | 31 | # us about | |
| 27 | 32 | retv = 0 | |
| 28 | - | for filename in (added_files() & set(filenames)) - lfs_files(): | |
| 33 | + | filenames_filtered = set(filenames) - lfs_files() | |
| 34 | + | if not enforce_all: | |
| 35 | + | filenames_filtered &= added_files() | |
| 36 | + | ||
| 37 | + | for filename in filenames_filtered: | |
| 29 | 38 | kb = int(math.ceil(os.stat(filename).st_size / 1024)) | |
| 30 | 39 | if kb > maxkb: | |
| 31 | 40 | print(f'{filename} ({kb} KB) exceeds {maxkb} KB.') | |
| ⋯ | |||
| 41 | 50 | help='Filenames pre-commit believes are changed.', | |
| 42 | 51 | ) | |
| 43 | 52 | parser.add_argument( | |
| 53 | + | '--enforce-all', action='store_true', | |
| 54 | + | help='Enforce all files are checked, not just staged files.', | |
| 55 | + | ) | |
| 56 | + | parser.add_argument( | |
| 44 | 57 | '--maxkb', type=int, default=500, | |
| 45 | 58 | help='Maxmimum allowable KB for added files', | |
| 46 | 59 | ) | |
| 47 | - | ||
| 48 | 60 | args = parser.parse_args(argv) | |
| 49 | - | return find_large_added_files(args.filenames, args.maxkb) | |
| 61 | + | ||
| 62 | + | return find_large_added_files( | |
| 63 | + | args.filenames, | |
| 64 | + | args.maxkb, | |
| 65 | + | enforce_all=args.enforce_all, | |
| 66 | + | ) | |
| 50 | 67 | ||
| 51 | 68 | ||
| 52 | 69 | if __name__ == '__main__': | |
The check that tells the two apart
fail→pass·tests/check_added_large_files_test.py::test_enforce_all
Check file tests/check_added_large_files_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 it31d41ff29115a87808277ee0ec23999b17d5b583
Broken version dated2020-08-26
Modulepre_commit_hooks.check_added_large_files
Units changedfind_large_added_files, main
Fingerprint2fb76db6a328deeb
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-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
- 2017-06-25Fix bug with the file-contents-sorter hook when processing file that does not end in a newline