Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pre_commit/commands/hook_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def _ns(
hook=None,
verbose=False,
show_diff_on_failure=False,
fail_fast=False,
)


Expand Down
3 changes: 2 additions & 1 deletion pre_commit/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ def _run_hooks(
verbose=args.verbose, use_color=args.color,
)
retval |= current_retval
if current_retval and (config['fail_fast'] or hook.fail_fast):
fail_fast = (config['fail_fast'] or hook.fail_fast or args.fail_fast)
if current_retval and fail_fast:
break
if retval and args.show_diff_on_failure and prior_diff:
if args.all_files:
Expand Down
4 changes: 4 additions & 0 deletions pre_commit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
'--show-diff-on-failure', action='store_true',
help='When hooks fail, run `git diff` directly afterward.',
)
parser.add_argument(
'--fail-fast', action='store_true',
help='Stop after the first failing hook.',
)
parser.add_argument(
'--hook-stage',
choices=clientlib.STAGES,
Expand Down
2 changes: 2 additions & 0 deletions testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def run_opts(
color=False,
verbose=False,
hook=None,
fail_fast=False,
remote_branch='',
local_branch='',
from_ref='',
Expand All @@ -65,6 +66,7 @@ def run_opts(
color=color,
verbose=verbose,
hook=hook,
fail_fast=fail_fast,
remote_branch=remote_branch,
local_branch=local_branch,
from_ref=from_ref,
Expand Down
13 changes: 13 additions & 0 deletions tests/commands/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,19 @@ def test_fail_fast_not_prev_failures(cap_out, store, repo_with_failing_hook):
assert printed.count(b'run me!') == 1


def test_fail_fast_run_arg(cap_out, store, repo_with_failing_hook):
with modify_config() as config:
# More than one hook to demonstrate early exit
config['repos'][0]['hooks'] *= 2
stage_a_file()

ret, printed = _do_run(
cap_out, store, repo_with_failing_hook, run_opts(fail_fast=True),
)
# it should have only run one hook due to the CLI flag
assert printed.count(b'Failing hook') == 1


def test_classifier_removes_dne():
classifier = Classifier(('this_file_does_not_exist',))
assert classifier.filenames == []
Expand Down
Loading