Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74fd04c

Browse files
authored
Merge pull request #1248 from pre-commit/top_level_files
Add top-level `files` key for inclusion
2 parents 0ecd50b + 6af0e33 commit 74fd04c

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

pre_commit/clientlib.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
logger = logging.getLogger('pre_commit')
2020

21+
check_string_regex = cfgv.check_and(cfgv.check_string, cfgv.check_regex)
22+
2123

2224
def check_type_tag(tag):
2325
if tag not in ALL_TAGS:
@@ -53,12 +55,8 @@ def _make_argparser(filenames_help):
5355
cfgv.Required('language', cfgv.check_one_of(all_languages)),
5456
cfgv.Optional('alias', cfgv.check_string, ''),
5557

56-
cfgv.Optional(
57-
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',
58-
),
59-
cfgv.Optional(
60-
'exclude', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '^$',
61-
),
58+
cfgv.Optional('files', check_string_regex, ''),
59+
cfgv.Optional('exclude', check_string_regex, '^$'),
6260
cfgv.Optional('types', cfgv.check_array(check_type_tag), ['file']),
6361
cfgv.Optional('exclude_types', cfgv.check_array(check_type_tag), []),
6462

@@ -260,7 +258,8 @@ def warn_unknown_keys_repo(extra, orig_keys, dct):
260258
cfgv.check_array(cfgv.check_one_of(C.STAGES)),
261259
C.STAGES,
262260
),
263-
cfgv.Optional('exclude', cfgv.check_regex, '^$'),
261+
cfgv.Optional('files', check_string_regex, ''),
262+
cfgv.Optional('exclude', check_string_regex, '^$'),
264263
cfgv.Optional('fail_fast', cfgv.check_bool, False),
265264
cfgv.Optional(
266265
'minimum_pre_commit_version',
@@ -272,6 +271,7 @@ def warn_unknown_keys_repo(extra, orig_keys, dct):
272271
'repos',
273272
'default_language_version',
274273
'default_stages',
274+
'files',
275275
'exclude',
276276
'fail_fast',
277277
'minimum_pre_commit_version',

pre_commit/commands/run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ def _run_hooks(config, hooks, args, environ):
206206
skips = _get_skips(environ)
207207
cols = _compute_cols(hooks, args.verbose)
208208
filenames = _all_filenames(args)
209-
filenames = filter_by_include_exclude(filenames, '', config['exclude'])
209+
filenames = filter_by_include_exclude(
210+
filenames, config['files'], config['exclude'],
211+
)
210212
classifier = Classifier(filenames)
211213
retval = 0
212214
for hook in hooks:

tests/commands/run_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ def test_global_exclude(cap_out, store, tempdir_factory):
180180
assert printed.endswith(expected)
181181

182182

183+
def test_global_files(cap_out, store, tempdir_factory):
184+
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
185+
with cwd(git_path):
186+
with modify_config() as config:
187+
config['files'] = '^bar.py$'
188+
open('foo.py', 'a').close()
189+
open('bar.py', 'a').close()
190+
cmd_output('git', 'add', '.')
191+
opts = run_opts(verbose=True)
192+
ret, printed = _do_run(cap_out, store, git_path, opts)
193+
assert ret == 0
194+
# Does not contain foo.py since it was not included
195+
expected = b'hookid: bash_hook\n\nbar.py\nHello World\n\n'
196+
assert printed.endswith(expected)
197+
198+
183199
@pytest.mark.parametrize(
184200
('args', 'expected_out'),
185201
[

0 commit comments

Comments
 (0)