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.
This commit is contained in:
Marcus Shawcroft 2020-09-16 06:26:11 +01:00 committed by Anthony Sottile
parent 31d41ff291
commit 012bb0691f
3 changed files with 47 additions and 4 deletions

View file

@ -21,11 +21,20 @@ def lfs_files() -> Set[str]:
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.')
@ -40,13 +49,21 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
'filenames', nargs='*',
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__':