2323logger = 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
3438def _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
99110def _hook (
100111 * hook_dicts : dict [str , Any ],
0 commit comments