1010import pre_commit .constants as C
1111from pre_commit import file_lock
1212from pre_commit import git
13+ from pre_commit .util import CalledProcessError
1314from pre_commit .util import clean_path_on_failure
1415from pre_commit .util import cmd_output
1516from pre_commit .util import resource_text
@@ -121,10 +122,7 @@ def _get_result():
121122 return result
122123
123124 logger .info ('Initializing environment for {}.' .format (repo ))
124-
125- directory = tempfile .mkdtemp (prefix = 'repo' , dir = self .directory )
126- with clean_path_on_failure (directory ):
127- make_strategy (directory )
125+ directory = make_strategy ()
128126
129127 # Update our db with the created repo
130128 with self .connect () as db :
@@ -134,19 +132,50 @@ def _get_result():
134132 )
135133 return directory
136134
137- def clone (self , repo , ref , deps = ()):
138- """Clone the given url and checkout the specific ref."""
139- def clone_strategy (directory ):
140- env = git .no_git_env ()
135+ def _perform_safe_clone (self , clone_strategy ):
136+ directory = tempfile .mkdtemp (prefix = 'repo' , dir = self .directory )
137+ with clean_path_on_failure (directory ):
138+ clone_strategy (directory )
139+ return directory
141140
142- cmd = ('git' , 'clone' , '--no-checkout' , repo , directory )
143- cmd_output (* cmd , env = env )
141+ def _complete_clone (self , repo , ref , directory ):
142+ """Perform a complete clone of a repository and its submodules """
143+ env = git .no_git_env ()
144144
145- def _git_cmd (* args ):
146- return cmd_output ('git' , * args , cwd = directory , env = env )
145+ cmd = ('git' , 'clone' , '--no-checkout' , repo , directory )
146+ cmd_output (* cmd , env = env )
147+
148+ def _git_cmd (* args ):
149+ return cmd_output ('git' , * args , cwd = directory , env = env )
150+
151+ _git_cmd ('reset' , ref , '--hard' )
152+ _git_cmd ('submodule' , 'update' , '--init' , '--recursive' )
147153
148- _git_cmd ('reset' , ref , '--hard' )
149- _git_cmd ('submodule' , 'update' , '--init' , '--recursive' )
154+ def _shallow_clone (self , repo , ref , directory ):
155+ """Perform a shallow clone of a repository and its submodules """
156+ env = git .no_git_env ()
157+
158+ def _git_cmd (* args ):
159+ return cmd_output ('git' , * args , cwd = directory , env = env )
160+
161+ _git_cmd ('init' , '.' )
162+ _git_cmd ('remote' , 'add' , 'origin' , repo )
163+ _git_cmd ('fetch' , 'origin' , ref , '--depth=1' )
164+ _git_cmd ('checkout' , ref )
165+ _git_cmd ('submodule' , 'update' , '--init' , '--recursive' , '--depth=1' )
166+
167+ def clone (self , repo , ref , deps = ()):
168+ """Clone the given url and checkout the specific ref."""
169+
170+ def clone_strategy ():
171+ try :
172+ def shallow_clone (directory ):
173+ self ._shallow_clone (repo , ref , directory )
174+ return self ._perform_safe_clone (shallow_clone )
175+ except CalledProcessError :
176+ def complete_clone (directory ):
177+ self ._complete_clone (repo , ref , directory )
178+ return self ._perform_safe_clone (complete_clone )
150179
151180 return self ._new_repo (repo , ref , deps , clone_strategy )
152181
@@ -173,8 +202,11 @@ def _git_cmd(*args):
173202 _git_cmd ('add' , '.' )
174203 git .commit (repo = directory )
175204
205+ def make_strategy ():
206+ return self ._perform_safe_clone (make_local_strategy )
207+
176208 return self ._new_repo (
177- 'local' , C .LOCAL_REPO_VERSION , deps , make_local_strategy ,
209+ 'local' , C .LOCAL_REPO_VERSION , deps , make_strategy ,
178210 )
179211
180212 def _create_config_table_if_not_exists (self , db ):
0 commit comments