Make pre_commit also support pre-push hook

This commit is contained in:
dongweiming
2015-01-11 22:40:35 +08:00
committed by Anthony Sottile
parent d2b11a0c50
commit b707cbba06
10 changed files with 227 additions and 42 deletions

View File

@@ -9,7 +9,6 @@ import stat
import sys
from pre_commit.logging_handler import LoggingHandler
from pre_commit.util import resource_filename
logger = logging.getLogger('pre_commit')
@@ -42,37 +41,55 @@ def make_executable(filename):
)
def install(runner, overwrite=False, hooks=False):
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."""
pre_commit_file = resource_filename('pre-commit-hook')
hook_path, legacy_path = get_hook_path(runner, hook_type)
# If we have an existing hook, move it to pre-commit.legacy
if (
os.path.exists(runner.pre_commit_path) and
not is_our_pre_commit(runner.pre_commit_path) and
not is_previous_pre_commit(runner.pre_commit_path)
os.path.exists(hook_path) and
not is_our_pre_commit(hook_path) and
not is_previous_pre_commit(hook_path)
):
os.rename(runner.pre_commit_path, runner.pre_commit_legacy_path)
os.rename(hook_path, legacy_path)
# If we specify overwrite, we simply delete the legacy file
if overwrite and os.path.exists(runner.pre_commit_legacy_path):
os.remove(runner.pre_commit_legacy_path)
elif os.path.exists(runner.pre_commit_legacy_path):
if overwrite and os.path.exists(legacy_path):
os.remove(legacy_path)
elif os.path.exists(legacy_path):
print(
'Running in migration mode with existing hooks at {0}\n'
'Use -f to use only pre-commit.'.format(
runner.pre_commit_legacy_path,
legacy_path,
)
)
with io.open(runner.pre_commit_path, 'w') as pre_commit_file_obj:
contents = io.open(pre_commit_file).read().format(
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:
pre_push_contents = fp.read()
else:
pre_push_contents = ''
contents = io.open(runner.pre_template).read().format(
sys_executable=sys.executable,
hook_type=hook_type,
pre_push=pre_push_contents,
)
pre_commit_file_obj.write(contents)
make_executable(runner.pre_commit_path)
make_executable(hook_path)
print('pre-commit installed at {0}'.format(runner.pre_commit_path))
print('pre-commit installed at {0}'.format(hook_path))
# If they requested we install all of the hooks, do so.
if hooks:
@@ -85,22 +102,23 @@ def install(runner, overwrite=False, hooks=False):
return 0
def uninstall(runner):
def uninstall(runner, hook_type='pre-commit'):
"""Uninstall the pre-commit hooks."""
hook_path, legacy_path = get_hook_path(runner, hook_type)
# If our file doesn't exist or it isn't ours, gtfo.
if (
not os.path.exists(runner.pre_commit_path) or (
not is_our_pre_commit(runner.pre_commit_path) and
not is_previous_pre_commit(runner.pre_commit_path)
not os.path.exists(hook_path) or (
not is_our_pre_commit(hook_path) and
not is_previous_pre_commit(hook_path)
)
):
return 0
os.remove(runner.pre_commit_path)
print('pre-commit uninstalled')
os.remove(hook_path)
print('{0} uninstalled'.format(hook_type))
if os.path.exists(runner.pre_commit_legacy_path):
os.rename(runner.pre_commit_legacy_path, runner.pre_commit_path)
print('Restored previous hooks to {0}'.format(runner.pre_commit_path))
if os.path.exists(legacy_path):
os.rename(legacy_path, hook_path)
print('Restored previous hooks to {0}'.format(hook_path))
return 0

View File

@@ -11,6 +11,7 @@ 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
@@ -48,8 +49,20 @@ def _print_user_skipped(hook, write, args):
))
def get_changed_files(new, old):
changed_files = 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.files:
if args.origin and args.source:
get_filenames = git.get_files_matching(
lambda: get_changed_files(args.origin, args.source))
elif args.files:
get_filenames = git.get_files_matching(lambda: args.files)
elif args.all_files:
get_filenames = git.get_all_files_matching
@@ -137,6 +150,10 @@ 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.')
return 1
# Don't stash if specified or files are specified
if args.no_stash or args.all_files or args.files:

View File

@@ -44,8 +44,18 @@ def main(argv=None):
'in the config file.'
),
)
install_parser.add_argument(
'-t', '--hook-type', choices=('pre-commit', 'pre-push'),
default='pre-commit',
)
subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.')
uninstall_parser = subparsers.add_parser(
'uninstall', help='Uninstall the pre-commit script.',
)
uninstall_parser.add_argument(
'-t', '--hook-type', choices=('pre-commit', 'pre-push'),
default='pre-commit',
)
subparsers.add_parser('clean', help='Clean out pre-commit files.')
@@ -67,6 +77,15 @@ def main(argv=None):
run_parser.add_argument(
'--verbose', '-v', action='store_true', default=False,
)
run_parser.add_argument(
'--origin', '-o', default='',
help='The origin branch"s commit_id when using `git push`',
)
run_parser.add_argument(
'--source', '-s', default='',
help='The remote branch"s commit_id when using `git push`',
)
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
run_mutex_group.add_argument(
'--all-files', '-a', action='store_true', default=False,
@@ -98,9 +117,10 @@ def main(argv=None):
if args.command == 'install':
return install(
runner, overwrite=args.overwrite, hooks=args.install_hooks,
hook_type=args.hook_type,
)
elif args.command == 'uninstall':
return uninstall(runner)
return uninstall(runner, hook_type=args.hook_type)
elif args.command == 'clean':
return clean(runner)
elif args.command == 'autoupdate':

View File

@@ -7,6 +7,7 @@ HERE=`pwd`
popd > /dev/null
retv=0
args=""
ENV_PYTHON='{sys_executable}'
@@ -23,29 +24,30 @@ if ((
(ENV_PYTHON_RETV != 0) &&
(PYTHON_RETV != 0)
)); then
echo '`pre-commit` not found. Did you forget to activate your virtualenv?'
echo '`{hook_type}` not found. Did you forget to activate your virtualenv?'
exit 1
fi
# Run the legacy pre-commit if it exists
if [ -x "$HERE"/pre-commit.legacy ]; then
"$HERE"/pre-commit.legacy
if [ -x "$HERE"/{hook_type}.legacy ]; then
"$HERE"/{hook_type}.legacy
if [ $? -ne 0 ]; then
retv=1
fi
fi
{pre_push}
# Run pre-commit
if ((WHICH_RETV == 0)); then
pre-commit
pre-commit $args
PRE_COMMIT_RETV=$?
elif ((ENV_PYTHON_RETV == 0)); then
"$ENV_PYTHON" -m pre_commit.main
"$ENV_PYTHON" -m pre_commit.main $args
PRE_COMMIT_RETV=$?
else
python -m pre_commit.main
python -m pre_commit.main $args
PRE_COMMIT_RETV=$?
fi

View File

@@ -0,0 +1,12 @@
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" != $z40 ]; then
if [ "$remote_sha" = $z40 ];
then
args="run --all-files"
else
args="run --origin $local_sha --source $remote_sha"
fi
fi
done

View File

@@ -10,6 +10,7 @@ from pre_commit import git
from pre_commit.clientlib.validate_config import load_config
from pre_commit.repository import Repository
from pre_commit.store import Store
from pre_commit.util import resource_filename
class Runner(object):
@@ -43,17 +44,39 @@ class Runner(object):
repository.require_installed()
return repositories
@cached_property
def pre_commit_path(self):
return os.path.join(self.git_root, '.git', 'hooks', 'pre-commit')
def get_hook_path(self, hook_type):
return os.path.join(self.git_root, '.git', 'hooks', hook_type)
@cached_property
def pre_commit_path(self):
return self.get_hook_path('pre-commit')
@cached_property
def pre_push_path(self):
return self.get_hook_path('pre-push')
@cached_property
def pre_template(self):
return resource_filename('hook-tmpl')
@cached_property
def pre_push_template(self):
return resource_filename('pre-push-tmpl')
@property
def pre_commit_legacy_path(self):
"""The path in the 'hooks' directory representing the temporary
storage for existing pre-commit hooks.
"""
return self.pre_commit_path + '.legacy'
@property
def pre_push_legacy_path(self):
"""The path in the 'hooks' directory representing the temporary
storage for existing pre-push hooks.
"""
return self.pre_push_path + '.legacy'
@cached_property
def cmd_runner(self):
# TODO: remove this and inline runner.store.cmd_runner