mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Reorganize output writing
This commit is contained in:
parent
1adfa24124
commit
0dda19f691
20 changed files with 202 additions and 142 deletions
|
|
@ -2,12 +2,12 @@ from __future__ import print_function
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from aspy.yaml import ordered_dump
|
||||
from aspy.yaml import ordered_load
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import output
|
||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||
from pre_commit.clientlib.validate_config import is_local_hooks
|
||||
from pre_commit.clientlib.validate_config import load_config
|
||||
|
|
@ -86,26 +86,23 @@ def autoupdate(runner):
|
|||
if is_local_hooks(repo_config):
|
||||
output_configs.append(repo_config)
|
||||
continue
|
||||
sys.stdout.write('Updating {}...'.format(repo_config['repo']))
|
||||
sys.stdout.flush()
|
||||
output.write('Updating {}...'.format(repo_config['repo']))
|
||||
try:
|
||||
new_repo_config = _update_repository(repo_config, runner)
|
||||
except RepositoryCannotBeUpdatedError as error:
|
||||
print(error.args[0])
|
||||
output.write_line(error.args[0])
|
||||
output_configs.append(repo_config)
|
||||
retv = 1
|
||||
continue
|
||||
|
||||
if new_repo_config['sha'] != repo_config['sha']:
|
||||
changed = True
|
||||
print(
|
||||
'updating {} -> {}.'.format(
|
||||
repo_config['sha'], new_repo_config['sha'],
|
||||
)
|
||||
)
|
||||
output.write_line('updating {} -> {}.'.format(
|
||||
repo_config['sha'], new_repo_config['sha'],
|
||||
))
|
||||
output_configs.append(new_repo_config)
|
||||
else:
|
||||
print('already up to date.')
|
||||
output.write_line('already up to date.')
|
||||
output_configs.append(repo_config)
|
||||
|
||||
if changed:
|
||||
|
|
|
|||
|
|
@ -3,11 +3,12 @@ from __future__ import unicode_literals
|
|||
|
||||
import os.path
|
||||
|
||||
from pre_commit import output
|
||||
from pre_commit.util import rmtree
|
||||
|
||||
|
||||
def clean(runner):
|
||||
if os.path.exists(runner.store.directory):
|
||||
rmtree(runner.store.directory)
|
||||
print('Cleaned {}.'.format(runner.store.directory))
|
||||
output.write_line('Cleaned {}.'.format(runner.store.directory))
|
||||
return 0
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import os
|
|||
import os.path
|
||||
import sys
|
||||
|
||||
from pre_commit import output
|
||||
from pre_commit.logging_handler import LoggingHandler
|
||||
from pre_commit.util import make_executable
|
||||
from pre_commit.util import mkdirp
|
||||
|
|
@ -61,7 +62,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
|
|||
if overwrite and os.path.exists(legacy_path):
|
||||
os.remove(legacy_path)
|
||||
elif os.path.exists(legacy_path):
|
||||
print(
|
||||
output.write_line(
|
||||
'Running in migration mode with existing hooks at {}\n'
|
||||
'Use -f to use only pre-commit.'.format(
|
||||
legacy_path,
|
||||
|
|
@ -83,7 +84,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
|
|||
pre_commit_file_obj.write(contents)
|
||||
make_executable(hook_path)
|
||||
|
||||
print('pre-commit installed at {}'.format(hook_path))
|
||||
output.write_line('pre-commit installed at {}'.format(hook_path))
|
||||
|
||||
# If they requested we install all of the hooks, do so.
|
||||
if hooks:
|
||||
|
|
@ -110,10 +111,10 @@ def uninstall(runner, hook_type='pre-commit'):
|
|||
return 0
|
||||
|
||||
os.remove(hook_path)
|
||||
print('{} uninstalled'.format(hook_type))
|
||||
output.write_line('{} uninstalled'.format(hook_type))
|
||||
|
||||
if os.path.exists(legacy_path):
|
||||
os.rename(legacy_path, hook_path)
|
||||
print('Restored previous hooks to {}'.format(hook_path))
|
||||
output.write_line('Restored previous hooks to {}'.format(hook_path))
|
||||
|
||||
return 0
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ import sys
|
|||
|
||||
from pre_commit import color
|
||||
from pre_commit import git
|
||||
from pre_commit import output
|
||||
from pre_commit.logging_handler import LoggingHandler
|
||||
from pre_commit.output import get_hook_message
|
||||
from pre_commit.output import sys_stdout_write_wrapper
|
||||
from pre_commit.staged_files_only import staged_files_only
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import noop_context
|
||||
|
|
@ -56,10 +56,10 @@ SKIPPED = 'Skipped'
|
|||
NO_FILES = '(no files to check)'
|
||||
|
||||
|
||||
def _run_single_hook(hook, repo, args, write, skips, cols):
|
||||
def _run_single_hook(hook, repo, args, skips, cols):
|
||||
filenames = get_filenames(args, hook['files'], hook['exclude'])
|
||||
if hook['id'] in skips:
|
||||
write(get_hook_message(
|
||||
output.write(get_hook_message(
|
||||
_hook_msg_start(hook, args.verbose),
|
||||
end_msg=SKIPPED,
|
||||
end_color=color.YELLOW,
|
||||
|
|
@ -68,7 +68,7 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
|
|||
))
|
||||
return 0
|
||||
elif not filenames and not hook['always_run']:
|
||||
write(get_hook_message(
|
||||
output.write(get_hook_message(
|
||||
_hook_msg_start(hook, args.verbose),
|
||||
postfix=NO_FILES,
|
||||
end_msg=SKIPPED,
|
||||
|
|
@ -80,7 +80,7 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
|
|||
|
||||
# Print the hook and the dots first in case the hook takes hella long to
|
||||
# run.
|
||||
write(get_hook_message(
|
||||
output.write(get_hook_message(
|
||||
_hook_msg_start(hook, args.verbose), end_len=6, cols=cols,
|
||||
))
|
||||
sys.stdout.flush()
|
||||
|
|
@ -104,26 +104,25 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
|
|||
print_color = color.GREEN
|
||||
pass_fail = 'Passed'
|
||||
|
||||
write(color.format_color(pass_fail, print_color, args.color) + '\n')
|
||||
output.write_line(color.format_color(pass_fail, print_color, args.color))
|
||||
|
||||
if (stdout or stderr or file_modifications) and (retcode or args.verbose):
|
||||
write('hookid: {}\n'.format(hook['id']))
|
||||
write('\n')
|
||||
output.write_line('hookid: {}\n'.format(hook['id']))
|
||||
|
||||
# Print a message if failing due to file modifications
|
||||
if file_modifications:
|
||||
write('Files were modified by this hook.')
|
||||
output.write('Files were modified by this hook.')
|
||||
|
||||
if stdout or stderr:
|
||||
write(' Additional output:\n')
|
||||
output.write_line(' Additional output:')
|
||||
|
||||
write('\n')
|
||||
output.write_line()
|
||||
|
||||
for output in (stdout, stderr):
|
||||
assert type(output) is bytes, type(output)
|
||||
if output.strip():
|
||||
write(output.strip() + b'\n')
|
||||
write('\n')
|
||||
for out in (stdout, stderr):
|
||||
assert type(out) is bytes, type(out)
|
||||
if out.strip():
|
||||
output.write_line(out.strip())
|
||||
output.write_line()
|
||||
|
||||
return retcode
|
||||
|
||||
|
|
@ -147,13 +146,13 @@ def _compute_cols(hooks, verbose):
|
|||
return max(cols, 80)
|
||||
|
||||
|
||||
def _run_hooks(repo_hooks, args, write, environ):
|
||||
def _run_hooks(repo_hooks, args, environ):
|
||||
"""Actually run the hooks."""
|
||||
skips = _get_skips(environ)
|
||||
cols = _compute_cols([hook for _, hook in repo_hooks], args.verbose)
|
||||
retval = 0
|
||||
for repo, hook in repo_hooks:
|
||||
retval |= _run_single_hook(hook, repo, args, write, skips, cols)
|
||||
retval |= _run_single_hook(hook, repo, args, skips, cols)
|
||||
return retval
|
||||
|
||||
|
||||
|
|
@ -177,10 +176,10 @@ def _has_unstaged_config(runner):
|
|||
return retcode == 1
|
||||
|
||||
|
||||
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
||||
def run(runner, args, environ=os.environ):
|
||||
no_stash = args.no_stash or args.all_files or bool(args.files)
|
||||
# Set up our logging handler
|
||||
logger.addHandler(LoggingHandler(args.color, write=write))
|
||||
logger.addHandler(LoggingHandler(args.color))
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
# Check if we have unresolved merge conflict files and fail fast.
|
||||
|
|
@ -220,7 +219,7 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
|||
if hook['id'] == args.hook
|
||||
]
|
||||
if not repo_hooks:
|
||||
write('No hook with id `{}`\n'.format(args.hook))
|
||||
output.write_line('No hook with id `{}`'.format(args.hook))
|
||||
return 1
|
||||
|
||||
# Filter hooks for stages
|
||||
|
|
@ -229,4 +228,4 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
|||
if not hook['stages'] or args.hook_stage in hook['stages']
|
||||
]
|
||||
|
||||
return _run_hooks(repo_hooks, args, write, environ)
|
||||
return _run_hooks(repo_hooks, args, environ)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue