Skip to content

Commit ecdc22c

Browse files
committed
Implement global exclude
1 parent 6af6015 commit ecdc22c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

pre_commit/clientlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def validate_manifest_main(argv=None):
130130
'Config', None,
131131

132132
schema.RequiredRecurse('repos', schema.Array(CONFIG_REPO_DICT)),
133+
schema.Optional('exclude', schema.check_regex, '^$'),
133134
schema.Optional('fail_fast', schema.check_bool, False),
134135
)
135136

pre_commit/commands/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def _run_hooks(config, repo_hooks, args, environ):
183183
skips = _get_skips(environ)
184184
cols = _compute_cols([hook for _, hook in repo_hooks], args.verbose)
185185
filenames = _all_filenames(args)
186+
filenames = _filter_by_include_exclude(filenames, '', config['exclude'])
186187
retval = 0
187188
for repo, hook in repo_hooks:
188189
retval |= _run_single_hook(filenames, hook, repo, args, skips, cols)

tests/commands/run_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ def test_exclude_types_hook_repository(
183183
assert b'exe' not in printed
184184

185185

186+
def test_global_exclude(cap_out, tempdir_factory, mock_out_store_directory):
187+
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
188+
with cwd(git_path):
189+
with modify_config() as config:
190+
config['exclude'] = '^foo.py$'
191+
open('foo.py', 'a').close()
192+
open('bar.py', 'a').close()
193+
cmd_output('git', 'add', '.')
194+
ret, printed = _do_run(cap_out, git_path, _get_opts(verbose=True))
195+
assert ret == 0
196+
# Does not contain foo.py since it was excluded
197+
expected = b'hookid: bash_hook\n\nbar.py\nHello World\n\n'
198+
assert printed.endswith(expected)
199+
200+
186201
def test_show_diff_on_failure(
187202
capfd, cap_out, tempdir_factory, mock_out_store_directory,
188203
):

0 commit comments

Comments
 (0)