2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-01-01 04:21:21 +08:00
|
|
|
import argparse
|
|
|
|
import math
|
|
|
|
import os
|
2021-10-22 06:29:54 +08:00
|
|
|
import subprocess
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2015-01-01 04:21:21 +08:00
|
|
|
|
2015-01-08 06:07:32 +08:00
|
|
|
from pre_commit_hooks.util import added_files
|
2021-10-22 06:29:54 +08:00
|
|
|
from pre_commit_hooks.util import zsplit
|
2015-12-26 01:25:14 +08:00
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def filter_lfs_files(filenames: set[str]) -> None: # pragma: no cover (lfs)
|
2021-10-22 06:29:54 +08:00
|
|
|
"""Remove files tracked by git-lfs from the set."""
|
|
|
|
if not filenames:
|
|
|
|
return
|
2015-12-26 01:25:14 +08:00
|
|
|
|
2021-10-22 06:29:54 +08:00
|
|
|
check_attr = subprocess.run(
|
|
|
|
('git', 'check-attr', 'filter', '-z', '--stdin'),
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
encoding='utf-8',
|
|
|
|
check=True,
|
|
|
|
input='\0'.join(filenames),
|
|
|
|
)
|
|
|
|
stdout = zsplit(check_attr.stdout)
|
|
|
|
for i in range(0, len(stdout), 3):
|
|
|
|
filename, filter_tag = stdout[i], stdout[i + 2]
|
|
|
|
if filter_tag == 'lfs':
|
|
|
|
filenames.remove(filename)
|
2015-01-01 04:21:21 +08:00
|
|
|
|
|
|
|
|
2020-09-16 13:26:11 +08:00
|
|
|
def find_large_added_files(
|
|
|
|
filenames: Sequence[str],
|
|
|
|
maxkb: int,
|
|
|
|
*,
|
|
|
|
enforce_all: bool = False,
|
|
|
|
) -> int:
|
2015-01-01 04:21:21 +08:00
|
|
|
# Find all added files that are also in the list of files pre-commit tells
|
|
|
|
# us about
|
|
|
|
retv = 0
|
2021-10-22 06:29:54 +08:00
|
|
|
filenames_filtered = set(filenames)
|
|
|
|
filter_lfs_files(filenames_filtered)
|
|
|
|
|
2020-09-16 13:26:11 +08:00
|
|
|
if not enforce_all:
|
|
|
|
filenames_filtered &= added_files()
|
|
|
|
|
|
|
|
for filename in filenames_filtered:
|
2023-09-22 01:54:38 +08:00
|
|
|
kb = math.ceil(os.stat(filename).st_size / 1024)
|
2015-01-01 04:21:21 +08:00
|
|
|
if kb > maxkb:
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
|
2015-01-01 04:21:21 +08:00
|
|
|
retv = 1
|
|
|
|
|
|
|
|
return retv
|
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2015-01-01 04:21:21 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'filenames', nargs='*',
|
2017-07-13 09:35:24 +08:00
|
|
|
help='Filenames pre-commit believes are changed.',
|
2015-01-01 04:21:21 +08:00
|
|
|
)
|
2020-09-16 13:26:11 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--enforce-all', action='store_true',
|
|
|
|
help='Enforce all files are checked, not just staged files.',
|
|
|
|
)
|
2015-01-01 04:21:21 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--maxkb', type=int, default=500,
|
2021-10-23 19:11:03 +08:00
|
|
|
help='Maximum allowable KB for added files',
|
2015-01-01 04:21:21 +08:00
|
|
|
)
|
|
|
|
args = parser.parse_args(argv)
|
2020-09-16 13:26:11 +08:00
|
|
|
|
|
|
|
return find_large_added_files(
|
|
|
|
args.filenames,
|
|
|
|
args.maxkb,
|
|
|
|
enforce_all=args.enforce_all,
|
|
|
|
)
|
2015-01-01 04:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|