Skip to content

Commit c6b4874

Browse files
committed
fetch tag pointing to HEAD in shallow clone
1 parent 8416413 commit c6b4874

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

pre_commit/store.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pre_commit import git
1616
from pre_commit.util import CalledProcessError
1717
from pre_commit.util import clean_path_on_failure
18-
from pre_commit.util import cmd_output_b
18+
from pre_commit.util import cmd_output
1919
from pre_commit.util import resource_text
2020

2121

@@ -174,19 +174,32 @@ def _get_result() -> str | None:
174174

175175
return directory
176176

177-
def _complete_clone(self, ref: str, git_cmd: Callable[..., None]) -> None:
177+
def _complete_clone(
178+
self, ref: str,
179+
git_cmd: Callable[..., tuple[int, str, str | None]],
180+
) -> None:
178181
"""Perform a complete clone of a repository and its submodules """
179182

180183
git_cmd('fetch', 'origin', '--tags')
181184
git_cmd('checkout', ref)
182185
git_cmd('submodule', 'update', '--init', '--recursive')
183186

184-
def _shallow_clone(self, ref: str, git_cmd: Callable[..., None]) -> None:
187+
def _shallow_clone(
188+
self, ref: str,
189+
git_cmd: Callable[..., tuple[int, str, str | None]],
190+
) -> None:
185191
"""Perform a shallow clone of a repository and its submodules """
186192

187193
git_config = 'protocol.version=2'
188194
git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1')
189195
git_cmd('checkout', 'FETCH_HEAD')
196+
rev = git_cmd('rev-parse', 'FETCH_HEAD')[1].strip()
197+
lines = git_cmd('ls-remote', '--heads', 'origin')[1].splitlines()
198+
for line in lines:
199+
if line.startswith(rev):
200+
tag_name = line.rsplit('/', 1)[-1]
201+
git_cmd('fetch', 'origin', 'tag', tag_name, '--no-tags')
202+
break
190203
git_cmd(
191204
'-c', git_config, 'submodule', 'update', '--init', '--recursive',
192205
'--depth=1',
@@ -199,8 +212,8 @@ def clone_strategy(directory: str) -> None:
199212
git.init_repo(directory, repo)
200213
env = git.no_git_env()
201214

202-
def _git_cmd(*args: str) -> None:
203-
cmd_output_b('git', *args, cwd=directory, env=env)
215+
def _git_cmd(*args: str) -> tuple[int, str, str | None]:
216+
return cmd_output('git', *args, cwd=directory, env=env)
204217

205218
try:
206219
self._shallow_clone(ref, _git_cmd)

0 commit comments

Comments
 (0)