From c6b4874025dad2d383a63c0f202fb51577d91172 Mon Sep 17 00:00:00 2001 From: gnought <1684105+gnought@users.noreply.github.com> Date: Sat, 7 Feb 2026 00:42:58 +0800 Subject: [PATCH 1/5] fetch tag pointing to HEAD in shallow clone --- pre_commit/store.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pre_commit/store.py b/pre_commit/store.py index dc90c051..e4a841bb 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -15,7 +15,7 @@ from pre_commit import file_lock from pre_commit import git from pre_commit.util import CalledProcessError from pre_commit.util import clean_path_on_failure -from pre_commit.util import cmd_output_b +from pre_commit.util import cmd_output from pre_commit.util import resource_text @@ -174,19 +174,32 @@ class Store: return directory - def _complete_clone(self, ref: str, git_cmd: Callable[..., None]) -> None: + def _complete_clone( + self, ref: str, + git_cmd: Callable[..., tuple[int, str, str | None]], + ) -> None: """Perform a complete clone of a repository and its submodules """ git_cmd('fetch', 'origin', '--tags') git_cmd('checkout', ref) git_cmd('submodule', 'update', '--init', '--recursive') - def _shallow_clone(self, ref: str, git_cmd: Callable[..., None]) -> None: + def _shallow_clone( + self, ref: str, + git_cmd: Callable[..., tuple[int, str, str | None]], + ) -> None: """Perform a shallow clone of a repository and its submodules """ git_config = 'protocol.version=2' git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1') git_cmd('checkout', 'FETCH_HEAD') + rev = git_cmd('rev-parse', 'FETCH_HEAD')[1].strip() + lines = git_cmd('ls-remote', '--heads', 'origin')[1].splitlines() + for line in lines: + if line.startswith(rev): + tag_name = line.rsplit('/', 1)[-1] + git_cmd('fetch', 'origin', 'tag', tag_name, '--no-tags') + break git_cmd( '-c', git_config, 'submodule', 'update', '--init', '--recursive', '--depth=1', @@ -199,8 +212,8 @@ class Store: git.init_repo(directory, repo) env = git.no_git_env() - def _git_cmd(*args: str) -> None: - cmd_output_b('git', *args, cwd=directory, env=env) + def _git_cmd(*args: str) -> tuple[int, str, str | None]: + return cmd_output('git', *args, cwd=directory, env=env) try: self._shallow_clone(ref, _git_cmd) From 19b60a6b487b0a913fd3e21895ec7167abbfef51 Mon Sep 17 00:00:00 2001 From: gnought <1684105+gnought@users.noreply.github.com> Date: Sat, 14 Feb 2026 01:28:27 +0800 Subject: [PATCH 2/5] Revert "fetch tag pointing to HEAD in shallow clone" This reverts commit c6b4874025dad2d383a63c0f202fb51577d91172. --- pre_commit/store.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/pre_commit/store.py b/pre_commit/store.py index e4a841bb..dc90c051 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -15,7 +15,7 @@ from pre_commit import file_lock from pre_commit import git from pre_commit.util import CalledProcessError from pre_commit.util import clean_path_on_failure -from pre_commit.util import cmd_output +from pre_commit.util import cmd_output_b from pre_commit.util import resource_text @@ -174,32 +174,19 @@ class Store: return directory - def _complete_clone( - self, ref: str, - git_cmd: Callable[..., tuple[int, str, str | None]], - ) -> None: + def _complete_clone(self, ref: str, git_cmd: Callable[..., None]) -> None: """Perform a complete clone of a repository and its submodules """ git_cmd('fetch', 'origin', '--tags') git_cmd('checkout', ref) git_cmd('submodule', 'update', '--init', '--recursive') - def _shallow_clone( - self, ref: str, - git_cmd: Callable[..., tuple[int, str, str | None]], - ) -> None: + def _shallow_clone(self, ref: str, git_cmd: Callable[..., None]) -> None: """Perform a shallow clone of a repository and its submodules """ git_config = 'protocol.version=2' git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1') git_cmd('checkout', 'FETCH_HEAD') - rev = git_cmd('rev-parse', 'FETCH_HEAD')[1].strip() - lines = git_cmd('ls-remote', '--heads', 'origin')[1].splitlines() - for line in lines: - if line.startswith(rev): - tag_name = line.rsplit('/', 1)[-1] - git_cmd('fetch', 'origin', 'tag', tag_name, '--no-tags') - break git_cmd( '-c', git_config, 'submodule', 'update', '--init', '--recursive', '--depth=1', @@ -212,8 +199,8 @@ class Store: git.init_repo(directory, repo) env = git.no_git_env() - def _git_cmd(*args: str) -> tuple[int, str, str | None]: - return cmd_output('git', *args, cwd=directory, env=env) + def _git_cmd(*args: str) -> None: + cmd_output_b('git', *args, cwd=directory, env=env) try: self._shallow_clone(ref, _git_cmd) From 5c9f50b681300c71363b581d6b895565b78eab90 Mon Sep 17 00:00:00 2001 From: gnought <1684105+gnought@users.noreply.github.com> Date: Sat, 14 Feb 2026 01:42:13 +0800 Subject: [PATCH 3/5] add local tag in shallow clone --- pre_commit/store.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pre_commit/store.py b/pre_commit/store.py index dc90c051..df811479 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -187,6 +187,7 @@ class Store: git_config = 'protocol.version=2' git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1') git_cmd('checkout', 'FETCH_HEAD') + git_cmd('tag', '-a', '-m', '""', ref, 'FETCH_HEAD') git_cmd( '-c', git_config, 'submodule', 'update', '--init', '--recursive', '--depth=1', From da180668ab832432a4cf9b269a651319e818e27a Mon Sep 17 00:00:00 2001 From: gnought <1684105+gnought@users.noreply.github.com> Date: Sat, 14 Feb 2026 21:32:58 +0800 Subject: [PATCH 4/5] Revert "add local tag in shallow clone" This reverts commit 5c9f50b681300c71363b581d6b895565b78eab90. --- pre_commit/store.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pre_commit/store.py b/pre_commit/store.py index df811479..dc90c051 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -187,7 +187,6 @@ class Store: git_config = 'protocol.version=2' git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1') git_cmd('checkout', 'FETCH_HEAD') - git_cmd('tag', '-a', '-m', '""', ref, 'FETCH_HEAD') git_cmd( '-c', git_config, 'submodule', 'update', '--init', '--recursive', '--depth=1', From 55f9ae755e3851d08875072fbf0930d8c2f6b40d Mon Sep 17 00:00:00 2001 From: gnought <1684105+gnought@users.noreply.github.com> Date: Sat, 14 Feb 2026 21:49:34 +0800 Subject: [PATCH 5/5] best-effort fetch tag for HEAD in shallow clone --- pre_commit/store.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pre_commit/store.py b/pre_commit/store.py index dc90c051..de51c473 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -186,6 +186,7 @@ class Store: git_config = 'protocol.version=2' git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1') + git_cmd('-c', git_config, 'fetch', 'origin', f'refs/tags/{ref}:refs/tags/{ref}', '--depth=1', check=False) git_cmd('checkout', 'FETCH_HEAD') git_cmd( '-c', git_config, 'submodule', 'update', '--init', '--recursive', @@ -199,8 +200,8 @@ class Store: git.init_repo(directory, repo) env = git.no_git_env() - def _git_cmd(*args: str) -> None: - cmd_output_b('git', *args, cwd=directory, env=env) + def _git_cmd(*args: str, check=True) -> None: + cmd_output_b('git', *args, cwd=directory, env=env, check=check) try: self._shallow_clone(ref, _git_cmd)