Skip to content

Commit 6498321

Browse files
committed
added more integration with git repo creation
1 parent ed16271 commit 6498321

File tree

2 files changed

+142
-12
lines changed

2 files changed

+142
-12
lines changed

pre_commit/git.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
22
import pkg_resources
3+
import contextlib
4+
5+
import pre_commit.constants as C
36
from plumbum import local
47

58

@@ -11,11 +14,11 @@ def get_pre_commit_path():
1114
return os.path.join(get_root(), '.git/hooks/pre-commit')
1215

1316

14-
def get_env_path():
15-
return os.path.join(get_root(), '.pre-commit')
17+
def get_pre_commit_dir_path():
18+
return os.path.join(get_root(), C.PRE_COMMIT_DIR)
1619

1720
def create_pre_commit_package_dir():
18-
local.path(get_root() + '/.pre-commit').mkdir()
21+
local.path(get_pre_commit_dir_path()).mkdir()
1922

2023
def create_pre_commit():
2124
path = get_pre_commit_path()
@@ -26,11 +29,40 @@ def create_pre_commit():
2629
def remove_pre_commit():
2730
local.path(get_pre_commit_path()).delete()
2831

29-
def create_repo_in_env(name, git_repo_path):
30-
create_pre_commit_package_dir()
3132

32-
env_path = get_env_path()
33+
class PreCommitProject(object):
34+
35+
def __init__(self, git_repo_path, sha):
36+
self.git_repo_path = git_repo_path
37+
self.sha = sha
38+
39+
@contextlib.contextmanager
40+
def in_checkout(self):
41+
with local.cwd(get_pre_commit_dir_path()):
42+
with local.cwd(self.sha):
43+
yield
44+
45+
def create(self):
46+
create_pre_commit_package_dir()
47+
48+
with local.cwd(get_pre_commit_dir_path()):
49+
local['git']['clone', self.git_repo_path, self.sha]()
50+
with self.in_checkout():
51+
local['git']['checkout', self.sha]()
52+
53+
def install(self):
54+
with self.in_checkout():
55+
if local.path('setup.py').exists():
56+
local['virtualenv']['py_env']()
57+
local['bash'][local['pip']['install', '.']]
58+
59+
def create_repo_in_env(git_repo_path, sha):
60+
project = PreCommitProject(git_repo_path, sha)
61+
project.create()
62+
63+
def install_pre_commit(git_repo_path, sha):
64+
project = PreCommitProject(git_repo_path, sha)
65+
project.create()
66+
project.install()
67+
3368

34-
with local.cwd(env_path):
35-
local['git']['clone', git_repo_path, name]()
36-
print local.cwd.getpath()

tests/git_test.py

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
from plumbum import local
66
from pre_commit import git
77

8+
import pre_commit.constants as C
9+
10+
def add_and_commit():
11+
local['git']['add', '.']()
12+
local['git']['commit', '-m', 'random commit']()
13+
14+
15+
def get_sha(git_repo):
16+
with local.cwd(git_repo):
17+
return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n')
18+
819

920
@pytest.yield_fixture
1021
def empty_git_dir(tmpdir):
@@ -16,11 +27,66 @@ def empty_git_dir(tmpdir):
1627
@pytest.yield_fixture
1728
def dummy_git_repo(empty_git_dir):
1829
local['touch']['dummy']()
19-
local['git']['add', 'dummy']()
20-
local['git']['commit', '-m', 'dummy commit']()
30+
add_and_commit()
2131

2232
yield empty_git_dir
2333

34+
35+
@pytest.yield_fixture
36+
def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
37+
local.path(C.MANIFEST_FILE).write("""
38+
hooks:
39+
-
40+
id: foo
41+
name: Foo
42+
entry: foo
43+
language: python>2.6
44+
""")
45+
46+
add_and_commit()
47+
48+
yield dummy_git_repo
49+
50+
@pytest.yield_fixture
51+
def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
52+
local.path('setup.py').write(
53+
"""
54+
from setuptools import find_packages
55+
from setuptools import setup
56+
57+
setup(
58+
name='Foo',
59+
version='0.0.0',
60+
packages=find_packages('.'),
61+
entry_points={
62+
'console_scripts': [
63+
'entry = foo.main:func'
64+
],
65+
}
66+
)
67+
"""
68+
)
69+
70+
foo_module = local.path('foo')
71+
72+
foo_module.mkdir()
73+
74+
with local.cwd(foo_module):
75+
local.path('__init__.py').write('')
76+
local.path('main.py').write(
77+
"""
78+
79+
def func():
80+
return 0
81+
82+
"""
83+
)
84+
85+
add_and_commit()
86+
87+
yield dummy_pre_commit_hooks_git_repo
88+
89+
2490
def test_get_root(empty_git_dir):
2591
assert git.get_root() == empty_git_dir
2692

@@ -52,4 +118,36 @@ def test_remove_pre_commit(empty_git_dir):
52118

53119

54120
def test_create_repo_in_env(empty_git_dir, dummy_git_repo):
55-
git.create_repo_in_env('pre-commit', dummy_git_repo)
121+
sha = get_sha(dummy_git_repo)
122+
git.create_repo_in_env(dummy_git_repo, sha)
123+
assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha))
124+
125+
126+
def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo):
127+
sha = get_sha(python_pre_commit_git_repo)
128+
git.install_pre_commit(python_pre_commit_git_repo, sha)
129+
assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env'))
130+
131+
132+
# def install_config():
133+
# config = [
134+
# {
135+
# 'repo': 'repo1',
136+
# 'sha': 'dsfjksljfslkf',
137+
# 'hooks': [
138+
# {
139+
# 'id': 'script',
140+
# 'args': [
141+
# {
142+
# 'type': 'files',
143+
# 'opt': '*.py'
144+
# },
145+
# ]
146+
# }
147+
# ],
148+
# },
149+
# ]
150+
# for repo in config:
151+
# clone(repo)
152+
# for
153+

0 commit comments

Comments
 (0)