Skip to content

Commit c78b696

Browse files
committed
Add top level minimum_pre_commit_version
1 parent 264161c commit c78b696

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pre_commit/clientlib.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pre_commit.constants as C
1414
from pre_commit.error_handler import FatalError
1515
from pre_commit.languages.all import all_languages
16+
from pre_commit.util import parse_version
1617

1718

1819
def check_type_tag(tag):
@@ -23,6 +24,16 @@ def check_type_tag(tag):
2324
)
2425

2526

27+
def check_min_version(version):
28+
if parse_version(version) > parse_version(C.VERSION):
29+
raise cfgv.ValidationError(
30+
'pre-commit version {} is required but version {} is installed. '
31+
'Perhaps run `pip install --upgrade pre-commit`.'.format(
32+
version, C.VERSION,
33+
),
34+
)
35+
36+
2637
def _make_argparser(filenames_help):
2738
parser = argparse.ArgumentParser()
2839
parser.add_argument('filenames', nargs='*', help=filenames_help)
@@ -231,6 +242,11 @@ def _entry(modname):
231242
),
232243
cfgv.Optional('exclude', cfgv.check_regex, '^$'),
233244
cfgv.Optional('fail_fast', cfgv.check_bool, False),
245+
cfgv.Optional(
246+
'minimum_pre_commit_version',
247+
cfgv.check_and(cfgv.check_string, check_min_version),
248+
'0',
249+
),
234250
)
235251

236252

tests/clientlib_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cfgv
44
import pytest
55

6+
import pre_commit.constants as C
67
from pre_commit.clientlib import check_type_tag
78
from pre_commit.clientlib import CONFIG_HOOK_DICT
89
from pre_commit.clientlib import CONFIG_REPO_DICT
@@ -234,3 +235,23 @@ def test_meta_hook_invalid(config_repo):
234235
def test_default_language_version_invalid(mapping):
235236
with pytest.raises(cfgv.ValidationError):
236237
cfgv.validate(mapping, DEFAULT_LANGUAGE_VERSION)
238+
239+
240+
def test_minimum_pre_commit_version_failing():
241+
with pytest.raises(cfgv.ValidationError) as excinfo:
242+
cfg = {'repos': [], 'minimum_pre_commit_version': '999'}
243+
cfgv.validate(cfg, CONFIG_SCHEMA)
244+
assert str(excinfo.value) == (
245+
'\n'
246+
'==> At Config()\n'
247+
'==> At key: minimum_pre_commit_version\n'
248+
'=====> pre-commit version 999 is required but version {} is '
249+
'installed. Perhaps run `pip install --upgrade pre-commit`.'.format(
250+
C.VERSION,
251+
)
252+
)
253+
254+
255+
def test_minimum_pre_commit_version_passing():
256+
cfg = {'repos': [], 'minimum_pre_commit_version': '0'}
257+
cfgv.validate(cfg, CONFIG_SCHEMA)

0 commit comments

Comments
 (0)