mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Install local hooks in the ~/.pre-commit folder
This commit is contained in:
parent
377cffbd27
commit
9a8fb17070
15 changed files with 103 additions and 75 deletions
|
|
@ -70,6 +70,7 @@ def validate_config_extra(config):
|
|||
raise InvalidConfigError(
|
||||
'"sha" property provided for local hooks'
|
||||
)
|
||||
repo['sha'] = 'local'
|
||||
elif 'sha' not in repo:
|
||||
raise InvalidConfigError(
|
||||
'Missing "sha" field for repository {}'.format(repo['repo'])
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ def _update_repo(repo_config, runner, tags_only):
|
|||
Args:
|
||||
repo_config - A config for a repository
|
||||
"""
|
||||
repo = Repository.create(repo_config, runner.store)
|
||||
repo = Repository.create(repo_config, runner.store, runner.git_root)
|
||||
|
||||
with cwd(repo.repo_path_getter.repo_path):
|
||||
cmd_output('git', 'fetch')
|
||||
|
|
@ -51,7 +51,7 @@ def _update_repo(repo_config, runner, tags_only):
|
|||
# Construct a new config with the head sha
|
||||
new_config = OrderedDict(repo_config)
|
||||
new_config['sha'] = rev
|
||||
new_repo = Repository.create(new_config, runner.store)
|
||||
new_repo = Repository.create(new_config, runner.store, runner.git_root)
|
||||
|
||||
# See if any of our hooks were deleted with the new commits
|
||||
hooks = {hook['id'] for hook in repo.repo_config['hooks']}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
): # pragma: windows no cover
|
||||
assert repo_cmd_runner.exists('Dockerfile'), (
|
||||
'No Dockerfile was found in the hook repository'
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
):
|
||||
helpers.assert_version_default('golang', version)
|
||||
directory = repo_cmd_runner.path(
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
): # pragma: windows no cover
|
||||
additional_dependencies = tuple(additional_dependencies)
|
||||
assert repo_cmd_runner.exists('package.json')
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
):
|
||||
"""Installation for pcre type is a noop."""
|
||||
raise AssertionError('Cannot install pcre repo.')
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
):
|
||||
additional_dependencies = tuple(additional_dependencies)
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
|
|
@ -74,7 +74,7 @@ def install_environment(
|
|||
else:
|
||||
venv_cmd.extend(['-p', os.path.realpath(sys.executable)])
|
||||
repo_cmd_runner.run(venv_cmd, cwd='/')
|
||||
to_install = () if is_local_hook else ('.')
|
||||
to_install = () if is_local_hook else ('.',)
|
||||
to_install += additional_dependencies
|
||||
with in_env(repo_cmd_runner, version):
|
||||
helpers.run_setup_cmd(
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
): # pragma: windows no cover
|
||||
additional_dependencies = tuple(additional_dependencies)
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
):
|
||||
"""Installation for script type is a noop."""
|
||||
raise AssertionError('Cannot install script repo.')
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
): # pragma: windows no cover
|
||||
helpers.assert_version_default('swift', version)
|
||||
helpers.assert_no_additional_deps('swift', additional_dependencies)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ def install_environment(
|
|||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=(),
|
||||
is_local_hook = False,
|
||||
is_local_hook=False,
|
||||
):
|
||||
"""Installation for system type is a noop."""
|
||||
raise AssertionError('Cannot install system repo.')
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import pkg_resources
|
|||
from cached_property import cached_property
|
||||
|
||||
from pre_commit import five
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib.validate_config import is_local_hooks
|
||||
from pre_commit.clientlib.validate_manifest import MANIFEST_JSON_SCHEMA
|
||||
from pre_commit.jsonschema_extensions import apply_defaults
|
||||
|
|
@ -38,13 +37,13 @@ class Repository(object):
|
|||
self.__installed = False
|
||||
|
||||
@classmethod
|
||||
def create(cls, config, store):
|
||||
def create(cls, config, store, owner):
|
||||
repo_path_getter = store.get_repo_path_getter(
|
||||
config['repo'], config['sha'], owner
|
||||
)
|
||||
if is_local_hooks(config):
|
||||
return LocalRepository(config)
|
||||
return LocalRepository(config, repo_path_getter)
|
||||
else:
|
||||
repo_path_getter = store.get_repo_path_getter(
|
||||
config['repo'], config['sha']
|
||||
)
|
||||
return cls(config, repo_path_getter)
|
||||
|
||||
@cached_property
|
||||
|
|
@ -198,9 +197,6 @@ class Repository(object):
|
|||
|
||||
|
||||
class LocalRepository(Repository):
|
||||
def __init__(self, repo_config):
|
||||
super(LocalRepository, self).__init__(repo_config, None)
|
||||
|
||||
@cached_property
|
||||
def hooks(self):
|
||||
return tuple(
|
||||
|
|
@ -208,14 +204,6 @@ class LocalRepository(Repository):
|
|||
for hook in self.repo_config['hooks']
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def cmd_runner(self):
|
||||
return PrefixedCommandRunner(git.get_root())
|
||||
|
||||
@cached_property
|
||||
def manifest(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class _UniqueList(list):
|
||||
def __init__(self):
|
||||
|
|
|
|||
|
|
@ -41,7 +41,9 @@ class Runner(object):
|
|||
def repositories(self):
|
||||
"""Returns a tuple of the configured repositories."""
|
||||
config = load_config(self.config_file_path)
|
||||
repositories = tuple(Repository.create(x, self.store) for x in config)
|
||||
repositories = tuple(
|
||||
Repository.create(x, self.store, self.git_root) for x in config
|
||||
)
|
||||
for repository in repositories:
|
||||
repository.require_installed()
|
||||
return repositories
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import tempfile
|
|||
|
||||
from cached_property import cached_property
|
||||
|
||||
from pre_commit.clientlib.validate_config import _LOCAL_HOOKS_MAGIC_REPO_STRING
|
||||
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
from pre_commit.util import cmd_output
|
||||
|
|
@ -45,6 +46,17 @@ class Store(object):
|
|||
def repo_path(self):
|
||||
return self._store.clone(self._repo, self._ref)
|
||||
|
||||
class LocalRepoPathGetter(RepoPathGetter):
|
||||
def __init__(self, repo, sha, store, owner):
|
||||
super(Store.LocalRepoPathGetter, self).__init__(
|
||||
repo, sha, store,
|
||||
)
|
||||
self._owner = owner
|
||||
|
||||
@cached_property
|
||||
def repo_path(self):
|
||||
return self._store.initialize_local_repo(self._owner)
|
||||
|
||||
def __init__(self, directory=None):
|
||||
if directory is None:
|
||||
directory = self.get_default_directory()
|
||||
|
|
@ -97,19 +109,31 @@ class Store(object):
|
|||
self._create()
|
||||
self.__created = True
|
||||
|
||||
def clone(self, url, ref):
|
||||
"""Clone the given url and checkout the specific ref."""
|
||||
self.require_created()
|
||||
|
||||
# Check if we already exist
|
||||
def find_local_path(self, repo, ref):
|
||||
with sqlite3.connect(self.db_path) as db:
|
||||
result = db.execute(
|
||||
'SELECT path FROM repos WHERE repo = ? AND ref = ?',
|
||||
[url, ref],
|
||||
[repo, ref],
|
||||
).fetchone()
|
||||
if result:
|
||||
return result[0]
|
||||
|
||||
def store_path(self, repo, ref, path):
|
||||
with sqlite3.connect(self.db_path) as db:
|
||||
db.execute(
|
||||
'INSERT INTO repos (repo, ref, path) VALUES (?, ?, ?)',
|
||||
[repo, ref, path],
|
||||
)
|
||||
|
||||
def clone(self, url, sha):
|
||||
"""Clone the given url and checkout the specific sha."""
|
||||
self.require_created()
|
||||
|
||||
# Check if we already exist
|
||||
local_path = self.find_local_path(url, sha)
|
||||
if local_path:
|
||||
return local_path
|
||||
|
||||
logger.info('Initializing environment for {}.'.format(url))
|
||||
|
||||
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
|
||||
|
|
@ -121,15 +145,33 @@ class Store(object):
|
|||
cmd_output('git', 'reset', ref, '--hard', env=no_git_env())
|
||||
|
||||
# Update our db with the created repo
|
||||
with sqlite3.connect(self.db_path) as db:
|
||||
db.execute(
|
||||
'INSERT INTO repos (repo, ref, path) VALUES (?, ?, ?)',
|
||||
[url, ref, dir],
|
||||
)
|
||||
self.store_path(url, sha, dir)
|
||||
return dir
|
||||
|
||||
def get_repo_path_getter(self, repo, ref):
|
||||
return self.RepoPathGetter(repo, ref, self)
|
||||
def initialize_local_repo(self, owner):
|
||||
"""Initializes a repo in the default repository for the local repo"""
|
||||
self.require_created()
|
||||
local_path = self.find_local_path(
|
||||
_LOCAL_HOOKS_MAGIC_REPO_STRING, owner,
|
||||
)
|
||||
if local_path:
|
||||
return local_path
|
||||
|
||||
logger.info('Initializing environment for {}.'.format(
|
||||
_LOCAL_HOOKS_MAGIC_REPO_STRING,
|
||||
))
|
||||
|
||||
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
|
||||
|
||||
# Update our db with the created repo
|
||||
self.store_path(_LOCAL_HOOKS_MAGIC_REPO_STRING, owner, dir)
|
||||
return dir
|
||||
|
||||
def get_repo_path_getter(self, repo, sha, owner=None):
|
||||
if sha == _LOCAL_HOOKS_MAGIC_REPO_STRING:
|
||||
return self.LocalRepoPathGetter(repo, sha, self, owner)
|
||||
else:
|
||||
return self.RepoPathGetter(repo, sha, self)
|
||||
|
||||
@cached_property
|
||||
def cmd_runner(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue