Skip to content

Commit 29d31bd

Browse files
committed
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.
1 parent 31d41ff commit 29d31bd

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ 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+
- Limits checked files to those indicated as staged for addition by git.
2930
- If `git-lfs` is installed, lfs files will be skipped
3031
(requires `git-lfs>=2.2.1`)
32+
- `--enforce-all` - Check all listed files not just those staged for
33+
addition.
3134

3235
#### `check-ast`
3336
Simply check whether files parse as valid python.

pre_commit_hooks/check_added_large_files.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ def lfs_files() -> Set[str]:
2121
return set(json.loads(lfs_ret)['files'])
2222

2323

24-
def find_large_added_files(filenames: Sequence[str], maxkb: int) -> int:
24+
def find_large_added_files(
25+
filenames: Sequence[str], maxkb: int,
26+
enforce_all: Optional[bool] = False,
27+
) -> int:
2528
# Find all added files that are also in the list of files pre-commit tells
2629
# us about
2730
retv = 0
28-
for filename in (added_files() & set(filenames)) - lfs_files():
31+
filename_set = set(filenames)
32+
if not enforce_all:
33+
filename_set &= added_files()
34+
filename_set -= lfs_files()
35+
36+
for filename in filename_set:
2937
kb = int(math.ceil(os.stat(filename).st_size / 1024))
3038
if kb > maxkb:
3139
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
@@ -40,13 +48,20 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
4048
'filenames', nargs='*',
4149
help='Filenames pre-commit believes are changed.',
4250
)
51+
parser.add_argument(
52+
'--enforce-all', action='store_true',
53+
help='Enforce all files are checked.',
54+
)
4355
parser.add_argument(
4456
'--maxkb', type=int, default=500,
4557
help='Maxmimum allowable KB for added files',
4658
)
4759

4860
args = parser.parse_args(argv)
49-
return find_large_added_files(args.filenames, args.maxkb)
61+
return find_large_added_files(
62+
args.filenames, args.maxkb,
63+
args.enforce_all,
64+
)
5065

5166

5267
if __name__ == '__main__':

tests/check_added_large_files_test.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ def test_add_something_giant(temp_git_dir):
3939
# Should pass with higher bound
4040
assert find_large_added_files(['f.py'], 10) == 0
4141

42+
# Should fail, irrespective of check_only_staged when staged
43+
assert find_large_added_files(['f.py'], 0, True) == 1
44+
assert find_large_added_files(['f.py'], 0, False) == 1
45+
46+
# File is committed hence no longer staged
47+
cmd_output('git', 'commit', '-m', 'log')
48+
49+
# Should fail, when not staged with enforce_all
50+
assert find_large_added_files(['f.py'], 0, True) == 1
51+
52+
# Should pass, when not staged without enforce_all
53+
assert find_large_added_files(['f.py'], 0, False) == 0
54+
4255

4356
def test_added_file_not_in_pre_commits_list(temp_git_dir):
4457
with temp_git_dir.as_cwd():
@@ -80,8 +93,10 @@ def test_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
8093
temp_git_dir.join('f.py').write('a' * 10000)
8194
cmd_output('git', 'lfs', 'track', 'f.py')
8295
cmd_output('git', 'add', '--', '.')
83-
# Should succeed
96+
# Without --enforce-all large files on git lfs should succeed
8497
assert main(('--maxkb', '9', 'f.py')) == 0
98+
# With --enforce-all large files on git lfs should fail
99+
assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 1
85100

86101

87102
@xfailif_no_gitlfs
@@ -97,3 +112,4 @@ def test_moves_with_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
97112
# Now move it and make sure the hook still succeeds
98113
cmd_output('git', 'mv', 'a.bin', 'b.bin')
99114
assert main(('--maxkb', '9', 'b.bin')) == 0
115+
assert main(('--enforce-all', '--maxkb', '9', 'b.bin')) == 1

0 commit comments

Comments
 (0)