mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 20:40:08 -06:00
introduce install state v2 to replace v1
the v1 state is unnecessary since new repos are created for new additional_dependencies
This commit is contained in:
@@ -5,8 +5,6 @@ import importlib.metadata
|
||||
CONFIG_FILE = '.pre-commit-config.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`
|
||||
LOCAL_REPO_VERSION = '1'
|
||||
|
||||
|
||||
@@ -23,16 +23,20 @@ from pre_commit.util import rmtree
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
||||
|
||||
def _state_filename_v1(venv: str) -> str:
|
||||
return os.path.join(venv, '.install_state_v1')
|
||||
|
||||
|
||||
def _state_filename_v2(venv: str) -> str:
|
||||
return os.path.join(venv, '.install_state_v2')
|
||||
|
||||
|
||||
def _state(additional_deps: Sequence[str]) -> object:
|
||||
return {'additional_dependencies': sorted(additional_deps)}
|
||||
|
||||
|
||||
def _state_filename(venv: str) -> str:
|
||||
return os.path.join(venv, f'.install_state_v{C.INSTALLED_STATE_VERSION}')
|
||||
|
||||
|
||||
def _read_state(venv: str) -> object | None:
|
||||
filename = _state_filename(venv)
|
||||
filename = _state_filename_v1(venv)
|
||||
if not os.path.exists(filename):
|
||||
return None
|
||||
else:
|
||||
@@ -51,7 +55,10 @@ def _hook_installed(hook: Hook) -> bool:
|
||||
hook.language_version,
|
||||
)
|
||||
return (
|
||||
_read_state(venv) == _state(hook.additional_dependencies) and
|
||||
(
|
||||
os.path.exists(_state_filename_v2(venv)) or
|
||||
_read_state(venv) == _state(hook.additional_dependencies)
|
||||
) and
|
||||
not lang.health_check(hook.prefix, hook.language_version)
|
||||
)
|
||||
|
||||
@@ -87,14 +94,18 @@ def _hook_install(hook: Hook) -> None:
|
||||
f'your environment\n\n'
|
||||
f'more info:\n\n{health_error}',
|
||||
)
|
||||
|
||||
# TODO: remove v1 state writing, no longer needed after pre-commit 3.0
|
||||
# Write our state to indicate we're installed
|
||||
state_filename = _state_filename(venv)
|
||||
state_filename = _state_filename_v1(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)
|
||||
|
||||
open(_state_filename_v2(venv), 'a+').close()
|
||||
|
||||
|
||||
def _hook(
|
||||
*hook_dicts: dict[str, Any],
|
||||
|
||||
@@ -23,6 +23,7 @@ from pre_commit.languages import ruby
|
||||
from pre_commit.languages import rust
|
||||
from pre_commit.languages.all import languages
|
||||
from pre_commit.prefix import Prefix
|
||||
from pre_commit.repository import _hook_installed
|
||||
from pre_commit.repository import all_hooks
|
||||
from pre_commit.repository import install_hook_envs
|
||||
from pre_commit.util import cmd_output
|
||||
@@ -562,6 +563,21 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
|
||||
assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('v', ('v1', 'v2'))
|
||||
def test_repository_state_compatibility(tempdir_factory, store, v):
|
||||
path = make_repo(tempdir_factory, 'python_hooks_repo')
|
||||
|
||||
config = make_config_from_repo(path)
|
||||
hook = _get_hook(config, store, 'foo')
|
||||
envdir = helpers.environment_dir(
|
||||
hook.prefix,
|
||||
python.ENVIRONMENT_DIR,
|
||||
hook.language_version,
|
||||
)
|
||||
os.remove(os.path.join(envdir, f'.install_state_{v}'))
|
||||
assert _hook_installed(hook) is True
|
||||
|
||||
|
||||
def test_additional_ruby_dependencies_installed(tempdir_factory, store):
|
||||
path = make_repo(tempdir_factory, 'ruby_hooks_repo')
|
||||
config = make_config_from_repo(path)
|
||||
|
||||
Reference in New Issue
Block a user