Install local hooks in the ~/.pre-commit folder

This commit is contained in:
Thierry Deo 2017-02-14 23:47:02 +01:00
parent 377cffbd27
commit 9a8fb17070
15 changed files with 103 additions and 75 deletions

View file

@ -70,6 +70,7 @@ def validate_config_extra(config):
raise InvalidConfigError( raise InvalidConfigError(
'"sha" property provided for local hooks' '"sha" property provided for local hooks'
) )
repo['sha'] = 'local'
elif 'sha' not in repo: elif 'sha' not in repo:
raise InvalidConfigError( raise InvalidConfigError(
'Missing "sha" field for repository {}'.format(repo['repo']) 'Missing "sha" field for repository {}'.format(repo['repo'])

View file

@ -30,7 +30,7 @@ def _update_repo(repo_config, runner, tags_only):
Args: Args:
repo_config - A config for a repository 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): with cwd(repo.repo_path_getter.repo_path):
cmd_output('git', 'fetch') cmd_output('git', 'fetch')
@ -51,7 +51,7 @@ def _update_repo(repo_config, runner, tags_only):
# Construct a new config with the head sha # Construct a new config with the head sha
new_config = OrderedDict(repo_config) new_config = OrderedDict(repo_config)
new_config['sha'] = rev 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 # See if any of our hooks were deleted with the new commits
hooks = {hook['id'] for hook in repo.repo_config['hooks']} hooks = {hook['id'] for hook in repo.repo_config['hooks']}

View file

@ -74,7 +74,7 @@ def install_environment(
else: else:
venv_cmd.extend(['-p', os.path.realpath(sys.executable)]) venv_cmd.extend(['-p', os.path.realpath(sys.executable)])
repo_cmd_runner.run(venv_cmd, cwd='/') 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 to_install += additional_dependencies
with in_env(repo_cmd_runner, version): with in_env(repo_cmd_runner, version):
helpers.run_setup_cmd( helpers.run_setup_cmd(

View file

@ -11,7 +11,6 @@ import pkg_resources
from cached_property import cached_property from cached_property import cached_property
from pre_commit import five 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_config import is_local_hooks
from pre_commit.clientlib.validate_manifest import MANIFEST_JSON_SCHEMA from pre_commit.clientlib.validate_manifest import MANIFEST_JSON_SCHEMA
from pre_commit.jsonschema_extensions import apply_defaults from pre_commit.jsonschema_extensions import apply_defaults
@ -38,13 +37,13 @@ class Repository(object):
self.__installed = False self.__installed = False
@classmethod @classmethod
def create(cls, config, store): def create(cls, config, store, owner):
if is_local_hooks(config):
return LocalRepository(config)
else:
repo_path_getter = store.get_repo_path_getter( repo_path_getter = store.get_repo_path_getter(
config['repo'], config['sha'] config['repo'], config['sha'], owner
) )
if is_local_hooks(config):
return LocalRepository(config, repo_path_getter)
else:
return cls(config, repo_path_getter) return cls(config, repo_path_getter)
@cached_property @cached_property
@ -198,9 +197,6 @@ class Repository(object):
class LocalRepository(Repository): class LocalRepository(Repository):
def __init__(self, repo_config):
super(LocalRepository, self).__init__(repo_config, None)
@cached_property @cached_property
def hooks(self): def hooks(self):
return tuple( return tuple(
@ -208,14 +204,6 @@ class LocalRepository(Repository):
for hook in self.repo_config['hooks'] 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): class _UniqueList(list):
def __init__(self): def __init__(self):

View file

@ -41,7 +41,9 @@ class Runner(object):
def repositories(self): def repositories(self):
"""Returns a tuple of the configured repositories.""" """Returns a tuple of the configured repositories."""
config = load_config(self.config_file_path) 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: for repository in repositories:
repository.require_installed() repository.require_installed()
return repositories return repositories

View file

@ -9,6 +9,7 @@ import tempfile
from cached_property import cached_property 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.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import clean_path_on_failure from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
@ -45,6 +46,17 @@ class Store(object):
def repo_path(self): def repo_path(self):
return self._store.clone(self._repo, self._ref) 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): def __init__(self, directory=None):
if directory is None: if directory is None:
directory = self.get_default_directory() directory = self.get_default_directory()
@ -97,19 +109,31 @@ class Store(object):
self._create() self._create()
self.__created = True self.__created = True
def clone(self, url, ref): def find_local_path(self, repo, ref):
"""Clone the given url and checkout the specific ref."""
self.require_created()
# Check if we already exist
with sqlite3.connect(self.db_path) as db: with sqlite3.connect(self.db_path) as db:
result = db.execute( result = db.execute(
'SELECT path FROM repos WHERE repo = ? AND ref = ?', 'SELECT path FROM repos WHERE repo = ? AND ref = ?',
[url, ref], [repo, ref],
).fetchone() ).fetchone()
if result: if result:
return result[0] 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)) logger.info('Initializing environment for {}.'.format(url))
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory) 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()) cmd_output('git', 'reset', ref, '--hard', env=no_git_env())
# Update our db with the created repo # Update our db with the created repo
with sqlite3.connect(self.db_path) as db: self.store_path(url, sha, dir)
db.execute(
'INSERT INTO repos (repo, ref, path) VALUES (?, ?, ?)',
[url, ref, dir],
)
return dir return dir
def get_repo_path_getter(self, repo, ref): def initialize_local_repo(self, owner):
return self.RepoPathGetter(repo, ref, self) """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 @cached_property
def cmd_runner(self): def cmd_runner(self):

View file

@ -50,11 +50,12 @@ def _test_hook_repo(
): ):
path = make_repo(tempdir_factory, repo_path) path = make_repo(tempdir_factory, repo_path)
config = make_config_from_repo(path, **(config_kwargs or {})) config = make_config_from_repo(path, **(config_kwargs or {}))
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
hook_dict = [ hook_dict = [
hook for repo_hook_id, hook in repo.hooks if repo_hook_id == hook_id hook for repo_hook_id, hook in repo.hooks if repo_hook_id == hook_id
][0] ][0]
ret = repo.run_hook(hook_dict, args) ret = repo.run_hook(hook_dict, args)
print ret
assert ret[0] == expected_return_code assert ret[0] == expected_return_code
assert ret[1].replace(b'\r\n', b'\n') == expected assert ret[1].replace(b'\r\n', b'\n') == expected
@ -108,7 +109,7 @@ def test_switch_language_versions_doesnt_clobber(tempdir_factory, store):
config = make_config_from_repo( config = make_config_from_repo(
path, hooks=[{'id': 'python3-hook', 'language_version': version}], path, hooks=[{'id': 'python3-hook', 'language_version': version}],
) )
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
hook_dict, = [ hook_dict, = [
hook hook
for repo_hook_id, hook in repo.hooks for repo_hook_id, hook in repo.hooks
@ -462,7 +463,7 @@ def test_repo_url(mock_repo_config):
def test_languages(tempdir_factory, store): def test_languages(tempdir_factory, store):
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
assert repo.languages == {('python', 'default')} assert repo.languages == {('python', 'default')}
@ -471,7 +472,7 @@ def test_additional_dependencies(tempdir_factory, store):
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['pep8'] config['hooks'][0]['additional_dependencies'] = ['pep8']
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
assert repo.additional_dependencies['python']['default'] == ['pep8'] assert repo.additional_dependencies['python']['default'] == ['pep8']
@ -483,7 +484,7 @@ def test_additional_dependencies_duplicated(
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = [ config['hooks'][0]['additional_dependencies'] = [
'thread_safe', 'tins', 'thread_safe'] 'thread_safe', 'tins', 'thread_safe']
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
assert repo.additional_dependencies['ruby']['default'] == [ assert repo.additional_dependencies['ruby']['default'] == [
'thread_safe', 'tins'] 'thread_safe', 'tins']
@ -493,7 +494,7 @@ def test_additional_python_dependencies_installed(tempdir_factory, store):
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['mccabe'] config['hooks'][0]['additional_dependencies'] = ['mccabe']
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
with python.in_env(repo.cmd_runner, 'default'): with python.in_env(repo.cmd_runner, 'default'):
output = cmd_output('pip', 'freeze', '-l')[1] output = cmd_output('pip', 'freeze', '-l')[1]
@ -505,11 +506,11 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
# Run the repo once without additional_dependencies # Run the repo once without additional_dependencies
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
# Now run it with additional_dependencies # Now run it with additional_dependencies
config['hooks'][0]['additional_dependencies'] = ['mccabe'] config['hooks'][0]['additional_dependencies'] = ['mccabe']
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
# We should see our additional dependency installed # We should see our additional dependency installed
with python.in_env(repo.cmd_runner, 'default'): with python.in_env(repo.cmd_runner, 'default'):
@ -526,7 +527,7 @@ def test_additional_ruby_dependencies_installed(
path = make_repo(tempdir_factory, 'ruby_hooks_repo') path = make_repo(tempdir_factory, 'ruby_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins'] config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins']
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
with ruby.in_env(repo.cmd_runner, 'default'): with ruby.in_env(repo.cmd_runner, 'default'):
output = cmd_output('gem', 'list', '--local')[1] output = cmd_output('gem', 'list', '--local')[1]
@ -544,7 +545,7 @@ def test_additional_node_dependencies_installed(
config = make_config_from_repo(path) config = make_config_from_repo(path)
# Careful to choose a small package that's not depped by npm # Careful to choose a small package that's not depped by npm
config['hooks'][0]['additional_dependencies'] = ['lodash'] config['hooks'][0]['additional_dependencies'] = ['lodash']
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
with node.in_env(repo.cmd_runner, 'default'): with node.in_env(repo.cmd_runner, 'default'):
cmd_output('npm', 'config', 'set', 'global', 'true') cmd_output('npm', 'config', 'set', 'global', 'true')
@ -561,7 +562,7 @@ def test_additional_golang_dependencies_installed(
# A small go package # A small go package
deps = ['github.com/golang/example/hello'] deps = ['github.com/golang/example/hello']
config['hooks'][0]['additional_dependencies'] = deps config['hooks'][0]['additional_dependencies'] = deps
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
binaries = os.listdir(repo.cmd_runner.path( binaries = os.listdir(repo.cmd_runner.path(
helpers.environment_dir(golang.ENVIRONMENT_DIR, 'default'), 'bin', helpers.environment_dir(golang.ENVIRONMENT_DIR, 'default'), 'bin',
@ -579,7 +580,8 @@ def test_install_local_ruby_hook(
): # pragma: no cover (non-windows) ): # pragma: no cover (non-windows)
config = config_with_local_hooks('ruby') config = config_with_local_hooks('ruby')
config['hooks'][0]['additional_dependencies'] = ['thread_safe'] config['hooks'][0]['additional_dependencies'] = ['thread_safe']
repo = Repository.create(config, store) validate_config_extra([config])
repo = Repository.create(config, store, '/path/to/repo/')
repo.require_installed() repo.require_installed()
with ruby.in_env(repo.cmd_runner, 'default'): with ruby.in_env(repo.cmd_runner, 'default'):
output = cmd_output('gem', 'list', '--local')[1] output = cmd_output('gem', 'list', '--local')[1]
@ -592,7 +594,8 @@ def test_install_local_python_hook(
): # pragma: no cover (non-windows) ): # pragma: no cover (non-windows)
config = config_with_local_hooks('python') config = config_with_local_hooks('python')
config['hooks'][0]['additional_dependencies'] = ['mccabe'] config['hooks'][0]['additional_dependencies'] = ['mccabe']
repo = Repository.create(config, store) validate_config_extra([config])
repo = Repository.create(config, store, '/path/to/repo/')
repo.require_installed() repo.require_installed()
with python.in_env(repo.cmd_runner, 'default'): with python.in_env(repo.cmd_runner, 'default'):
output = cmd_output('pip', 'freeze', '-l')[1] output = cmd_output('pip', 'freeze', '-l')[1]
@ -602,7 +605,7 @@ def test_install_local_python_hook(
def test_reinstall(tempdir_factory, store, log_info_mock): def test_reinstall(tempdir_factory, store, log_info_mock):
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
# We print some logging during clone (1) + install (3) # We print some logging during clone (1) + install (3)
assert log_info_mock.call_count == 4 assert log_info_mock.call_count == 4
@ -611,7 +614,7 @@ def test_reinstall(tempdir_factory, store, log_info_mock):
repo.require_installed() repo.require_installed()
assert log_info_mock.call_count == 0 assert log_info_mock.call_count == 0
# Reinstall on another run should not trigger another install # Reinstall on another run should not trigger another install
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
assert log_info_mock.call_count == 0 assert log_info_mock.call_count == 0
@ -620,7 +623,7 @@ def test_control_c_control_c_on_install(tempdir_factory, store):
"""Regression test for #186.""" """Regression test for #186."""
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
hook = repo.hooks[0][1] hook = repo.hooks[0][1]
class MyKeyboardInterrupt(KeyboardInterrupt): class MyKeyboardInterrupt(KeyboardInterrupt):
@ -657,7 +660,7 @@ def test_really_long_file_paths(tempdir_factory, store):
config = make_config_from_repo(path) config = make_config_from_repo(path)
with cwd(really_long_path): with cwd(really_long_path):
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
repo.require_installed() repo.require_installed()
@ -666,11 +669,11 @@ def test_config_overrides_repo_specifics(tempdir_factory, store):
path = make_repo(tempdir_factory, 'script_hooks_repo') path = make_repo(tempdir_factory, 'script_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
assert repo.hooks[0][1]['files'] == '' assert repo.hooks[0][1]['files'] == ''
# Set the file regex to something else # Set the file regex to something else
config['hooks'][0]['files'] = '\\.sh$' config['hooks'][0]['files'] = '\\.sh$'
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
assert repo.hooks[0][1]['files'] == '\\.sh$' assert repo.hooks[0][1]['files'] == '\\.sh$'
@ -690,28 +693,20 @@ def test_tags_on_repositories(in_tmpdir, tempdir_factory, store):
) )
repo_1 = Repository.create( repo_1 = Repository.create(
make_config_from_repo(git_dir_1, sha=tag), store, make_config_from_repo(git_dir_1, sha=tag), store, git_dir_1
) )
ret = repo_1.run_hook(repo_1.hooks[0][1], ['-L']) ret = repo_1.run_hook(repo_1.hooks[0][1], ['-L'])
assert ret[0] == 0 assert ret[0] == 0
assert ret[1].strip() == _norm_pwd(in_tmpdir) assert ret[1].strip() == _norm_pwd(in_tmpdir)
repo_2 = Repository.create( repo_2 = Repository.create(
make_config_from_repo(git_dir_2, sha=tag), store, make_config_from_repo(git_dir_2, sha=tag), store, git_dir_2
) )
ret = repo_2.run_hook(repo_2.hooks[0][1], ['bar']) ret = repo_2.run_hook(repo_2.hooks[0][1], ['bar'])
assert ret[0] == 0 assert ret[0] == 0
assert ret[1] == b'bar\nHello World\n' assert ret[1] == b'bar\nHello World\n'
def test_local_repository():
config = config_with_local_hooks()
local_repo = Repository.create(config, 'dummy')
with pytest.raises(NotImplementedError):
local_repo.manifest
assert len(local_repo.hooks) == 1
@pytest.yield_fixture @pytest.yield_fixture
def fake_log_handler(): def fake_log_handler():
handler = mock.Mock(level=logging.INFO) handler = mock.Mock(level=logging.INFO)
@ -725,7 +720,7 @@ def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
path = make_repo(tempdir_factory, 'script_hooks_repo') path = make_repo(tempdir_factory, 'script_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['id'] = 'i-dont-exist' config['hooks'][0]['id'] = 'i-dont-exist'
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
repo.install() repo.install()
assert fake_log_handler.handle.call_args[0][0].msg == ( assert fake_log_handler.handle.call_args[0][0].msg == (
@ -740,7 +735,7 @@ def test_too_new_version(tempdir_factory, store, fake_log_handler):
with modify_manifest(path) as manifest: with modify_manifest(path) as manifest:
manifest[0]['minimum_pre_commit_version'] = '999.0.0' manifest[0]['minimum_pre_commit_version'] = '999.0.0'
config = make_config_from_repo(path) config = make_config_from_repo(path)
repo = Repository.create(config, store) repo = Repository.create(config, store, path)
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
repo.install() repo.install()
msg = fake_log_handler.handle.call_args[0][0].msg msg = fake_log_handler.handle.call_args[0][0].msg
@ -762,4 +757,4 @@ def test_versions_ok(tempdir_factory, store, version):
manifest[0]['minimum_pre_commit_version'] = version manifest[0]['minimum_pre_commit_version'] = version
config = make_config_from_repo(path) config = make_config_from_repo(path)
# Should succeed # Should succeed
Repository.create(config, store).install() Repository.create(config, store, path).install()