Skip to content

Commit 6e446e7

Browse files
committed
Add fail-fast argument for run command
1 parent e70b313 commit 6e446e7

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

pre_commit/commands/hook_impl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def _ns(
106106
hook=None,
107107
verbose=False,
108108
show_diff_on_failure=False,
109+
fail_fast=False,
109110
)
110111

111112

pre_commit/commands/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ def _run_hooks(
298298
verbose=args.verbose, use_color=args.color,
299299
)
300300
retval |= current_retval
301-
if current_retval and (config['fail_fast'] or hook.fail_fast):
301+
fail_fast = (config['fail_fast'] or hook.fail_fast or args.fail_fast)
302+
if current_retval and fail_fast:
302303
break
303304
if retval and args.show_diff_on_failure and prior_diff:
304305
if args.all_files:

pre_commit/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
7676
'--show-diff-on-failure', action='store_true',
7777
help='When hooks fail, run `git diff` directly afterward.',
7878
)
79+
parser.add_argument(
80+
'--fail-fast', action='store_true', default=False,
81+
help='Stop after the first failing hook.',
82+
)
7983
parser.add_argument(
8084
'--hook-stage',
8185
choices=clientlib.STAGES,

testing/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def run_opts(
4040
color=False,
4141
verbose=False,
4242
hook=None,
43+
fail_fast=False,
4344
remote_branch='',
4445
local_branch='',
4546
from_ref='',
@@ -65,6 +66,7 @@ def run_opts(
6566
color=color,
6667
verbose=verbose,
6768
hook=hook,
69+
fail_fast=fail_fast,
6870
remote_branch=remote_branch,
6971
local_branch=local_branch,
7072
from_ref=from_ref,

tests/commands/run_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,19 @@ def test_fail_fast_not_prev_failures(cap_out, store, repo_with_failing_hook):
11041104
assert printed.count(b'run me!') == 1
11051105

11061106

1107+
def test_fail_fast_run_arg(cap_out, store, repo_with_failing_hook):
1108+
with modify_config() as config:
1109+
# More than one hook to demonstrate early exit
1110+
config['repos'][0]['hooks'] *= 2
1111+
stage_a_file()
1112+
1113+
ret, printed = _do_run(
1114+
cap_out, store, repo_with_failing_hook, run_opts(fail_fast=True),
1115+
)
1116+
# it should have only run one hook due to the CLI flag
1117+
assert printed.count(b'Failing hook') == 1
1118+
1119+
11071120
def test_classifier_removes_dne():
11081121
classifier = Classifier(('this_file_does_not_exist',))
11091122
assert classifier.filenames == []

0 commit comments

Comments
 (0)