mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-16 02:21:46 +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
|
|
@ -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