11import argparse
2+ import contextlib
23import functools
34import logging
45import os
2728from pre_commit .store import Store
2829from pre_commit .util import cmd_output_b
2930from pre_commit .util import EnvironT
30- from pre_commit .util import noop_context
3131
3232
3333logger = logging .getLogger ('pre_commit' )
@@ -272,7 +272,7 @@ def run(
272272 args : argparse .Namespace ,
273273 environ : EnvironT = os .environ ,
274274) -> int :
275- no_stash = args .all_files or bool ( args .files )
275+ stash = not args .all_files and not args .files
276276
277277 # Check if we have unresolved merge conflict files and fail fast.
278278 if _has_unmerged_paths ():
@@ -281,7 +281,7 @@ def run(
281281 if bool (args .source ) != bool (args .origin ):
282282 logger .error ('Specify both --origin and --source.' )
283283 return 1
284- if _has_unstaged_config (config_file ) and not no_stash :
284+ if stash and _has_unstaged_config (config_file ):
285285 logger .error (
286286 f'Your pre-commit configuration is unstaged.\n '
287287 f'`git add { config_file } ` to fix this.' ,
@@ -293,12 +293,10 @@ def run(
293293 environ ['PRE_COMMIT_ORIGIN' ] = args .origin
294294 environ ['PRE_COMMIT_SOURCE' ] = args .source
295295
296- if no_stash :
297- ctx = noop_context ()
298- else :
299- ctx = staged_files_only (store .directory )
296+ with contextlib .ExitStack () as exit_stack :
297+ if stash :
298+ exit_stack .enter_context (staged_files_only (store .directory ))
300299
301- with ctx :
302300 config = load_config (config_file )
303301 hooks = [
304302 hook
@@ -316,3 +314,6 @@ def run(
316314 install_hook_envs (hooks , store )
317315
318316 return _run_hooks (config , hooks , args , environ )
317+
318+ # https://github.com/python/mypy/issues/7726
319+ raise AssertionError ('unreachable' )
0 commit comments