Simplify a few things

This commit is contained in:
Anthony Sottile 2015-01-14 19:21:32 -08:00
parent b707cbba06
commit 931c69b3fa
7 changed files with 29 additions and 97 deletions

View file

@ -9,6 +9,7 @@ import stat
import sys
from pre_commit.logging_handler import LoggingHandler
from pre_commit.util import resource_filename
logger = logging.getLogger('pre_commit')
@ -41,19 +42,10 @@ def make_executable(filename):
)
def get_hook_path(runner, hook_type):
if hook_type == 'pre-commit':
hook_path = runner.pre_commit_path
legacy_path = runner.pre_commit_legacy_path
else:
hook_path = runner.pre_push_path
legacy_path = runner.pre_push_legacy_path
return hook_path, legacy_path
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
"""Install the pre-commit hooks."""
hook_path, legacy_path = get_hook_path(runner, hook_type)
hook_path = runner.get_hook_path(hook_type)
legacy_path = hook_path + '.legacy'
# If we have an existing hook, move it to pre-commit.legacy
if (
@ -76,12 +68,12 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
with io.open(hook_path, 'w') as pre_commit_file_obj:
if hook_type == 'pre-push':
with io.open(runner.pre_push_template) as fp:
with io.open(resource_filename('pre-push-tmpl')) as fp:
pre_push_contents = fp.read()
else:
pre_push_contents = ''
contents = io.open(runner.pre_template).read().format(
contents = io.open(resource_filename('hook-tmpl')).read().format(
sys_executable=sys.executable,
hook_type=hook_type,
pre_push=pre_push_contents,
@ -104,7 +96,8 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
def uninstall(runner, hook_type='pre-commit'):
"""Uninstall the pre-commit hooks."""
hook_path, legacy_path = get_hook_path(runner, hook_type)
hook_path = runner.get_hook_path(hook_type)
legacy_path = hook_path + '.legacy'
# If our file doesn't exist or it isn't ours, gtfo.
if (
not os.path.exists(hook_path) or (

View file

@ -50,18 +50,16 @@ def _print_user_skipped(hook, write, args):
def get_changed_files(new, old):
changed_files = cmd_output(
return cmd_output(
'git', 'diff', '--name-only', '{0}..{1}'.format(old, new),
)[1].splitlines()
for f in changed_files:
if f:
yield f
def _run_single_hook(runner, repository, hook, args, write, skips=set()):
if args.origin and args.source:
get_filenames = git.get_files_matching(
lambda: get_changed_files(args.origin, args.source))
lambda: get_changed_files(args.origin, args.source),
)
elif args.files:
get_filenames = git.get_files_matching(lambda: args.files)
elif args.all_files:
@ -150,9 +148,8 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
if _has_unmerged_paths(runner):
logger.error('Unmerged files. Resolve before committing.')
return 1
if (args.source and not args.origin) or \
(args.origin and not args.source):
logger.error('--origin and --source depend on each other.')
if bool(args.source) != bool(args.origin):
logger.error('Specify both --origin and --source.')
return 1
# Don't stash if specified or files are specified