Skip to content

Commit 5bd9e74

Browse files
authored
Merge pull request #519 from mshawcroft/fix-518
Fix #518, provide --enforce-all option to check_added_large_files
2 parents 31d41ff + 012bb06 commit 5bd9e74

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

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: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ 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],
26+
maxkb: int,
27+
*,
28+
enforce_all: bool = False,
29+
) -> int:
2530
# Find all added files that are also in the list of files pre-commit tells
2631
# us about
2732
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:
2938
kb = int(math.ceil(os.stat(filename).st_size / 1024))
3039
if kb > maxkb:
3140
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
@@ -40,13 +49,21 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
4049
'filenames', nargs='*',
4150
help='Filenames pre-commit believes are changed.',
4251
)
52+
parser.add_argument(
53+
'--enforce-all', action='store_true',
54+
help='Enforce all files are checked, not just staged files.',
55+
)
4356
parser.add_argument(
4457
'--maxkb', type=int, default=500,
4558
help='Maxmimum allowable KB for added files',
4659
)
47-
4860
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+
)
5067

5168

5269
if __name__ == '__main__':

tests/check_added_large_files_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def test_add_something_giant(temp_git_dir):
4040
assert find_large_added_files(['f.py'], 10) == 0
4141

4242

43+
def test_enforce_all(temp_git_dir):
44+
with temp_git_dir.as_cwd():
45+
temp_git_dir.join('f.py').write('a' * 10000)
46+
47+
# Should fail, when not staged with enforce_all
48+
assert find_large_added_files(['f.py'], 0, enforce_all=True) == 1
49+
50+
# Should pass, when not staged without enforce_all
51+
assert find_large_added_files(['f.py'], 0, enforce_all=False) == 0
52+
53+
4354
def test_added_file_not_in_pre_commits_list(temp_git_dir):
4455
with temp_git_dir.as_cwd():
4556
temp_git_dir.join('f.py').write("print('hello world')")
@@ -97,3 +108,15 @@ def test_moves_with_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
97108
# Now move it and make sure the hook still succeeds
98109
cmd_output('git', 'mv', 'a.bin', 'b.bin')
99110
assert main(('--maxkb', '9', 'b.bin')) == 0
111+
112+
113+
@xfailif_no_gitlfs
114+
def test_enforce_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover
115+
with temp_git_dir.as_cwd():
116+
monkeypatch.setenv('HOME', str(temp_git_dir))
117+
cmd_output('git', 'lfs', 'install')
118+
temp_git_dir.join('f.py').write('a' * 10000)
119+
cmd_output('git', 'lfs', 'track', 'f.py')
120+
cmd_output('git', 'add', '--', '.')
121+
# With --enforce-all large files on git lfs should succeed
122+
assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0

0 commit comments

Comments
 (0)