Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c550d0

Browse files
authored
Merge pull request #886 from s0undt3ch/features/repo-alias
Allow aliasing a hook and calling it by it's alias
2 parents c5c0a06 + 6d40b2a commit 8c550d0

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

pre_commit/clientlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def _make_argparser(filenames_help):
3636
cfgv.Required('name', cfgv.check_string),
3737
cfgv.Required('entry', cfgv.check_string),
3838
cfgv.Required('language', cfgv.check_one_of(all_languages)),
39+
cfgv.Optional('alias', cfgv.check_string, ''),
3940

4041
cfgv.Optional(
4142
'files', cfgv.check_and(cfgv.check_string, cfgv.check_regex), '',

pre_commit/commands/run.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
7777
'replacement.'.format(hook['id'], repo.repo_config['repo']),
7878
)
7979

80-
if hook['id'] in skips:
80+
if hook['id'] in skips or hook['alias'] in skips:
8181
output.write(get_hook_message(
8282
_hook_msg_start(hook, args.verbose),
8383
end_msg=SKIPPED,
@@ -257,8 +257,15 @@ def run(config_file, store, args, environ=os.environ):
257257
for repo in repositories(config, store):
258258
for _, hook in repo.hooks:
259259
if (
260-
(not args.hook or hook['id'] == args.hook) and
261-
(not hook['stages'] or args.hook_stage in hook['stages'])
260+
(
261+
not args.hook or
262+
hook['id'] == args.hook or
263+
hook['alias'] == args.hook
264+
) and
265+
(
266+
not hook['stages'] or
267+
args.hook_stage in hook['stages']
268+
)
262269
):
263270
repo_hooks.append((repo, hook))
264271

tests/commands/run_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def repo_with_failing_hook(tempdir_factory):
4242
yield git_path
4343

4444

45+
@pytest.fixture
46+
def aliased_repo(tempdir_factory):
47+
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
48+
with cwd(git_path):
49+
with modify_config() as config:
50+
config['repos'][0]['hooks'].append(
51+
{'id': 'bash_hook', 'alias': 'foo_bash'},
52+
)
53+
stage_a_file()
54+
yield git_path
55+
56+
4557
def stage_a_file(filename='foo.py'):
4658
open(filename, 'a').close()
4759
cmd_output('git', 'add', filename)
@@ -388,6 +400,18 @@ def test_skip_hook(cap_out, store, repo_with_passing_hook):
388400
assert msg in printed
389401

390402

403+
def test_skip_aliased_hook(cap_out, store, aliased_repo):
404+
ret, printed = _do_run(
405+
cap_out, store, aliased_repo,
406+
run_opts(hook='foo_bash'),
407+
{'SKIP': 'foo_bash'},
408+
)
409+
assert ret == 0
410+
# Only the aliased hook runs and is skipped
411+
for msg in (b'Bash hook', b'Skipped'):
412+
assert printed.count(msg) == 1
413+
414+
391415
def test_hook_id_not_in_non_verbose_output(
392416
cap_out, store, repo_with_passing_hook,
393417
):
@@ -416,6 +440,24 @@ def test_multiple_hooks_same_id(cap_out, store, repo_with_passing_hook):
416440
assert output.count(b'Bash hook') == 2
417441

418442

443+
def test_aliased_hook_run(cap_out, store, aliased_repo):
444+
ret, output = _do_run(
445+
cap_out, store, aliased_repo,
446+
run_opts(verbose=True, hook='bash_hook'),
447+
)
448+
assert ret == 0
449+
# Both hooks will run since they share the same ID
450+
assert output.count(b'Bash hook') == 2
451+
452+
ret, output = _do_run(
453+
cap_out, store, aliased_repo,
454+
run_opts(verbose=True, hook='foo_bash'),
455+
)
456+
assert ret == 0
457+
# Only the aliased hook runs
458+
assert output.count(b'Bash hook') == 1
459+
460+
419461
def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory):
420462
with cwd(repo_with_passing_hook):
421463
_, stdout, _ = cmd_output_mocked_pre_commit_home(

tests/repository_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ def test_manifest_hooks(tempdir_factory, store):
831831
'exclude': '^$',
832832
'files': '',
833833
'id': 'bash_hook',
834+
'alias': '',
834835
'language': 'script',
835836
'language_version': 'default',
836837
'log_file': '',

0 commit comments

Comments
 (0)