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' )
@@ -173,7 +173,7 @@ def _run_single_hook(
173173
174174 if out .strip ():
175175 output .write_line ()
176- output .write_line (out .strip (), logfile_name = hook .log_file )
176+ output .write_line_b (out .strip (), logfile_name = hook .log_file )
177177 output .write_line ()
178178
179179 return files_modified or bool (retcode )
@@ -243,9 +243,10 @@ def _run_hooks(
243243 output .write_line ('All changes made by hooks:' )
244244 # args.color is a boolean.
245245 # See user_color function in color.py
246+ git_color_opt = 'always' if args .color else 'never'
246247 subprocess .call ((
247248 'git' , '--no-pager' , 'diff' , '--no-ext-diff' ,
248- '--color={}' . format ({ True : 'always' , False : 'never' }[ args . color ]) ,
249+ f '--color={ git_color_opt } ' ,
249250 ))
250251
251252 return retval
@@ -271,7 +272,7 @@ def run(
271272 args : argparse .Namespace ,
272273 environ : EnvironT = os .environ ,
273274) -> int :
274- no_stash = args .all_files or bool ( args .files )
275+ stash = not args .all_files and not args .files
275276
276277 # Check if we have unresolved merge conflict files and fail fast.
277278 if _has_unmerged_paths ():
@@ -280,10 +281,10 @@ def run(
280281 if bool (args .source ) != bool (args .origin ):
281282 logger .error ('Specify both --origin and --source.' )
282283 return 1
283- if _has_unstaged_config (config_file ) and not no_stash :
284+ if stash and _has_unstaged_config (config_file ):
284285 logger .error (
285- 'Your pre-commit configuration is unstaged.\n '
286- '`git add {}` to fix this.' . format ( config_file ) ,
286+ f 'Your pre-commit configuration is unstaged.\n '
287+ f '`git add { config_file } ` to fix this.' ,
287288 )
288289 return 1
289290
@@ -292,12 +293,10 @@ def run(
292293 environ ['PRE_COMMIT_ORIGIN' ] = args .origin
293294 environ ['PRE_COMMIT_SOURCE' ] = args .source
294295
295- if no_stash :
296- ctx = noop_context ()
297- else :
298- 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 ))
299299
300- with ctx :
301300 config = load_config (config_file )
302301 hooks = [
303302 hook
@@ -308,12 +307,13 @@ def run(
308307
309308 if args .hook and not hooks :
310309 output .write_line (
311- 'No hook with id `{}` in stage `{}`' .format (
312- args .hook , args .hook_stage ,
313- ),
310+ f'No hook with id `{ args .hook } ` in stage `{ args .hook_stage } `' ,
314311 )
315312 return 1
316313
317314 install_hook_envs (hooks , store )
318315
319316 return _run_hooks (config , hooks , args , environ )
317+
318+ # https://github.com/python/mypy/issues/7726
319+ raise AssertionError ('unreachable' )
0 commit comments