From 4dbb3d1d4250c21e12354a0632c046f1c21fc929 Mon Sep 17 00:00:00 2001 From: Premek Vysoky Date: Tue, 22 Oct 2024 14:46:34 +0200 Subject: [PATCH] Re-clone a repo if dir is not a git repo Fixes https://github.com/dotnet/arcade-services/issues/4006 --- .../DarcLib/VirtualMonoRepo/CloneManager.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/CloneManager.cs b/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/CloneManager.cs index d7c937c983..5e38b7ada0 100644 --- a/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/CloneManager.cs +++ b/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/CloneManager.cs @@ -151,7 +151,19 @@ protected async Task PrepareCloneInternal(string remoteUri, string d else { _logger.LogDebug("Clone of {repo} found in {clonePath}", remoteUri, clonePath); - var remote = await _localGitRepo.AddRemoteIfMissingAsync(clonePath, remoteUri, cancellationToken); + + string remote; + + try + { + remote = await _localGitRepo.AddRemoteIfMissingAsync(clonePath, remoteUri, cancellationToken); + } + catch (Exception e) when (e.Message.Contains("fatal: not a git repository")) + { + _logger.LogWarning("Clone at {clonePath} is not a git repository, re-cloning", clonePath); + _fileSystem.DeleteDirectory(clonePath, recursive: true); + return await PrepareCloneInternal(remoteUri, dirName, cancellationToken); + } // We cannot do `fetch --all` as tokens might be needed but fetch +refs/heads/*:+refs/remotes/origin/* doesn't fetch new refs // So we need to call `remote update origin` to fetch everything