Skip to content

Commit 840a55b

Browse files
committed
Fixup log_file commit
1 parent 71e500a commit 840a55b

5 files changed

Lines changed: 18 additions & 19 deletions

File tree

.coveragerc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ source = .
44
omit =
55
.tox/*
66
/usr/*
7-
*/tmp*
87
setup.py
98
# Don't complain if non-runnable code isn't run
109
*/__main__.py

pre_commit/clientlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _make_argparser(filenames_help):
5353
'^$',
5454
),
5555
schema.Optional('language_version', schema.check_string, 'default'),
56-
schema.OptionalNoDefault('log_file', schema.check_string),
56+
schema.Optional('log_file', schema.check_string, ''),
5757
schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),
5858
schema.Optional('stages', schema.check_array(schema.check_string), []),
5959
)

pre_commit/commands/run.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ 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(
125-
out.strip(),
126-
logfile_name=hook.get('log_file'),
127-
)
124+
output.write_line(out.strip(), logfile_name=hook['log_file'])
128125
output.write_line()
129126

130127
return retcode

pre_commit/output.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from pre_commit import color
66
from pre_commit import five
7+
from pre_commit.util import noop_context
78

89

910
def get_hook_message(
@@ -72,16 +73,16 @@ def write(s, stream=stdout_byte_stream):
7273

7374

7475
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()
76+
output_streams = [stream]
77+
if logfile_name:
78+
ctx = open(logfile_name, 'ab')
79+
output_streams.append(ctx)
80+
else:
81+
ctx = noop_context()
82+
83+
with ctx:
84+
for output_stream in output_streams:
85+
if s is not None:
86+
output_stream.write(five.to_bytes(s))
87+
output_stream.write(b'\n')
88+
output_stream.flush()

tests/manifest_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_manifest_contents(manifest):
2929
'id': 'bash_hook',
3030
'language': 'script',
3131
'language_version': 'default',
32+
'log_file': '',
3233
'minimum_pre_commit_version': '0',
3334
'name': 'Bash hook',
3435
'stages': [],
@@ -47,6 +48,7 @@ def test_hooks(manifest):
4748
'id': 'bash_hook',
4849
'language': 'script',
4950
'language_version': 'default',
51+
'log_file': '',
5052
'minimum_pre_commit_version': '0',
5153
'name': 'Bash hook',
5254
'stages': [],

0 commit comments

Comments
 (0)