mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Refactored how the installer works
This commit is contained in:
parent
8b0247e17f
commit
abea886a3d
8 changed files with 105 additions and 70 deletions
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
CONFIG_FILE = '.pre-commit-config.yaml'
|
||||
|
||||
PRE_COMMIT_DIR = '.pre-commit-files'
|
||||
HOOKS_WORKSPACE = '.pre-commit-files'
|
||||
|
||||
MANIFEST_FILE = 'manifest.yaml'
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +1,18 @@
|
|||
import os
|
||||
import pkg_resources
|
||||
|
||||
import pre_commit.constants as C
|
||||
from plumbum import local
|
||||
|
||||
|
||||
# TODO: optimization: memoize based on local.cwd.getpath()
|
||||
def get_root():
|
||||
return local['git']['rev-parse', '--show-toplevel']().strip()
|
||||
|
||||
|
||||
def get_pre_commit_path():
|
||||
return os.path.join(get_root(), '.git/hooks/pre-commit')
|
||||
|
||||
|
||||
def get_pre_commit_dir_path():
|
||||
return os.path.join(get_root(), C.PRE_COMMIT_DIR)
|
||||
|
||||
def create_pre_commit_package_dir():
|
||||
local.path(get_pre_commit_dir_path()).mkdir()
|
||||
|
||||
def create_pre_commit():
|
||||
path = get_pre_commit_path()
|
||||
pre_commit_file = pkg_resources.resource_filename('pre_commit', 'resources/pre-commit.sh')
|
||||
|
|
@ -28,4 +23,6 @@ def remove_pre_commit():
|
|||
local.path(get_pre_commit_path()).delete()
|
||||
|
||||
|
||||
|
||||
def get_head_sha(git_repo_path):
|
||||
with local.cwd(git_repo_path):
|
||||
return (local['git']['rev-parse', 'HEAD'])().strip()
|
||||
20
pre_commit/hooks_workspace.py
Normal file
20
pre_commit/hooks_workspace.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
import contextlib
|
||||
import os.path
|
||||
from plumbum import local
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import git
|
||||
|
||||
|
||||
def get_pre_commit_dir_path():
|
||||
return os.path.join(git.get_root(), C.HOOKS_WORKSPACE)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_hooks_workspace():
|
||||
"""Change into the hooks workspace. If it does not exist create it."""
|
||||
if not os.path.exists(get_pre_commit_dir_path()):
|
||||
local.path(get_pre_commit_dir_path()).mkdir()
|
||||
|
||||
with local.cwd(get_pre_commit_dir_path()):
|
||||
yield
|
||||
|
|
@ -1,46 +1,44 @@
|
|||
import contextlib
|
||||
|
||||
import contextlib
|
||||
from plumbum import local
|
||||
from pre_commit import git
|
||||
|
||||
from pre_commit.hooks_workspace import in_hooks_workspace
|
||||
|
||||
|
||||
class RepoInstaller(object):
|
||||
def __init__(self, git_repo_path, sha):
|
||||
self.git_repo_path = git_repo_path
|
||||
self.sha = sha
|
||||
def __init__(self, repo_config):
|
||||
self.repo_config = repo_config
|
||||
|
||||
@property
|
||||
def repo_url(self):
|
||||
return self.repo_config['repo']
|
||||
|
||||
@property
|
||||
def sha(self):
|
||||
return self.repo_config['sha']
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_checkout(self):
|
||||
with local.cwd(git.get_pre_commit_dir_path()):
|
||||
with in_hooks_workspace():
|
||||
with local.cwd(self.sha):
|
||||
yield
|
||||
|
||||
def create(self):
|
||||
git.create_pre_commit_package_dir()
|
||||
|
||||
with local.cwd(git.get_pre_commit_dir_path()):
|
||||
with in_hooks_workspace():
|
||||
if local.path(self.sha).exists():
|
||||
# Project already exists, no reason to re-create it
|
||||
return
|
||||
|
||||
local['git']['clone', self.git_repo_path, self.sha]()
|
||||
local['git']['clone', self.repo_url, self.sha]()
|
||||
with self.in_checkout():
|
||||
local['git']['checkout', self.sha]()
|
||||
|
||||
def install(self):
|
||||
# Create if we have not already
|
||||
self.create()
|
||||
# TODO: need to take in the config here and determine if we actually
|
||||
# need to run any installers (and what languages to install)
|
||||
with self.in_checkout():
|
||||
if local.path('setup.py').exists():
|
||||
local['virtualenv']['py_env']()
|
||||
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
||||
|
||||
|
||||
def create_repo_in_env(git_repo_path, sha):
|
||||
project = RepoInstaller(git_repo_path, sha)
|
||||
project.create()
|
||||
|
||||
def install_pre_commit(git_repo_path, sha):
|
||||
project = RepoInstaller(git_repo_path, sha)
|
||||
project.create()
|
||||
project.install()
|
||||
Loading…
Add table
Add a link
Reference in a new issue