1- import contextlib
2- import logging
31from asottile .ordereddict import OrderedDict
4- from plumbum import local
52
6- import pre_commit .constants as C
7- from pre_commit import five
8- from pre_commit .clientlib .validate_manifest import load_manifest
9- from pre_commit .hooks_workspace import in_hooks_workspace
103from pre_commit .languages .all import languages
4+ from pre_commit .manifest import Manifest
115from pre_commit .prefixed_command_runner import PrefixedCommandRunner
126from pre_commit .util import cached_property
13- from pre_commit .util import clean_path_on_failure
14-
15-
16- logger = logging .getLogger ('pre_commit' )
177
188
199class Repository (object ):
20- def __init__ (self , repo_config ):
10+ def __init__ (self , repo_config , repo_path_getter ):
2111 self .repo_config = repo_config
22- self .__created = False
12+ self .repo_path_getter = repo_path_getter
2313 self .__installed = False
2414
15+ @classmethod
16+ def create (cls , config , store ):
17+ repo_path_getter = store .get_repo_path_getter (
18+ config ['repo' ], config ['sha' ]
19+ )
20+ return cls (config , repo_path_getter )
21+
2522 @cached_property
2623 def repo_url (self ):
2724 return self .repo_config ['repo' ]
@@ -36,46 +33,22 @@ def languages(self):
3633
3734 @cached_property
3835 def hooks (self ):
36+ # TODO: merging in manifest dicts is a smell imo
3937 return OrderedDict (
40- (hook ['id' ], dict (hook , ** self .manifest [hook ['id' ]]))
38+ (hook ['id' ], dict (hook , ** self .manifest . hooks [hook ['id' ]]))
4139 for hook in self .repo_config ['hooks' ]
4240 )
4341
4442 @cached_property
4543 def manifest (self ):
46- with self .in_checkout ():
47- return dict (
48- (hook ['id' ], hook )
49- for hook in load_manifest (C .MANIFEST_FILE )
50- )
44+ return Manifest (self .repo_path_getter )
5145
5246 def get_cmd_runner (self , hooks_cmd_runner ):
47+ # TODO: this effectively throws away the original cmd runner
5348 return PrefixedCommandRunner .from_command_runner (
54- hooks_cmd_runner , self .sha ,
49+ hooks_cmd_runner , self .repo_path_getter . repo_path ,
5550 )
5651
57- def require_created (self ):
58- if self .__created :
59- return
60-
61- self .create ()
62- self .__created = True
63-
64- def create (self ):
65- with in_hooks_workspace ():
66- if local .path (self .sha ).exists ():
67- # Project already exists, no reason to re-create it
68- return
69-
70- # Checking out environment for the first time
71- logger .info ('Installing environment for {0}.' .format (self .repo_url ))
72- logger .info ('Once installed this environment will be reused.' )
73- logger .info ('This may take a few minutes...' )
74- with clean_path_on_failure (five .u (local .path (self .sha ))):
75- local ['git' ]['clone' , '--no-checkout' , self .repo_url , self .sha ]()
76- with self .in_checkout ():
77- local ['git' ]['checkout' , self .sha ]()
78-
7952 def require_installed (self , cmd_runner ):
8053 if self .__installed :
8154 return
@@ -89,7 +62,6 @@ def install(self, cmd_runner):
8962 Args:
9063 cmd_runner - A `PrefixedCommandRunner` bound to the hooks workspace
9164 """
92- self .require_created ()
9365 repo_cmd_runner = self .get_cmd_runner (cmd_runner )
9466 for language_name in self .languages :
9567 language = languages [language_name ]
@@ -101,13 +73,6 @@ def install(self, cmd_runner):
10173 continue
10274 language .install_environment (repo_cmd_runner )
10375
104- @contextlib .contextmanager
105- def in_checkout (self ):
106- self .require_created ()
107- with in_hooks_workspace ():
108- with local .cwd (self .sha ):
109- yield
110-
11176 def run_hook (self , cmd_runner , hook_id , file_args ):
11277 """Run a hook.
11378
0 commit comments