Merge pull request #2674 from pre-commit/revert-2671-simplify-state

Revert "simplify install state"
This commit is contained in:
Anthony Sottile 2023-01-02 19:09:16 -05:00 committed by GitHub
commit e1567b6148
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View file

@ -5,6 +5,8 @@ import importlib.metadata
CONFIG_FILE = '.pre-commit-config.yaml' CONFIG_FILE = '.pre-commit-config.yaml'
MANIFEST_FILE = '.pre-commit-hooks.yaml' MANIFEST_FILE = '.pre-commit-hooks.yaml'
# Bump when installation changes in a backwards / forwards incompatible way
INSTALLED_STATE_VERSION = '1'
# Bump when modifying `empty_template` # Bump when modifying `empty_template`
LOCAL_REPO_VERSION = '1' LOCAL_REPO_VERSION = '1'

View file

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import json
import logging import logging
import os import os
from typing import Any from typing import Any
@ -22,8 +23,21 @@ from pre_commit.util import rmtree
logger = logging.getLogger('pre_commit') logger = logging.getLogger('pre_commit')
def _state(additional_deps: Sequence[str]) -> object:
return {'additional_dependencies': sorted(additional_deps)}
def _state_filename(venv: str) -> str: def _state_filename(venv: str) -> str:
return os.path.join(venv, '.install_state_v2') return os.path.join(venv, f'.install_state_v{C.INSTALLED_STATE_VERSION}')
def _read_state(venv: str) -> object | None:
filename = _state_filename(venv)
if not os.path.exists(filename):
return None
else:
with open(filename) as f:
return json.load(f)
def _hook_installed(hook: Hook) -> bool: def _hook_installed(hook: Hook) -> bool:
@ -37,7 +51,7 @@ def _hook_installed(hook: Hook) -> bool:
hook.language_version, hook.language_version,
) )
return ( return (
os.path.exists(_state_filename(venv)) and _read_state(venv) == _state(hook.additional_dependencies) and
not lang.health_check(hook.prefix, hook.language_version) not lang.health_check(hook.prefix, hook.language_version)
) )
@ -73,8 +87,13 @@ def _hook_install(hook: Hook) -> None:
f'your environment\n\n' f'your environment\n\n'
f'more info:\n\n{health_error}', f'more info:\n\n{health_error}',
) )
# touch state file to indicate we're installed # Write our state to indicate we're installed
open(_state_filename(venv), 'a+').close() state_filename = _state_filename(venv)
staging = f'{state_filename}staging'
with open(staging, 'w') as state_file:
state_file.write(json.dumps(_state(hook.additional_dependencies)))
# Move the file into place atomically to indicate we've installed
os.replace(staging, state_filename)
def _hook( def _hook(