From 1a69ceaae98f8c5a909725c52341fdab3e06c253 Mon Sep 17 00:00:00 2001 From: coolkiid <73600915+coolkiid@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:30:16 +0800 Subject: [PATCH] fix: re-fetch the git dependency if its target_dir is occupied If a git dependency's target_dir is occupied by another git repository, the checkout will fail very likely. To fix the issue above, when a checkout failed, delete the occupied target_dir for a clean re-fetch. --- core/fetchers/git_fetcher.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/fetchers/git_fetcher.py b/core/fetchers/git_fetcher.py index 58f6d6c..f54015e 100644 --- a/core/fetchers/git_fetcher.py +++ b/core/fetchers/git_fetcher.py @@ -233,7 +233,16 @@ async def fetch(self, root_dir, options, *args, **kwargs): cmd = f'git --work-tree={target_dir} checkout FETCH_HEAD -- .' else: cmd = f'git checkout {checkout_args}' - await run_git_command(cmd, shell=True, cwd=source_dir, stderr=subprocess.STDOUT) + try: + await run_git_command(cmd, shell=True, cwd=source_dir, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError: + logging.warning( + f'A checkout for {target_dir} has failed. This might caused by that ' + f'the target directory for {url} is occupied by another git repository. A clean' + ' fetch is on the run.' + ) + rmtree(source_dir) + await self.fetch(root_dir, options, *args, **kwargs) if getattr(self.component, 'enable_lfs', False): try: