77from pre_commit .hooks_workspace import in_hooks_workspace
88from pre_commit .languages .all import languages
99from pre_commit .ordereddict import OrderedDict
10+ from pre_commit .prefixed_command_runner import PrefixedCommandRunner
1011from pre_commit .util import cached_property
1112
1213
1314class Repository (object ):
1415 def __init__ (self , repo_config ):
1516 self .repo_config = repo_config
17+ self .__created = False
18+ self .__installed = False
1619
1720 @cached_property
1821 def repo_url (self ):
@@ -43,13 +46,17 @@ def manifest(self):
4346 for hook in load_manifest (C .MANIFEST_FILE )
4447 )
4548
46- @contextlib .contextmanager
47- def in_checkout (self ):
48- with in_hooks_workspace ():
49- # SMELL:
50- self .create ()
51- with local .cwd (self .sha ):
52- yield
49+ def get_cmd_runner (self , hooks_cmd_runner ):
50+ return PrefixedCommandRunner .from_command_runner (
51+ hooks_cmd_runner , self .sha ,
52+ )
53+
54+ def require_created (self ):
55+ if self .__created :
56+ return
57+
58+ self .create ()
59+ self .__created = True
5360
5461 def create (self ):
5562 with in_hooks_workspace ():
@@ -61,13 +68,42 @@ def create(self):
6168 with self .in_checkout ():
6269 local ['git' ]['checkout' , self .sha ]()
6370
64- def install (self ):
65- with self .in_checkout ():
66- for language in C .SUPPORTED_LANGUAGES :
67- if language in self .languages :
68- languages [language ].install_environment ()
71+ def require_installed (self , cmd_runner ):
72+ if self .__installed :
73+ return
6974
70- def run_hook (self , hook_id , file_args ):
71- with self .in_checkout ():
72- hook = self .hooks [hook_id ]
73- return languages [hook ['language' ]].run_hook (hook , file_args )
75+ self .install (cmd_runner )
76+
77+ def install (self , cmd_runner ):
78+ """Install the hook repository.
79+
80+ Args:
81+ cmd_runner - A `PrefixedCommandRunner` bound to the hooks workspace
82+ """
83+ self .require_created ()
84+ repo_cmd_runner = self .get_cmd_runner (cmd_runner )
85+ for language in C .SUPPORTED_LANGUAGES :
86+ if language in self .languages :
87+ languages [language ].install_environment (repo_cmd_runner )
88+
89+ @contextlib .contextmanager
90+ def in_checkout (self ):
91+ self .require_created ()
92+ with in_hooks_workspace ():
93+ with local .cwd (self .sha ):
94+ yield
95+
96+ def run_hook (self , cmd_runner , hook_id , file_args ):
97+ """Run a hook.
98+
99+ Args:
100+ cmd_runner - A `PrefixedCommandRunner` bound to the hooks workspace
101+ hook_id - Id of the hook
102+ file_args - List of files to run
103+ """
104+ self .require_installed (cmd_runner )
105+ repo_cmd_runner = self .get_cmd_runner (cmd_runner )
106+ hook = self .hooks [hook_id ]
107+ return languages [hook ['language' ]].run_hook (
108+ repo_cmd_runner , hook , file_args ,
109+ )
0 commit comments