Skip to content

Commit

Permalink
Clean up clones before usage
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Dec 3, 2024
1 parent 3e47dc3 commit 333ce10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/CloneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ protected async Task<ILocalGitRepo> PrepareCloneInternalAsync(
string.Join(", ", remoteUris));

NativePath path = null!;
bool cleanup = true;
foreach (string remoteUri in remoteUris)
{
// Path should be returned the same for all invocations
// We checkout a default ref
path = await PrepareCloneInternal(remoteUri, dirName, cancellationToken);
path = await PrepareCloneInternal(remoteUri, dirName, cleanup, cancellationToken);
cleanup = false;

// Verify that all requested commits are available
foreach (string gitRef in refsToVerify.ToArray())
Expand Down Expand Up @@ -110,7 +112,7 @@ protected async Task<ILocalGitRepo> PrepareCloneInternalAsync(
/// When clone is already present, it is re-used and we only fetch.
/// When given remotes have already been fetched during this run, they are not fetched again.
/// </summary>
protected async Task<NativePath> PrepareCloneInternal(string remoteUri, string dirName, CancellationToken cancellationToken)
protected async Task<NativePath> PrepareCloneInternal(string remoteUri, string dirName, bool performCleanup, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -139,7 +141,19 @@ protected async Task<NativePath> PrepareCloneInternal(string remoteUri, string d
}
else
{
_logger.LogDebug("Clone of {repo} found in {clonePath}", remoteUri, clonePath);
_logger.LogDebug("Clone of {repo} found in {clonePath}. Preparing for use...", remoteUri, clonePath);

// We make sure the clone is clean and we re-clone if it's unusable
if (performCleanup)
{
var result = await _localGitRepo.RunGitCommandAsync(clonePath, ["reset", "--hard"], cancellationToken);
if (!result.Succeeded)
{
_logger.LogWarning("Failed to clean up {clonePath}, re-cloning", clonePath);
_fileSystem.DeleteDirectory(clonePath, recursive: true);
return await PrepareCloneInternal(remoteUri, dirName, performCleanup: true, cancellationToken);
}
}

string remote;

Expand All @@ -151,7 +165,7 @@ protected async Task<NativePath> PrepareCloneInternal(string remoteUri, string d
{
_logger.LogWarning("Clone at {clonePath} is not a git repository, re-cloning", clonePath);
_fileSystem.DeleteDirectory(clonePath, recursive: true);
return await PrepareCloneInternal(remoteUri, dirName, cancellationToken);
return await PrepareCloneInternal(remoteUri, dirName, performCleanup: true, cancellationToken);
}

// We cannot do `fetch --all` as tokens might be needed but fetch +refs/heads/*:+refs/remotes/origin/* doesn't fetch new refs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ public async Task<ILocalGitRepo> PrepareCloneAsync(
}

NativePath path = null!;
bool cleanup = true;
foreach (string remoteUri in remoteUris)
{
// Path should be returned the same for all invocations
// We checkout a default ref
path = await PrepareCloneInternal(remoteUri, mapping.Name, cancellationToken);
path = await PrepareCloneInternal(remoteUri, mapping.Name, cleanup, cancellationToken);
cleanup = false;
}

var repo = _localGitRepoFactory.Create(path);
Expand All @@ -115,7 +117,7 @@ public async Task<ILocalGitRepo> PrepareCloneAsync(
{
// We store clones in directories named as a hash of the repo URI
var cloneDir = StringUtils.GetXxHash64(repoUri);
var path = await PrepareCloneInternal(repoUri, cloneDir, cancellationToken);
var path = await PrepareCloneInternal(repoUri, cloneDir, performCleanup: true, cancellationToken);
var repo = _localGitRepoFactory.Create(path);
await repo.CheckoutAsync(checkoutRef);
return repo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public async Task<ILocalGitRepo> PrepareVmrAsync(
string checkoutRef,
CancellationToken cancellationToken)
{
// TODO https://github.com/dotnet/arcade-services/issues/4197: Make sure VMR is ready for use (working tree is clean..)

// This makes sure we keep different VMRs separate
// We expect to have up to 3:
// 1. The GitHub VMR (dotnet/dotnet)
Expand Down

0 comments on commit 333ce10

Please sign in to comment.