Skip to content

Commit bd65d89

Browse files
committed
Implement default_stages
1 parent 9c6a1d8 commit bd65d89

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

pre_commit/clientlib.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ def _entry(modname):
224224
cfgv.OptionalRecurse(
225225
'default_language_version', DEFAULT_LANGUAGE_VERSION, {},
226226
),
227+
cfgv.Optional(
228+
'default_stages',
229+
cfgv.check_array(cfgv.check_one_of(C.STAGES)),
230+
C.STAGES,
231+
),
227232
cfgv.Optional('exclude', cfgv.check_regex, '^$'),
228233
cfgv.Optional('fail_fast', cfgv.check_bool, False),
229234
)

pre_commit/commands/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def run(config_file, store, args, environ=os.environ):
252252
hook
253253
for hook in all_hooks(config, store)
254254
if not args.hook or hook.id == args.hook or hook.alias == args.hook
255-
if not hook.stages or args.hook_stage in hook.stages
255+
if args.hook_stage in hook.stages
256256
]
257257

258258
if args.hook and not hooks:

pre_commit/repository.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def _hook(*hook_dicts, **kwargs):
135135
if ret['language_version'] == C.DEFAULT:
136136
ret['language_version'] = languages[lang].get_default_version()
137137

138+
if not ret['stages']:
139+
ret['stages'] = root_config['default_stages']
140+
138141
return ret
139142

140143

tests/repository_test.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ def test_local_python_repo(store, local_python_config):
715715
def test_default_language_version(store, local_python_config):
716716
config = {
717717
'default_language_version': {'python': 'fake'},
718+
'default_stages': ['commit'],
718719
'repos': [local_python_config],
719720
}
720721

@@ -728,6 +729,23 @@ def test_default_language_version(store, local_python_config):
728729
assert hook.language_version == 'fake2'
729730

730731

732+
def test_default_stages(store, local_python_config):
733+
config = {
734+
'default_language_version': {'python': C.DEFAULT},
735+
'default_stages': ['commit'],
736+
'repos': [local_python_config],
737+
}
738+
739+
# `stages` was not set, should default
740+
hook, = all_hooks(config, store)
741+
assert hook.stages == ['commit']
742+
743+
# `stages` is set, should not default
744+
config['repos'][0]['hooks'][0]['stages'] = ['push']
745+
hook, = all_hooks(config, store)
746+
assert hook.stages == ['push']
747+
748+
731749
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
732750
path = make_repo(tempdir_factory, 'script_hooks_repo')
733751
config = make_config_from_repo(path)
@@ -786,13 +804,13 @@ def test_manifest_hooks(tempdir_factory, store):
786804
files='',
787805
id='bash_hook',
788806
language='script',
789-
language_version=C.DEFAULT,
807+
language_version='default',
790808
log_file='',
791809
minimum_pre_commit_version='0',
792810
name='Bash hook',
793811
pass_filenames=True,
794812
require_serial=False,
795-
stages=[],
813+
stages=('commit', 'commit-msg', 'manual', 'push'),
796814
types=['file'],
797815
verbose=False,
798816
)

0 commit comments

Comments
 (0)