Merge pull request #886 from s0undt3ch/features/repo-alias

Allow aliasing a hook and calling it by it's alias
This commit is contained in:
Anthony Sottile 2018-12-27 12:17:29 -08:00 committed by GitHub
commit 8c550d0157
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 3 deletions

View file

@ -36,6 +36,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
cfgv.Required('name', cfgv.check_string),
cfgv.Required('entry', cfgv.check_string),
cfgv.Required('language', cfgv.check_one_of(all_languages)),
cfgv.Optional('alias', cfgv.check_string, ''),
cfgv.Optional(
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',

View file

@ -77,7 +77,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
'replacement.'.format(hook['id'], repo.repo_config['repo']),
)
if hook['id'] in skips:
if hook['id'] in skips or hook['alias'] in skips:
output.write(get_hook_message(
_hook_msg_start(hook, args.verbose),
end_msg=SKIPPED,
@ -257,8 +257,15 @@ def run(config_file, store, args, environ=os.environ):
for repo in repositories(config, store):
for _, hook in repo.hooks:
if (
(not args.hook or hook['id'] == args.hook) and
(not hook['stages'] or args.hook_stage in hook['stages'])
(
not args.hook or
hook['id'] == args.hook or
hook['alias'] == args.hook
) and
(
not hook['stages'] or
args.hook_stage in hook['stages']
)
):
repo_hooks.append((repo, hook))

View file

@ -42,6 +42,18 @@ def repo_with_failing_hook(tempdir_factory):
yield git_path
@pytest.fixture
def aliased_repo(tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(git_path):
with modify_config() as config:
config['repos'][0]['hooks'].append(
{'id': 'bash_hook', 'alias': 'foo_bash'},
)
stage_a_file()
yield git_path
def stage_a_file(filename='foo.py'):
open(filename, 'a').close()
cmd_output('git', 'add', filename)
@ -388,6 +400,18 @@ def test_skip_hook(cap_out, store, repo_with_passing_hook):
assert msg in printed
def test_skip_aliased_hook(cap_out, store, aliased_repo):
ret, printed = _do_run(
cap_out, store, aliased_repo,
run_opts(hook='foo_bash'),
{'SKIP': 'foo_bash'},
)
assert ret == 0
# Only the aliased hook runs and is skipped
for msg in (b'Bash hook', b'Skipped'):
assert printed.count(msg) == 1
def test_hook_id_not_in_non_verbose_output(
cap_out, store, repo_with_passing_hook,
):
@ -416,6 +440,24 @@ def test_multiple_hooks_same_id(cap_out, store, repo_with_passing_hook):
assert output.count(b'Bash hook') == 2
def test_aliased_hook_run(cap_out, store, aliased_repo):
ret, output = _do_run(
cap_out, store, aliased_repo,
run_opts(verbose=True, hook='bash_hook'),
)
assert ret == 0
# Both hooks will run since they share the same ID
assert output.count(b'Bash hook') == 2
ret, output = _do_run(
cap_out, store, aliased_repo,
run_opts(verbose=True, hook='foo_bash'),
)
assert ret == 0
# Only the aliased hook runs
assert output.count(b'Bash hook') == 1
def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory):
with cwd(repo_with_passing_hook):
_, stdout, _ = cmd_output_mocked_pre_commit_home(

View file

@ -831,6 +831,7 @@ def test_manifest_hooks(tempdir_factory, store):
'exclude': '^$',
'files': '',
'id': 'bash_hook',
'alias': '',
'language': 'script',
'language_version': 'default',
'log_file': '',