@@ -147,6 +147,7 @@ def _run_single_hook(
147147 diff_before : bytes ,
148148 verbose : bool ,
149149 use_color : bool ,
150+ stage : str ,
150151) -> tuple [bool , bytes ]:
151152 filenames = tuple (classifier .filenames_for_hook (hook ))
152153
@@ -172,7 +173,7 @@ def _run_single_hook(
172173 filenames = ()
173174 time_before = time .monotonic ()
174175 language = languages [hook .language ]
175- with monitor .trace (f'precommit.hook.{ hook .name } ' ) as trace :
176+ with monitor .trace (f'precommit.{ stage } . hook.{ hook .name } ' ) as trace :
176177 with language .in_env (hook .prefix , hook .language_version ):
177178 retcode , out = language .run_hook (
178179 hook .prefix ,
@@ -292,6 +293,7 @@ def _run_hooks(
292293 current_retval , prior_diff = _run_single_hook (
293294 classifier , hook , skips , cols , prior_diff ,
294295 verbose = args .verbose , use_color = args .color ,
296+ stage = args .hook_stage ,
295297 )
296298 retval |= current_retval
297299 if current_retval and (config ['fail_fast' ] or hook .fail_fast ):
@@ -330,11 +332,11 @@ def _has_unstaged_config(config_file: str) -> bool:
330332 return retcode == 1
331333
332334
333- def _run_inner (
335+ def run (
334336 config_file : str ,
335337 store : Store ,
336338 args : argparse .Namespace ,
337- environ : MutableMapping [str , str ],
339+ environ : MutableMapping [str , str ] = os . environ ,
338340) -> int :
339341 stash = not args .all_files and not args .files
340342
@@ -412,6 +414,11 @@ def _run_inner(
412414 environ ['PRE_COMMIT' ] = '1'
413415
414416 with contextlib .ExitStack () as exit_stack :
417+ # Metrics should get reported as the last thing that happens.
418+ exit_stack .callback (monitor .report_metrics )
419+
420+ # Start the timing trace.
421+ trace = exit_stack .enter_context (monitor .trace (f'precommit.{ args .hook_stage } ' ))
415422 if stash :
416423 exit_stack .enter_context (staged_files_only (store .directory ))
417424
@@ -428,6 +435,7 @@ def _run_inner(
428435 output .write_line (
429436 f'No hook with id `{ args .hook } ` in stage `{ args .hook_stage } `' ,
430437 )
438+ trace .set_success (False )
431439 return 1
432440
433441 skips = _get_skips (environ )
@@ -438,22 +446,10 @@ def _run_inner(
438446 ]
439447 install_hook_envs (to_install , store )
440448
441- return _run_hooks (config , hooks , skips , args )
449+ retval = _run_hooks (config , hooks , skips , args )
450+ trace .set_success (retval == 0 )
451+ return retval
442452
443453 # https://github.com/python/mypy/issues/7726
444454 raise AssertionError ('unreachable' )
445455
446- def run (
447- config_file : str ,
448- store : Store ,
449- args : argparse .Namespace ,
450- environ : MutableMapping [str , str ] = os .environ ,
451- ) -> int :
452- try :
453- with monitor .trace ('precommit' ) as trace :
454- retval = _run_inner (config_file , store , args , environ )
455- trace .set_success (retval == 0 )
456- return retval
457- finally :
458- monitor .report_metrics ()
459-
0 commit comments