summaryrefslogtreecommitdiffstats
path: root/pre_commit/meta_hooks/check_hooks_apply.py
blob: 84c142b455a260639f588fa0700227844e7020ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import annotations

import argparse
from collections.abc import Sequence

import pre_commit.constants as C
from pre_commit import git
from pre_commit.clientlib import load_config
from pre_commit.commands.run import Classifier
from pre_commit.repository import all_hooks
from pre_commit.store import Store


def check_all_hooks_match_files(config_file: str) -> int:
    config = load_config(config_file)
    classifier = Classifier.from_config(
        git.get_all_files(), config['files'], config['exclude'],
    )
    retv = 0

    for hook in all_hooks(config, Store()):
        if hook.always_run or hook.language == 'fail':
            continue
        elif not any(classifier.filenames_for_hook(hook)):
            print(f'{hook.id} does not apply to this repository')
            retv = 1

    return retv


def main(argv: Sequence[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument('filenames', nargs='*', default=[C.CONFIG_FILE])
    args = parser.parse_args(argv)

    retv = 0
    for filename in args.filenames:
        retv |= check_all_hooks_match_files(filename)
    return retv


if __name__ == '__main__':
    raise SystemExit(main())