Add --allow-missing-config option to install

When no '.pre-commit-config.yaml' file exists while `pre-commit` hooks
are enabled, `pre-commit` returns an error and the action is aborted.
This is a very common scenario when pre-commit is added later on a
project and the user wants to work on a previous branch where the
configuration file does not exist.

This commits allow the user to optionally install the `pre-commit` hooks
with an option to allow a missing configuration and trigger only the
legacy pre-commit hooks (if any) when it is missing.
This commit is contained in:
Filippos Giannakos
2017-02-21 12:40:33 +02:00
parent 41dcaff3fb
commit 2f4199850d
5 changed files with 84 additions and 2 deletions

View File

@@ -37,7 +37,10 @@ def is_previous_pre_commit(filename):
return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
def install(
runner, overwrite=False, hooks=False, hook_type='pre-commit',
skip_on_missing_conf=False
):
"""Install the pre-commit hooks."""
hook_path = runner.get_hook_path(hook_type)
legacy_path = hook_path + '.legacy'
@@ -70,10 +73,12 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
else:
pre_push_contents = ''
skip_on_missing_conf = 'true' if skip_on_missing_conf else 'false'
contents = io.open(resource_filename('hook-tmpl')).read().format(
sys_executable=sys.executable,
hook_type=hook_type,
pre_push=pre_push_contents,
skip_on_missing_conf=skip_on_missing_conf
)
pre_commit_file_obj.write(contents)
make_executable(hook_path)