77
88from pre_commit import color
99from pre_commit import git
10+ from pre_commit import output
1011from pre_commit .logging_handler import LoggingHandler
1112from pre_commit .output import get_hook_message
12- from pre_commit .output import sys_stdout_write_wrapper
1313from pre_commit .staged_files_only import staged_files_only
1414from pre_commit .util import cmd_output
1515from pre_commit .util import noop_context
@@ -56,10 +56,10 @@ def get_filenames(args, include_expr, exclude_expr):
5656NO_FILES = '(no files to check)'
5757
5858
59- def _run_single_hook (hook , repo , args , write , skips , cols ):
59+ def _run_single_hook (hook , repo , args , skips , cols ):
6060 filenames = get_filenames (args , hook ['files' ], hook ['exclude' ])
6161 if hook ['id' ] in skips :
62- write (get_hook_message (
62+ output . write (get_hook_message (
6363 _hook_msg_start (hook , args .verbose ),
6464 end_msg = SKIPPED ,
6565 end_color = color .YELLOW ,
@@ -68,7 +68,7 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
6868 ))
6969 return 0
7070 elif not filenames and not hook ['always_run' ]:
71- write (get_hook_message (
71+ output . write (get_hook_message (
7272 _hook_msg_start (hook , args .verbose ),
7373 postfix = NO_FILES ,
7474 end_msg = SKIPPED ,
@@ -80,7 +80,7 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
8080
8181 # Print the hook and the dots first in case the hook takes hella long to
8282 # run.
83- write (get_hook_message (
83+ output . write (get_hook_message (
8484 _hook_msg_start (hook , args .verbose ), end_len = 6 , cols = cols ,
8585 ))
8686 sys .stdout .flush ()
@@ -104,26 +104,25 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
104104 print_color = color .GREEN
105105 pass_fail = 'Passed'
106106
107- write (color .format_color (pass_fail , print_color , args .color ) + ' \n ' )
107+ output . write_line (color .format_color (pass_fail , print_color , args .color ))
108108
109109 if (stdout or stderr or file_modifications ) and (retcode or args .verbose ):
110- write ('hookid: {}\n ' .format (hook ['id' ]))
111- write ('\n ' )
110+ output .write_line ('hookid: {}\n ' .format (hook ['id' ]))
112111
113112 # Print a message if failing due to file modifications
114113 if file_modifications :
115- write ('Files were modified by this hook.' )
114+ output . write ('Files were modified by this hook.' )
116115
117116 if stdout or stderr :
118- write (' Additional output:\n ' )
117+ output . write_line (' Additional output:' )
119118
120- write ( ' \n ' )
119+ output . write_line ( )
121120
122- for output in (stdout , stderr ):
123- assert type (output ) is bytes , type (output )
124- if output .strip ():
125- write ( output .strip () + b' \n ' )
126- write ( ' \n ' )
121+ for out in (stdout , stderr ):
122+ assert type (out ) is bytes , type (out )
123+ if out .strip ():
124+ output .write_line ( out . strip ())
125+ output . write_line ( )
127126
128127 return retcode
129128
@@ -147,13 +146,13 @@ def _compute_cols(hooks, verbose):
147146 return max (cols , 80 )
148147
149148
150- def _run_hooks (repo_hooks , args , write , environ ):
149+ def _run_hooks (repo_hooks , args , environ ):
151150 """Actually run the hooks."""
152151 skips = _get_skips (environ )
153152 cols = _compute_cols ([hook for _ , hook in repo_hooks ], args .verbose )
154153 retval = 0
155154 for repo , hook in repo_hooks :
156- retval |= _run_single_hook (hook , repo , args , write , skips , cols )
155+ retval |= _run_single_hook (hook , repo , args , skips , cols )
157156 return retval
158157
159158
@@ -177,10 +176,10 @@ def _has_unstaged_config(runner):
177176 return retcode == 1
178177
179178
180- def run (runner , args , write = sys_stdout_write_wrapper , environ = os .environ ):
179+ def run (runner , args , environ = os .environ ):
181180 no_stash = args .no_stash or args .all_files or bool (args .files )
182181 # Set up our logging handler
183- logger .addHandler (LoggingHandler (args .color , write = write ))
182+ logger .addHandler (LoggingHandler (args .color ))
184183 logger .setLevel (logging .INFO )
185184
186185 # Check if we have unresolved merge conflict files and fail fast.
@@ -220,7 +219,7 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
220219 if hook ['id' ] == args .hook
221220 ]
222221 if not repo_hooks :
223- write ('No hook with id `{}`\n ' .format (args .hook ))
222+ output . write_line ('No hook with id `{}`' .format (args .hook ))
224223 return 1
225224
226225 # Filter hooks for stages
@@ -229,4 +228,4 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
229228 if not hook ['stages' ] or args .hook_stage in hook ['stages' ]
230229 ]
231230
232- return _run_hooks (repo_hooks , args , write , environ )
231+ return _run_hooks (repo_hooks , args , environ )
0 commit comments