mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1248 from pre-commit/top_level_files
Add top-level `files` key for inclusion
This commit is contained in:
commit
74fd04c67e
3 changed files with 26 additions and 8 deletions
|
|
@ -18,6 +18,8 @@ from pre_commit.util import parse_version
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
||||||
|
check_string_regex = cfgv.check_and(cfgv.check_string, cfgv.check_regex)
|
||||||
|
|
||||||
|
|
||||||
def check_type_tag(tag):
|
def check_type_tag(tag):
|
||||||
if tag not in ALL_TAGS:
|
if tag not in ALL_TAGS:
|
||||||
|
|
@ -53,12 +55,8 @@ MANIFEST_HOOK_DICT = cfgv.Map(
|
||||||
cfgv.Required('language', cfgv.check_one_of(all_languages)),
|
cfgv.Required('language', cfgv.check_one_of(all_languages)),
|
||||||
cfgv.Optional('alias', cfgv.check_string, ''),
|
cfgv.Optional('alias', cfgv.check_string, ''),
|
||||||
|
|
||||||
cfgv.Optional(
|
cfgv.Optional('files', check_string_regex, ''),
|
||||||
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',
|
cfgv.Optional('exclude', check_string_regex, '^$'),
|
||||||
),
|
|
||||||
cfgv.Optional(
|
|
||||||
'exclude', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '^$',
|
|
||||||
),
|
|
||||||
cfgv.Optional('types', cfgv.check_array(check_type_tag), ['file']),
|
cfgv.Optional('types', cfgv.check_array(check_type_tag), ['file']),
|
||||||
cfgv.Optional('exclude_types', cfgv.check_array(check_type_tag), []),
|
cfgv.Optional('exclude_types', cfgv.check_array(check_type_tag), []),
|
||||||
|
|
||||||
|
|
@ -260,7 +258,8 @@ CONFIG_SCHEMA = cfgv.Map(
|
||||||
cfgv.check_array(cfgv.check_one_of(C.STAGES)),
|
cfgv.check_array(cfgv.check_one_of(C.STAGES)),
|
||||||
C.STAGES,
|
C.STAGES,
|
||||||
),
|
),
|
||||||
cfgv.Optional('exclude', cfgv.check_regex, '^$'),
|
cfgv.Optional('files', check_string_regex, ''),
|
||||||
|
cfgv.Optional('exclude', check_string_regex, '^$'),
|
||||||
cfgv.Optional('fail_fast', cfgv.check_bool, False),
|
cfgv.Optional('fail_fast', cfgv.check_bool, False),
|
||||||
cfgv.Optional(
|
cfgv.Optional(
|
||||||
'minimum_pre_commit_version',
|
'minimum_pre_commit_version',
|
||||||
|
|
@ -272,6 +271,7 @@ CONFIG_SCHEMA = cfgv.Map(
|
||||||
'repos',
|
'repos',
|
||||||
'default_language_version',
|
'default_language_version',
|
||||||
'default_stages',
|
'default_stages',
|
||||||
|
'files',
|
||||||
'exclude',
|
'exclude',
|
||||||
'fail_fast',
|
'fail_fast',
|
||||||
'minimum_pre_commit_version',
|
'minimum_pre_commit_version',
|
||||||
|
|
|
||||||
|
|
@ -206,7 +206,9 @@ def _run_hooks(config, hooks, args, environ):
|
||||||
skips = _get_skips(environ)
|
skips = _get_skips(environ)
|
||||||
cols = _compute_cols(hooks, args.verbose)
|
cols = _compute_cols(hooks, args.verbose)
|
||||||
filenames = _all_filenames(args)
|
filenames = _all_filenames(args)
|
||||||
filenames = filter_by_include_exclude(filenames, '', config['exclude'])
|
filenames = filter_by_include_exclude(
|
||||||
|
filenames, config['files'], config['exclude'],
|
||||||
|
)
|
||||||
classifier = Classifier(filenames)
|
classifier = Classifier(filenames)
|
||||||
retval = 0
|
retval = 0
|
||||||
for hook in hooks:
|
for hook in hooks:
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,22 @@ def test_global_exclude(cap_out, store, tempdir_factory):
|
||||||
assert printed.endswith(expected)
|
assert printed.endswith(expected)
|
||||||
|
|
||||||
|
|
||||||
|
def test_global_files(cap_out, store, tempdir_factory):
|
||||||
|
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
|
with cwd(git_path):
|
||||||
|
with modify_config() as config:
|
||||||
|
config['files'] = '^bar.py$'
|
||||||
|
open('foo.py', 'a').close()
|
||||||
|
open('bar.py', 'a').close()
|
||||||
|
cmd_output('git', 'add', '.')
|
||||||
|
opts = run_opts(verbose=True)
|
||||||
|
ret, printed = _do_run(cap_out, store, git_path, opts)
|
||||||
|
assert ret == 0
|
||||||
|
# Does not contain foo.py since it was not included
|
||||||
|
expected = b'hookid: bash_hook\n\nbar.py\nHello World\n\n'
|
||||||
|
assert printed.endswith(expected)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('args', 'expected_out'),
|
('args', 'expected_out'),
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue