44import os
55import re
66import subprocess
7- import sys
87
98from identify .identify import tags_from_path
109
@@ -71,15 +70,15 @@ def _get_skips(environ):
7170 return {skip .strip () for skip in skips .split (',' ) if skip .strip ()}
7271
7372
74- def _hook_msg_start (hook , verbose ):
75- return '{}{}' .format ('[{}] ' .format (hook .id ) if verbose else '' , hook .name )
76-
77-
7873SKIPPED = 'Skipped'
7974NO_FILES = '(no files to check)'
8075
8176
82- def _run_single_hook (classifier , hook , args , skips , cols , use_color ):
77+ def _subtle_line (s , use_color ):
78+ output .write_line (color .format_color (s , color .SUBTLE , use_color ))
79+
80+
81+ def _run_single_hook (classifier , hook , skips , cols , verbose , use_color ):
8382 filenames = classifier .filenames_for_hook (hook )
8483
8584 if hook .language == 'pcre' :
@@ -93,92 +92,78 @@ def _run_single_hook(classifier, hook, args, skips, cols, use_color):
9392 if hook .id in skips or hook .alias in skips :
9493 output .write (
9594 get_hook_message (
96- _hook_msg_start ( hook , args . verbose ) ,
95+ hook . name ,
9796 end_msg = SKIPPED ,
9897 end_color = color .YELLOW ,
99- use_color = args . color ,
98+ use_color = use_color ,
10099 cols = cols ,
101100 ),
102101 )
103- return 0
102+ retcode = 0
103+ files_modified = False
104+ out = b''
104105 elif not filenames and not hook .always_run :
105106 output .write (
106107 get_hook_message (
107- _hook_msg_start ( hook , args . verbose ) ,
108+ hook . name ,
108109 postfix = NO_FILES ,
109110 end_msg = SKIPPED ,
110111 end_color = color .TURQUOISE ,
111- use_color = args . color ,
112+ use_color = use_color ,
112113 cols = cols ,
113114 ),
114115 )
115- return 0
116-
117- # Print the hook and the dots first in case the hook takes hella long to
118- # run.
119- output .write (
120- get_hook_message (
121- _hook_msg_start (hook , args .verbose ), end_len = 6 , cols = cols ,
122- ),
123- )
124- sys .stdout .flush ()
116+ retcode = 0
117+ files_modified = False
118+ out = b''
119+ else :
120+ # print hook and dots first in case the hook takes a while to run
121+ output .write (get_hook_message (hook .name , end_len = 6 , cols = cols ))
125122
126- diff_before = cmd_output_b ('git' , 'diff' , '--no-ext-diff' , retcode = None )
127- filenames = tuple (filenames ) if hook .pass_filenames else ()
128- retcode , out = hook .run (filenames , use_color )
129- diff_after = cmd_output_b ('git' , 'diff' , '--no-ext-diff' , retcode = None )
123+ diff_cmd = ('git' , 'diff' , '--no-ext-diff' )
124+ diff_before = cmd_output_b (* diff_cmd , retcode = None )
125+ filenames = tuple (filenames ) if hook .pass_filenames else ()
126+ retcode , out = hook .run (filenames , use_color )
127+ diff_after = cmd_output_b (* diff_cmd , retcode = None )
130128
131- file_modifications = diff_before != diff_after
129+ # if the hook makes changes, fail the commit
130+ files_modified = diff_before != diff_after
132131
133- # If the hook makes changes, fail the commit
134- if file_modifications :
135- retcode = 1
132+ if retcode or files_modified :
133+ print_color = color .RED
134+ status = 'Failed'
135+ else :
136+ print_color = color .GREEN
137+ status = 'Passed'
136138
137- if retcode :
138- retcode = 1
139- print_color = color .RED
140- pass_fail = 'Failed'
141- else :
142- retcode = 0
143- print_color = color .GREEN
144- pass_fail = 'Passed'
139+ output .write_line (color .format_color (status , print_color , use_color ))
145140
146- output .write_line (color .format_color (pass_fail , print_color , args .color ))
141+ if verbose or hook .verbose or retcode or files_modified :
142+ _subtle_line ('- hook id: {}' .format (hook .id ), use_color )
147143
148- if (
149- (out or file_modifications ) and
150- (retcode or args .verbose or hook .verbose )
151- ):
152- output .write_line ('hookid: {}\n ' .format (hook .id ))
144+ if retcode :
145+ _subtle_line ('- exit code: {}' .format (retcode ), use_color )
153146
154147 # Print a message if failing due to file modifications
155- if file_modifications :
156- output .write ('Files were modified by this hook.' )
157-
158- if out :
159- output .write_line (' Additional output:' )
160-
161- output .write_line ()
148+ if files_modified :
149+ _subtle_line ('- files were modified by this hook' , use_color )
162150
163151 if out .strip ():
152+ output .write_line ()
164153 output .write_line (out .strip (), logfile_name = hook .log_file )
165- output .write_line ()
154+ output .write_line ()
166155
167- return retcode
156+ return files_modified or bool ( retcode )
168157
169158
170- def _compute_cols (hooks , verbose ):
159+ def _compute_cols (hooks ):
171160 """Compute the number of columns to display hook messages. The widest
172161 that will be displayed is in the no files skipped case:
173162
174163 Hook name...(no files to check) Skipped
175-
176- or in the verbose case
177-
178- Hook name [hookid]...(no files to check) Skipped
179164 """
180165 if hooks :
181- name_len = max (len (_hook_msg_start ( hook , verbose ) ) for hook in hooks )
166+ name_len = max (len (hook . name ) for hook in hooks )
182167 else :
183168 name_len = 0
184169
@@ -204,7 +189,7 @@ def _all_filenames(args):
204189def _run_hooks (config , hooks , args , environ ):
205190 """Actually run the hooks."""
206191 skips = _get_skips (environ )
207- cols = _compute_cols (hooks , args . verbose )
192+ cols = _compute_cols (hooks )
208193 filenames = _all_filenames (args )
209194 filenames = filter_by_include_exclude (
210195 filenames , config ['files' ], config ['exclude' ],
@@ -213,7 +198,8 @@ def _run_hooks(config, hooks, args, environ):
213198 retval = 0
214199 for hook in hooks :
215200 retval |= _run_single_hook (
216- classifier , hook , args , skips , cols , args .color ,
201+ classifier , hook , skips , cols ,
202+ verbose = args .verbose , use_color = args .color ,
217203 )
218204 if retval and config ['fail_fast' ]:
219205 break
0 commit comments