Prepend hook name to each output line

This commit is contained in:
elagil 2022-03-13 11:51:35 +01:00
parent d525928665
commit 8861b85dae
4 changed files with 15 additions and 2 deletions

View file

@ -79,6 +79,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
cfgv.Optional('require_serial', cfgv.check_bool, False), cfgv.Optional('require_serial', cfgv.check_bool, False),
cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []), cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
cfgv.Optional('verbose', cfgv.check_bool, False), cfgv.Optional('verbose', cfgv.check_bool, False),
cfgv.Optional('prepend_name', cfgv.check_bool, False),
) )
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT) MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)

View file

@ -218,9 +218,19 @@ def _run_single_hook(
if files_modified: if files_modified:
_subtle_line('- files were modified by this hook', use_color) _subtle_line('- files were modified by this hook', use_color)
if out.strip(): hook_output = out.strip()
if hook_output:
if hook.prepend_name:
# Prepends the hook name to each line of its console output
hook_output = b'\n'.join(
[
bytes(f'{hook.name}: ', encoding='utf8') + line
for line in hook_output.split(b'\n')
],
)
output.write_line() output.write_line()
output.write_line_b(out.strip(), logfile_name=hook.log_file) output.write_line_b(hook_output, logfile_name=hook.log_file)
output.write_line() output.write_line()
return files_modified or bool(retcode), diff_after return files_modified or bool(retcode), diff_after

View file

@ -35,6 +35,7 @@ class Hook(NamedTuple):
minimum_pre_commit_version: str minimum_pre_commit_version: str
require_serial: bool require_serial: bool
stages: Sequence[str] stages: Sequence[str]
prepend_name: bool
verbose: bool verbose: bool
@property @property

View file

@ -1005,6 +1005,7 @@ def test_manifest_hooks(tempdir_factory, store):
types_or=[], types_or=[],
verbose=False, verbose=False,
fail_fast=False, fail_fast=False,
prepend_name=False,
) )