Skip to content

Commit dd73ffd

Browse files
barrysteynTravis CI
authored andcommitted
Filtering of hooks for commit or push stages
1 parent e3a2206 commit dd73ffd

File tree

8 files changed

+89
-3
lines changed

8 files changed

+89
-3
lines changed

pre_commit/clientlib/validate_manifest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class InvalidManifestError(ValueError):
2525
'language_version': {'type': 'string', 'default': 'default'},
2626
'files': {'type': 'string'},
2727
'expected_return_value': {'type': 'number', 'default': 0},
28+
'stages': {
29+
'type': 'array',
30+
'default': [],
31+
'items': {
32+
'type': 'string',
33+
},
34+
},
2835
'args': {
2936
'type': 'array',
3037
'default': [],

pre_commit/commands/install_uninstall.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
'4d9958c90bc262f47553e2c073f14cfe',
2121
'd8ee923c46731b42cd95cc869add4062',
2222
'49fd668cb42069aa1b6048464be5d395',
23+
'79f09a650522a87b0da915d0d983b2de'
2324
)
2425

2526

26-
IDENTIFYING_HASH = '79f09a650522a87b0da915d0d983b2de'
27+
IDENTIFYING_HASH = 'e358c9dae00eac5d06b38dfdb1e33a8c'
2728

2829

2930
def is_our_pre_commit(filename):

pre_commit/commands/run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
175175

176176
with ctx:
177177
repo_hooks = list(get_repo_hooks(runner))
178+
178179
if args.hook:
179180
repo_hooks = [
180181
(repo, hook) for repo, hook in repo_hooks
@@ -183,4 +184,11 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
183184
if not repo_hooks:
184185
write('No hook with id `{0}`\n'.format(args.hook))
185186
return 1
187+
188+
# Filter hooks for stages
189+
repo_hooks = [
190+
(repo, hook) for repo, hook in repo_hooks
191+
if not hook['stages'] or args.hook_stage in hook['stages']
192+
]
193+
186194
return _run_hooks(repo_hooks, args, write, environ)

pre_commit/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def main(argv=None):
8787
run_parser.add_argument(
8888
'--verbose', '-v', action='store_true', default=False,
8989
)
90-
9190
run_parser.add_argument(
9291
'--origin', '-o',
9392
help='The origin branch\'s commit_id when using `git push`',
@@ -101,6 +100,10 @@ def main(argv=None):
101100
help='Allow an unstaged config to be present. Note that this will'
102101
'be stashed before parsing unless --no-stash is specified'
103102
)
103+
run_parser.add_argument(
104+
'--hook-stage', choices=('commit', 'push'), default='commit',
105+
help='The stage during which the hook is fired e.g. commit or push'
106+
)
104107
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
105108
run_mutex_group.add_argument(
106109
'--all-files', '-a', action='store_true', default=False,

pre_commit/resources/hook-tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
# This is a randomish md5 to identify this script
3-
# 79f09a650522a87b0da915d0d983b2de
3+
# e358c9dae00eac5d06b38dfdb1e33a8c
44

55
pushd `dirname $0` > /dev/null
66
HERE=`pwd`

pre_commit/resources/pre-push-tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ do
1010
fi
1111
fi
1212
done
13+
14+
args="$args --hook-stage push"

tests/commands/run_test.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def _get_opts(
5959
origin='',
6060
source='',
6161
allow_unstaged_config=False,
62+
hook_stage='commit'
6263
):
6364
# These are mutually exclusive
6465
assert not (all_files and files)
@@ -68,6 +69,7 @@ def _get_opts(
6869
color=color,
6970
verbose=verbose,
7071
hook=hook,
72+
hook_stage=hook_stage,
7173
no_stash=no_stash,
7274
origin=origin,
7375
source=source,
@@ -89,6 +91,7 @@ def _test_run(repo, options, expected_outputs, expected_ret, stage):
8991
stage_a_file()
9092
args = _get_opts(**options)
9193
ret, printed = _do_run(repo, args)
94+
9295
assert ret == expected_ret, (ret, expected_ret, printed)
9396
for expected_output_part in expected_outputs:
9497
assert expected_output_part in printed
@@ -371,6 +374,66 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
371374
)
372375

373376

377+
@pytest.mark.parametrize(
378+
('hook_stage', 'stage_for_first_hook', 'stage_for_second_hook',
379+
'expected_output'),
380+
(
381+
('push', ['commit'], ['commit'], [b'', b'']),
382+
('push', ['commit', 'push'], ['commit', 'push'],
383+
[b'hook 1', b'hook 2']),
384+
('push', [], [], [b'hook 1', b'hook 2']),
385+
('push', [], ['commit'], [b'hook 1', b'']),
386+
('push', ['push'], ['commit'], [b'hook 1', b'']),
387+
('push', ['commit'], ['push'], [b'', b'hook 2']),
388+
('commit', ['commit', 'push'], ['commit', 'push'],
389+
[b'hook 1', b'hook 2']),
390+
('commit', ['commit'], ['commit'], [b'hook 1', b'hook 2']),
391+
('commit', [], [], [b'hook 1', b'hook 2']),
392+
('commit', [], ['commit'], [b'', b'hook 2']),
393+
('commit', ['push'], ['commit'], [b'', b'hook 2']),
394+
('commit', ['commit'], ['push'], [b'hook 1', b'']),
395+
)
396+
)
397+
def test_local_hook_for_stages(
398+
repo_with_passing_hook, mock_out_store_directory,
399+
stage_for_first_hook,
400+
stage_for_second_hook,
401+
hook_stage,
402+
expected_output
403+
):
404+
config = OrderedDict((
405+
('repo', 'local'),
406+
('hooks', (OrderedDict((
407+
('id', 'pylint'),
408+
('name', 'hook 1'),
409+
('entry', 'python -m pylint.__main__'),
410+
('language', 'system'),
411+
('files', r'\.py$'),
412+
('stages', stage_for_first_hook)
413+
)), OrderedDict((
414+
('id', 'do_not_commit'),
415+
('name', 'hook 2'),
416+
('entry', 'DO NOT COMMIT'),
417+
('language', 'pcre'),
418+
('files', '^(.*)$'),
419+
('stages', stage_for_second_hook)
420+
))))
421+
))
422+
add_config_to_repo(repo_with_passing_hook, config)
423+
424+
with io.open('dummy.py', 'w') as staged_file:
425+
staged_file.write('"""TODO: something"""\n')
426+
cmd_output('git', 'add', 'dummy.py')
427+
428+
_test_run(
429+
repo_with_passing_hook,
430+
{'hook_stage': hook_stage},
431+
expected_outputs=expected_output,
432+
expected_ret=0,
433+
stage=False
434+
)
435+
436+
374437
def test_local_hook_passes(
375438
repo_with_passing_hook, mock_out_store_directory,
376439
):

tests/manifest_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_manifest_contents(manifest):
2929
'language': 'script',
3030
'language_version': 'default',
3131
'name': 'Bash hook',
32+
'stages': [],
3233
}]
3334

3435

@@ -44,4 +45,5 @@ def test_hooks(manifest):
4445
'language': 'script',
4546
'language_version': 'default',
4647
'name': 'Bash hook',
48+
'stages': [],
4749
}

0 commit comments

Comments
 (0)