Adds support for 'log_file' in hook config · pre-commit/pre-commit@f2644a4 · GitHub
Skip to content

Commit f2644a4

Browse files
Alex HuttonAlex Hutton
authored andcommitted
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.
1 parent 5d43b05 commit f2644a4

6 files changed

Lines changed: 59 additions & 6 deletions

File tree

‎pre_commit/clientlib.py‎

Lines changed: 1 addition & 0 deletions

‎pre_commit/commands/run.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def _run_single_hook(hook, repo, args, skips, cols):
121121
for out in (stdout, stderr):
122122
assert type(out) is bytes, type(out)
123123
if out.strip():
124-
output.write_line(out.strip())
124+
output.write_line(
125+
out.strip(),
126+
logfile_name=hook.get('log_file'),
127+
)
125128
output.write_line()
126129

127130
return retcode

‎pre_commit/output.py‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ def write(s, stream=stdout_byte_stream):
7171
stream.flush()
7272

7373

74-
def write_line(s=None, stream=stdout_byte_stream):
75-
if s is not None:
76-
stream.write(five.to_bytes(s))
77-
stream.write(b'\n')
78-
stream.flush()
74+
def write_line(s=None, stream=stdout_byte_stream, logfile_name=None):
75+
def output_streams():
76+
yield stream
77+
try:
78+
with open(logfile_name, 'ab') as logfile:
79+
yield logfile
80+
except (TypeError, IOError):
81+
pass
82+
83+
for output_stream in output_streams():
84+
if s is not None:
85+
output_stream.write(five.to_bytes(s))
86+
output_stream.write(b'\n')
87+
output_stream.flush()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- id: logfile test hook
2+
name: Logfile test hook
3+
entry: bin/hook.sh
4+
language: script
5+
files: .
6+
log_file: test.log
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
echo "This is STDOUT output"
3+
echo "This is STDERR output" 1>&2
4+
5+
exit 1

‎tests/commands/run_test.py‎

Lines changed: 29 additions & 0 deletions

0 commit comments

Comments
 (0)