Fix #518, provide --enforce-all option to check_added_large_files
The --enforce-all option when provided ensures that all files passed
on the command line are checked against the size limit. Default
behaviour remains unchanged.
diff --git a/pre_commit_hooks/check_added_large_files.py b/pre_commit_hooks/check_added_large_files.py
index 91f5754..cb646d7 100644
--- a/pre_commit_hooks/check_added_large_files.py
+++ b/pre_commit_hooks/check_added_large_files.py
@@ -21,11 +21,20 @@
return set(json.loads(lfs_ret)['files'])
-def find_large_added_files(filenames: Sequence[str], maxkb: int) -> int:
+def find_large_added_files(
+ filenames: Sequence[str],
+ maxkb: int,
+ *,
+ enforce_all: bool = False,
+) -> int:
# Find all added files that are also in the list of files pre-commit tells
# us about
retv = 0
- for filename in (added_files() & set(filenames)) - lfs_files():
+ filenames_filtered = set(filenames) - lfs_files()
+ if not enforce_all:
+ filenames_filtered &= added_files()
+
+ for filename in filenames_filtered:
kb = int(math.ceil(os.stat(filename).st_size / 1024))
if kb > maxkb:
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
@@ -41,12 +50,20 @@
help='Filenames pre-commit believes are changed.',
)
parser.add_argument(
+ '--enforce-all', action='store_true',
+ help='Enforce all files are checked, not just staged files.',
+ )
+ parser.add_argument(
'--maxkb', type=int, default=500,
help='Maxmimum allowable KB for added files',
)
-
args = parser.parse_args(argv)
- return find_large_added_files(args.filenames, args.maxkb)
+
+ return find_large_added_files(
+ args.filenames,
+ args.maxkb,
+ enforce_all=args.enforce_all,
+ )
if __name__ == '__main__':