Skip to content

Commit 84d3f3f

Browse files
committed
Add --exclude to large files filter
1 parent 3fed74c commit 84d3f3f

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Add this to your `.pre-commit-config.yaml`
2626
#### `check-added-large-files`
2727
Prevent giant files from being committed.
2828
- Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
29+
- Optionally exclude glob-like patterns with `args: ['--exclude=uv.lock,examples/*ipynb']`
2930
- Limits checked files to those indicated as staged for addition by git.
3031
- If `git-lfs` is installed, lfs files will be skipped
3132
(requires `git-lfs>=2.2.1`)

pre_commit_hooks/check_added_large_files.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import argparse
4+
from fnmatch import fnmatch
45
import math
56
import os
67
import subprocess
@@ -34,12 +35,17 @@ def find_large_added_files(
3435
filenames: Sequence[str],
3536
maxkb: int,
3637
*,
38+
exclude: list[str]|None = None,
3739
enforce_all: bool = False,
3840
) -> int:
3941
# Find all added files that are also in the list of files pre-commit tells
4042
# us about
4143
retv = 0
42-
filenames_filtered = set(filenames)
44+
exclude = [] if not exclude else exclude
45+
filenames_filtered = {
46+
fname for fname in filenames
47+
if not any(fnmatch(fname, pat) for pat in exclude)
48+
}
4349
filter_lfs_files(filenames_filtered)
4450

4551
if not enforce_all:
@@ -68,12 +74,17 @@ def main(argv: Sequence[str] | None = None) -> int:
6874
'--maxkb', type=int, default=500,
6975
help='Maximum allowable KB for added files',
7076
)
77+
parser.add_argument(
78+
'--exclude', type=str, default='',
79+
help="Comma-separated list of glob-style patterns to be excluded",
80+
)
7181
args = parser.parse_args(argv)
7282

7383
return find_large_added_files(
7484
args.filenames,
7585
args.maxkb,
7686
enforce_all=args.enforce_all,
87+
exclude=args.exclude.split(',')
7788
)
7889

7990

tests/check_added_large_files_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def test_add_something_giant(temp_git_dir):
4242
# Should pass with higher bound
4343
assert find_large_added_files(['f.py'], 10) == 0
4444

45+
def test_use_exclude(temp_git_dir):
46+
with temp_git_dir.as_cwd():
47+
temp_git_dir.join('uv.lock').write('a' * 10_000)
48+
temp_git_dir.join('big.baddie').write('a' * 10_000)
49+
50+
cmd_output('git', 'add', 'uv.lock')
51+
cmd_output('git', 'add', 'big.baddie')
52+
53+
# should fail due to big baddie as thats not excluded
54+
assert find_large_added_files(['uv.lock', 'big.baddie'], 1, exclude=['*.lock']) == 1
55+
# should pass when all files excluded, with both expand and exact matches
56+
assert find_large_added_files(['uv.lock'], 1, exclude=['*.lock']) == 0
57+
assert find_large_added_files(['uv.lock'], 1, exclude=['uv.lock']) == 0
58+
4559

4660
def test_enforce_all(temp_git_dir):
4761
with temp_git_dir.as_cwd():

0 commit comments

Comments
 (0)