Refactored how the installer works · precommit/pre-commit@abea886 · GitHub
Skip to content

Commit abea886

Browse files
committed
Refactored how the installer works
1 parent 8b0247e commit abea886

8 files changed

Lines changed: 105 additions & 70 deletions

File tree

pre_commit/constants.py

Lines changed: 1 addition & 1 deletion

pre_commit/git.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import os
22
import pkg_resources
33

4-
import pre_commit.constants as C
54
from plumbum import local
65

76

7+
# TODO: optimization: memoize based on local.cwd.getpath()
88
def get_root():
99
return local['git']['rev-parse', '--show-toplevel']().strip()
1010

11+
1112
def get_pre_commit_path():
1213
return os.path.join(get_root(), '.git/hooks/pre-commit')
1314

1415

15-
def get_pre_commit_dir_path():
16-
return os.path.join(get_root(), C.PRE_COMMIT_DIR)
17-
18-
def create_pre_commit_package_dir():
19-
local.path(get_pre_commit_dir_path()).mkdir()
20-
2116
def create_pre_commit():
2217
path = get_pre_commit_path()
2318
pre_commit_file = pkg_resources.resource_filename('pre_commit', 'resources/pre-commit.sh')
@@ -28,4 +23,6 @@ def remove_pre_commit():
2823
local.path(get_pre_commit_path()).delete()
2924

3025

31-
26+
def get_head_sha(git_repo_path):
27+
with local.cwd(git_repo_path):
28+
return (local['git']['rev-parse', 'HEAD'])().strip()

pre_commit/hooks_workspace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import contextlib
3+
import os.path
4+
from plumbum import local
5+
6+
import pre_commit.constants as C
7+
from pre_commit import git
8+
9+
10+
def get_pre_commit_dir_path():
11+
return os.path.join(git.get_root(), C.HOOKS_WORKSPACE)
12+
13+
@contextlib.contextmanager
14+
def in_hooks_workspace():
15+
"""Change into the hooks workspace. If it does not exist create it."""
16+
if not os.path.exists(get_pre_commit_dir_path()):
17+
local.path(get_pre_commit_dir_path()).mkdir()
18+
19+
with local.cwd(get_pre_commit_dir_path()):
20+
yield

pre_commit/repo_installer.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
import contextlib
21

2+
import contextlib
33
from plumbum import local
4-
from pre_commit import git
4+
5+
from pre_commit.hooks_workspace import in_hooks_workspace
56

67

78
class RepoInstaller(object):
8-
def __init__(self, git_repo_path, sha):
9-
self.git_repo_path = git_repo_path
10-
self.sha = sha
9+
def __init__(self, repo_config):
10+
self.repo_config = repo_config
11+
12+
@property
13+
def repo_url(self):
14+
return self.repo_config['repo']
15+
16+
@property
17+
def sha(self):
18+
return self.repo_config['sha']
1119

1220
@contextlib.contextmanager
1321
def in_checkout(self):
14-
with local.cwd(git.get_pre_commit_dir_path()):
22+
with in_hooks_workspace():
1523
with local.cwd(self.sha):
1624
yield
1725

1826
def create(self):
19-
git.create_pre_commit_package_dir()
20-
21-
with local.cwd(git.get_pre_commit_dir_path()):
27+
with in_hooks_workspace():
2228
if local.path(self.sha).exists():
2329
# Project already exists, no reason to re-create it
2430
return
2531

26-
local['git']['clone', self.git_repo_path, self.sha]()
32+
local['git']['clone', self.repo_url, self.sha]()
2733
with self.in_checkout():
2834
local['git']['checkout', self.sha]()
2935

3036
def install(self):
37+
# Create if we have not already
38+
self.create()
3139
# TODO: need to take in the config here and determine if we actually
3240
# need to run any installers (and what languages to install)
3341
with self.in_checkout():
3442
if local.path('setup.py').exists():
3543
local['virtualenv']['py_env']()
3644
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
37-
38-
39-
def create_repo_in_env(git_repo_path, sha):
40-
project = RepoInstaller(git_repo_path, sha)
41-
project.create()
42-
43-
def install_pre_commit(git_repo_path, sha):
44-
project = RepoInstaller(git_repo_path, sha)
45-
project.create()
46-
project.install()

tests/conftest.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from __future__ import absolute_import
12

3+
import jsonschema
24
import pytest
3-
import pre_commit.constants as C
5+
import time
46
from plumbum import local
57

8+
import pre_commit.constants as C
9+
from pre_commit import git
10+
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
11+
612

713
@pytest.yield_fixture
814
def empty_git_dir(tmpdir):
@@ -11,10 +17,9 @@ def empty_git_dir(tmpdir):
1117
yield tmpdir.strpath
1218

1319

14-
1520
def add_and_commit():
1621
local['git']['add', '.']()
17-
local['git']['commit', '-m', 'random commit']()
22+
local['git']['commit', '-m', 'random commit {0}'.format(time.time())]()
1823

1924

2025
@pytest.yield_fixture
@@ -42,8 +47,7 @@ def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
4247

4348
@pytest.yield_fixture
4449
def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
45-
local.path('setup.py').write(
46-
"""
50+
local.path('setup.py').write("""
4751
from setuptools import find_packages
4852
from setuptools import setup
4953
@@ -53,7 +57,7 @@ def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
5357
packages=find_packages('.'),
5458
entry_points={
5559
'console_scripts': [
56-
'entry = foo.main:func'
60+
'foo = foo.main:func'
5761
],
5862
}
5963
)
@@ -66,15 +70,28 @@ def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
6670

6771
with local.cwd(foo_module):
6872
local.path('__init__.py').write('')
69-
local.path('main.py').write(
70-
"""
71-
73+
local.path('main.py').write("""
7274
def func():
7375
return 0
74-
7576
"""
7677
)
7778

7879
add_and_commit()
7980

8081
yield dummy_pre_commit_hooks_git_repo
82+
83+
84+
@pytest.fixture
85+
def config_for_python_pre_commit_git_repo(python_pre_commit_git_repo):
86+
config = {
87+
'repo': python_pre_commit_git_repo,
88+
'sha': git.get_head_sha(python_pre_commit_git_repo),
89+
'hooks': [{
90+
'id': 'foo',
91+
'files': '*.py',
92+
}],
93+
}
94+
95+
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
96+
97+
return config

tests/git_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def test_get_root(empty_git_dir):
1616

1717

1818
def test_get_pre_commit_path(empty_git_dir):
19-
assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(empty_git_dir)
19+
assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(
20+
empty_git_dir,
21+
)
2022

2123

2224
def test_create_pre_commit(empty_git_dir):

tests/hooks_workspace_test.py

Whitespace-only changes.

tests/repo_installer_test.py

Lines changed: 31 additions & 30 deletions

0 commit comments

Comments
 (0)