Fix ordering of mixed stdout / stderr printing

This commit is contained in:
Anthony Sottile
2019-10-12 13:35:04 -07:00
parent 183c8cbb3a
commit 2633d38a63
6 changed files with 55 additions and 32 deletions

View File

@@ -118,9 +118,7 @@ def _run_single_hook(classifier, hook, args, skips, cols):
sys.stdout.flush()
diff_before = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
retcode, stdout, stderr = hook.run(
tuple(filenames) if hook.pass_filenames else (),
)
retcode, out = hook.run(tuple(filenames) if hook.pass_filenames else ())
diff_after = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
file_modifications = diff_before != diff_after
@@ -141,7 +139,7 @@ def _run_single_hook(classifier, hook, args, skips, cols):
output.write_line(color.format_color(pass_fail, print_color, args.color))
if (
(stdout or stderr or file_modifications) and
(out or file_modifications) and
(retcode or args.verbose or hook.verbose)
):
output.write_line('hookid: {}\n'.format(hook.id))
@@ -150,15 +148,13 @@ def _run_single_hook(classifier, hook, args, skips, cols):
if file_modifications:
output.write('Files were modified by this hook.')
if stdout or stderr:
if out:
output.write_line(' Additional output:')
output.write_line()
for out in (stdout, stderr):
assert type(out) is bytes, type(out)
if out.strip():
output.write_line(out.strip(), logfile_name=hook.log_file)
if out.strip():
output.write_line(out.strip(), logfile_name=hook.log_file)
output.write_line()
return retcode

View File

@@ -6,6 +6,7 @@ import concurrent.futures
import contextlib
import math
import os
import subprocess
import sys
import six
@@ -112,23 +113,24 @@ def xargs(cmd, varargs, **kwargs):
max_length = kwargs.pop('_max_length', _get_platform_max_length())
retcode = 0
stdout = b''
stderr = b''
try:
cmd = parse_shebang.normalize_cmd(cmd)
except parse_shebang.ExecutableNotFoundError as e:
return e.to_output()
return e.to_output()[:2]
partitions = partition(cmd, varargs, target_concurrency, max_length)
def run_cmd_partition(run_cmd):
return cmd_output_b(*run_cmd, retcode=None, **kwargs)
return cmd_output_b(
*run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs
)
threads = min(len(partitions), target_concurrency)
with _thread_mapper(threads) as thread_map:
results = thread_map(run_cmd_partition, partitions)
for proc_retcode, proc_out, proc_err in results:
for proc_retcode, proc_out, _ in results:
# This is *slightly* too clever so I'll explain it.
# First the xor boolean table:
# T | F |
@@ -141,6 +143,5 @@ def xargs(cmd, varargs, **kwargs):
# code. Otherwise, the returncode is unchanged.
retcode |= bool(proc_retcode) ^ negate
stdout += proc_out
stderr += proc_err
return retcode, stdout, stderr
return retcode, stdout