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

Skip to content

Commit 4640dc7

Browse files
committed
Run only the specified hook even when stages exist in config.
This branches fixes the run logic so that when `pre-commit run some_hook -a` runs when the config contains `stages: ['commit']` for some other hook, only the hook specified as an argument will run. Fixes #772
1 parent f2da2c4 commit 4640dc7

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

pre_commit/commands/run.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ def run(runner, store, args, environ=os.environ):
254254
repo_hooks = []
255255
for repo in repositories(runner.config, store):
256256
for _, hook in repo.hooks:
257-
if (
258-
(not args.hook or hook['id'] == args.hook) and
259-
not hook['stages'] or args.hook_stage in hook['stages']
260-
):
261-
repo_hooks.append((repo, hook))
257+
if args.hook:
258+
if args.hook == hook['id']:
259+
repo_hooks.append((repo, hook))
260+
break
261+
else:
262+
if not hook['stages'] or args.hook_stage in hook['stages']:
263+
repo_hooks.append((repo, hook))
262264

263265
if args.hook and not repo_hooks:
264266
output.write_line('No hook with id `{}`'.format(args.hook))

tests/commands/run_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,34 @@ def test_include_exclude_does_search_instead_of_match(some_filenames):
762762
def test_include_exclude_exclude_removes_files(some_filenames):
763763
ret = _filter_by_include_exclude(some_filenames, '', r'\.py$')
764764
assert ret == ['.pre-commit-hooks.yaml']
765+
766+
767+
def test_args_hook_only(cap_out, store, repo_with_passing_hook):
768+
config = OrderedDict((
769+
('repo', 'local'),
770+
(
771+
'hooks', (
772+
OrderedDict((
773+
('id', 'flake8'),
774+
('name', 'flake8'),
775+
('entry', "'{}' -m flake8".format(sys.executable)),
776+
('language', 'system'),
777+
('stages', ['commit']),
778+
)), OrderedDict((
779+
('id', 'do_not_commit'),
780+
('name', 'Block if "DO NOT COMMIT" is found'),
781+
('entry', 'DO NOT COMMIT'),
782+
('language', 'pygrep'),
783+
)),
784+
),
785+
),
786+
))
787+
add_config_to_repo(repo_with_passing_hook, config)
788+
stage_a_file()
789+
ret, printed = _do_run(
790+
cap_out,
791+
store,
792+
repo_with_passing_hook,
793+
run_opts(hook='do_not_commit'),
794+
)
795+
assert 'flake8' not in printed

0 commit comments

Comments
 (0)