Skip to content

Commit ea88b37

Browse files
authored
Merge pull request #2720 from pre-commit/install-state-v2
introduce install state v2 to replace v1
2 parents 2c39545 + bff5e0e commit ea88b37

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

pre_commit/constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
CONFIG_FILE = '.pre-commit-config.yaml'
66
MANIFEST_FILE = '.pre-commit-hooks.yaml'
77

8-
# Bump when installation changes in a backwards / forwards incompatible way
9-
INSTALLED_STATE_VERSION = '1'
108
# Bump when modifying `empty_template`
119
LOCAL_REPO_VERSION = '1'
1210

pre_commit/repository.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@
2323
logger = logging.getLogger('pre_commit')
2424

2525

26-
def _state(additional_deps: Sequence[str]) -> object:
27-
return {'additional_dependencies': sorted(additional_deps)}
26+
def _state_filename_v1(venv: str) -> str:
27+
return os.path.join(venv, '.install_state_v1')
28+
29+
30+
def _state_filename_v2(venv: str) -> str:
31+
return os.path.join(venv, '.install_state_v2')
2832

2933

30-
def _state_filename(venv: str) -> str:
31-
return os.path.join(venv, f'.install_state_v{C.INSTALLED_STATE_VERSION}')
34+
def _state(additional_deps: Sequence[str]) -> object:
35+
return {'additional_dependencies': sorted(additional_deps)}
3236

3337

3438
def _read_state(venv: str) -> object | None:
35-
filename = _state_filename(venv)
39+
filename = _state_filename_v1(venv)
3640
if not os.path.exists(filename):
3741
return None
3842
else:
@@ -51,7 +55,10 @@ def _hook_installed(hook: Hook) -> bool:
5155
hook.language_version,
5256
)
5357
return (
54-
_read_state(venv) == _state(hook.additional_dependencies) and
58+
(
59+
os.path.exists(_state_filename_v2(venv)) or
60+
_read_state(venv) == _state(hook.additional_dependencies)
61+
) and
5562
not lang.health_check(hook.prefix, hook.language_version)
5663
)
5764

@@ -87,14 +94,18 @@ def _hook_install(hook: Hook) -> None:
8794
f'your environment\n\n'
8895
f'more info:\n\n{health_error}',
8996
)
97+
98+
# TODO: remove v1 state writing, no longer needed after pre-commit 3.0
9099
# Write our state to indicate we're installed
91-
state_filename = _state_filename(venv)
100+
state_filename = _state_filename_v1(venv)
92101
staging = f'{state_filename}staging'
93102
with open(staging, 'w') as state_file:
94103
state_file.write(json.dumps(_state(hook.additional_dependencies)))
95104
# Move the file into place atomically to indicate we've installed
96105
os.replace(staging, state_filename)
97106

107+
open(_state_filename_v2(venv), 'a+').close()
108+
98109

99110
def _hook(
100111
*hook_dicts: dict[str, Any],

tests/repository_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pre_commit.languages import rust
2424
from pre_commit.languages.all import languages
2525
from pre_commit.prefix import Prefix
26+
from pre_commit.repository import _hook_installed
2627
from pre_commit.repository import all_hooks
2728
from pre_commit.repository import install_hook_envs
2829
from pre_commit.util import cmd_output
@@ -562,6 +563,21 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
562563
assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1]
563564

564565

566+
@pytest.mark.parametrize('v', ('v1', 'v2'))
567+
def test_repository_state_compatibility(tempdir_factory, store, v):
568+
path = make_repo(tempdir_factory, 'python_hooks_repo')
569+
570+
config = make_config_from_repo(path)
571+
hook = _get_hook(config, store, 'foo')
572+
envdir = helpers.environment_dir(
573+
hook.prefix,
574+
python.ENVIRONMENT_DIR,
575+
hook.language_version,
576+
)
577+
os.remove(os.path.join(envdir, f'.install_state_{v}'))
578+
assert _hook_installed(hook) is True
579+
580+
565581
def test_additional_ruby_dependencies_installed(tempdir_factory, store):
566582
path = make_repo(tempdir_factory, 'ruby_hooks_repo')
567583
config = make_config_from_repo(path)

0 commit comments

Comments
 (0)