@@ -30,25 +30,6 @@ def _hook_msg_start(hook, verbose):
3030 )
3131
3232
33- def _print_no_files_skipped (hook , write , args ):
34- write (get_hook_message (
35- _hook_msg_start (hook , args .verbose ),
36- postfix = '(no files to check) ' ,
37- end_msg = 'Skipped' ,
38- end_color = color .TURQUOISE ,
39- use_color = args .color ,
40- ))
41-
42-
43- def _print_user_skipped (hook , write , args ):
44- write (get_hook_message (
45- _hook_msg_start (hook , args .verbose ),
46- end_msg = 'Skipped' ,
47- end_color = color .YELLOW ,
48- use_color = args .color ,
49- ))
50-
51-
5233def get_changed_files (new , old ):
5334 return cmd_output (
5435 'git' , 'diff' , '--name-only' , '{0}...{1}' .format (old , new ),
@@ -71,18 +52,37 @@ def get_filenames(args, include_expr, exclude_expr):
7152 return getter (include_expr , exclude_expr )
7253
7354
74- def _run_single_hook (hook , repo , args , write , skips = frozenset ()):
55+ SKIPPED = 'Skipped'
56+ NO_FILES = '(no files to check)'
57+
58+
59+ def _run_single_hook (hook , repo , args , write , skips , cols ):
7560 filenames = get_filenames (args , hook ['files' ], hook ['exclude' ])
7661 if hook ['id' ] in skips :
77- _print_user_skipped (hook , write , args )
62+ write (get_hook_message (
63+ _hook_msg_start (hook , args .verbose ),
64+ end_msg = SKIPPED ,
65+ end_color = color .YELLOW ,
66+ use_color = args .color ,
67+ cols = cols ,
68+ ))
7869 return 0
7970 elif not filenames and not hook ['always_run' ]:
80- _print_no_files_skipped (hook , write , args )
71+ write (get_hook_message (
72+ _hook_msg_start (hook , args .verbose ),
73+ postfix = NO_FILES ,
74+ end_msg = SKIPPED ,
75+ end_color = color .TURQUOISE ,
76+ use_color = args .color ,
77+ cols = cols ,
78+ ))
8179 return 0
8280
8381 # Print the hook and the dots first in case the hook takes hella long to
8482 # run.
85- write (get_hook_message (_hook_msg_start (hook , args .verbose ), end_len = 6 ))
83+ write (get_hook_message (
84+ _hook_msg_start (hook , args .verbose ), end_len = 6 , cols = cols ,
85+ ))
8686 sys .stdout .flush ()
8787
8888 diff_before = cmd_output ('git' , 'diff' , retcode = None , encoding = None )
@@ -128,12 +128,32 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
128128 return retcode
129129
130130
131+ def _compute_cols (hooks , verbose ):
132+ """Compute the number of columns to display hook messages. The widest
133+ that will be displayed is in the no files skipped case:
134+
135+ Hook name...(no files to check) Skipped
136+
137+ or in the verbose case
138+
139+ Hook name [hookid]...(no files to check) Skipped
140+ """
141+ if hooks :
142+ name_len = max (len (_hook_msg_start (hook , verbose )) for hook in hooks )
143+ else :
144+ name_len = 0
145+
146+ cols = name_len + 3 + len (NO_FILES ) + 1 + len (SKIPPED )
147+ return max (cols , 80 )
148+
149+
131150def _run_hooks (repo_hooks , args , write , environ ):
132151 """Actually run the hooks."""
133152 skips = _get_skips (environ )
153+ cols = _compute_cols ([hook for _ , hook in repo_hooks ], args .verbose )
134154 retval = 0
135155 for repo , hook in repo_hooks :
136- retval |= _run_single_hook (hook , repo , args , write , skips )
156+ retval |= _run_single_hook (hook , repo , args , write , skips , cols )
137157 return retval
138158
139159
0 commit comments