mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 20:40:08 -06:00
Adds support for 'log_file' in hook config
Specify a filename on a per hook basis and pre-commit will write the STDOUT and STDERR of that hook into the file. Useful for CI. Resolves #499.
This commit is contained in:
@@ -53,6 +53,7 @@ MANIFEST_HOOK_DICT = schema.Map(
|
||||
'^$',
|
||||
),
|
||||
schema.Optional('language_version', schema.check_string, 'default'),
|
||||
schema.OptionalNoDefault('log_file', schema.check_string),
|
||||
schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),
|
||||
schema.Optional('stages', schema.check_array(schema.check_string), []),
|
||||
)
|
||||
|
||||
@@ -121,7 +121,10 @@ def _run_single_hook(hook, repo, args, skips, cols):
|
||||
for out in (stdout, stderr):
|
||||
assert type(out) is bytes, type(out)
|
||||
if out.strip():
|
||||
output.write_line(out.strip())
|
||||
output.write_line(
|
||||
out.strip(),
|
||||
logfile_name=hook.get('log_file'),
|
||||
)
|
||||
output.write_line()
|
||||
|
||||
return retcode
|
||||
|
||||
@@ -71,8 +71,17 @@ def write(s, stream=stdout_byte_stream):
|
||||
stream.flush()
|
||||
|
||||
|
||||
def write_line(s=None, stream=stdout_byte_stream):
|
||||
if s is not None:
|
||||
stream.write(five.to_bytes(s))
|
||||
stream.write(b'\n')
|
||||
stream.flush()
|
||||
def write_line(s=None, stream=stdout_byte_stream, logfile_name=None):
|
||||
def output_streams():
|
||||
yield stream
|
||||
try:
|
||||
with open(logfile_name, 'ab') as logfile:
|
||||
yield logfile
|
||||
except (TypeError, IOError):
|
||||
pass
|
||||
|
||||
for output_stream in output_streams():
|
||||
if s is not None:
|
||||
output_stream.write(five.to_bytes(s))
|
||||
output_stream.write(b'\n')
|
||||
output_stream.flush()
|
||||
|
||||
Reference in New Issue
Block a user