More miscellaneous cleanup

This commit is contained in:
Anthony Sottile 2020-01-12 21:17:59 -08:00
parent 489d9f9926
commit df40e862f4
33 changed files with 209 additions and 296 deletions

View file

@ -29,7 +29,5 @@ def init_templatedir(
dest = os.path.realpath(directory)
if configured_path != dest:
logger.warning('`init.templateDir` not set to the target directory')
logger.warning(
f'maybe `git config --global init.templateDir {dest}`?',
)
logger.warning(f'maybe `git config --global init.templateDir {dest}`?')
return 0

View file

@ -28,18 +28,17 @@ def _migrate_map(contents: str) -> str:
# If they are using the "default" flow style of yaml, this operation
# will yield a valid configuration
try:
trial_contents = header + 'repos:\n' + rest
trial_contents = f'{header}repos:\n{rest}'
ordered_load(trial_contents)
contents = trial_contents
except yaml.YAMLError:
contents = header + 'repos:\n' + _indent(rest)
contents = f'{header}repos:\n{_indent(rest)}'
return contents
def _migrate_sha_to_rev(contents: str) -> str:
reg = re.compile(r'(\n\s+)sha:')
return reg.sub(r'\1rev:', contents)
return re.sub(r'(\n\s+)sha:', r'\1rev:', contents)
def migrate_config(config_file: str, quiet: bool = False) -> int:

View file

@ -20,7 +20,6 @@ from pre_commit import color
from pre_commit import git
from pre_commit import output
from pre_commit.clientlib import load_config
from pre_commit.output import get_hook_message
from pre_commit.repository import all_hooks
from pre_commit.repository import Hook
from pre_commit.repository import install_hook_envs
@ -33,6 +32,25 @@ from pre_commit.util import EnvironT
logger = logging.getLogger('pre_commit')
def _start_msg(*, start: str, cols: int, end_len: int) -> str:
dots = '.' * (cols - len(start) - end_len - 1)
return f'{start}{dots}'
def _full_msg(
*,
start: str,
cols: int,
end_msg: str,
end_color: str,
use_color: bool,
postfix: str = '',
) -> str:
dots = '.' * (cols - len(start) - len(postfix) - len(end_msg) - 1)
end = color.format_color(end_msg, end_color, use_color)
return f'{start}{dots}{postfix}{end}\n'
def filter_by_include_exclude(
names: Collection[str],
include: str,
@ -106,8 +124,8 @@ def _run_single_hook(
if hook.id in skips or hook.alias in skips:
output.write(
get_hook_message(
hook.name,
_full_msg(
start=hook.name,
end_msg=SKIPPED,
end_color=color.YELLOW,
use_color=use_color,
@ -120,8 +138,8 @@ def _run_single_hook(
out = b''
elif not filenames and not hook.always_run:
output.write(
get_hook_message(
hook.name,
_full_msg(
start=hook.name,
postfix=NO_FILES,
end_msg=SKIPPED,
end_color=color.TURQUOISE,
@ -135,7 +153,7 @@ def _run_single_hook(
out = b''
else:
# print hook and dots first in case the hook takes a while to run
output.write(get_hook_message(hook.name, end_len=6, cols=cols))
output.write(_start_msg(start=hook.name, end_len=6, cols=cols))
diff_cmd = ('git', 'diff', '--no-ext-diff')
diff_before = cmd_output_b(*diff_cmd, retcode=None)
@ -218,9 +236,8 @@ def _run_hooks(
"""Actually run the hooks."""
skips = _get_skips(environ)
cols = _compute_cols(hooks)
filenames = _all_filenames(args)
filenames = filter_by_include_exclude(
filenames, config['files'], config['exclude'],
_all_filenames(args), config['files'], config['exclude'],
)
classifier = Classifier(filenames)
retval = 0

View file

@ -1,6 +1,7 @@
import argparse
import logging
import os.path
from typing import Optional
from typing import Tuple
from aspy.yaml import ordered_dump
@ -18,9 +19,9 @@ from pre_commit.xargs import xargs
logger = logging.getLogger(__name__)
def _repo_ref(tmpdir: str, repo: str, ref: str) -> Tuple[str, str]:
def _repo_ref(tmpdir: str, repo: str, ref: Optional[str]) -> Tuple[str, str]:
# if `ref` is explicitly passed, use it
if ref:
if ref is not None:
return repo, ref
ref = git.head_rev(repo)