Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly reset PCS branches during codeflow #4348

Merged
merged 11 commits into from
Jan 22, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public async Task<ILocalGitRepo> PrepareCloneAsync(
}

var repo = await PrepareCloneInternalAsync(mapping.Name, remoteUris, [checkoutRef], checkoutRef, resetToRemote, cancellationToken);
await repo.CheckoutAsync(checkoutRef);
return repo;
}

Expand All @@ -116,7 +115,6 @@ public async Task<ILocalGitRepo> PrepareCloneAsync(
// We store clones in directories named as a hash of the repo URI
var cloneDir = StringUtils.GetXxHash64(repoUri);
var repo = await PrepareCloneInternalAsync(cloneDir, [repoUri], [checkoutRef], checkoutRef, resetToRemote, cancellationToken);
await repo.CheckoutAsync(checkoutRef);
return repo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public async Task RepositoryBranchShouldResetToRemote(bool resetToRemote)

// We advance the clone one commit ahead to see if it rewinds back with the reset
File.WriteAllText(filePath, "new content");
await oldClone.StageAsync(["."]);
await oldClone.CommitAsync("Change test file", false);
await GitOperations.CommitAll(oldClone.Path, "Change test file");

var localCommit = await GitOperations.GetRepoLastCommit(oldClone.Path);
var localCommit = await oldClone.GetShaForRefAsync(branchName);
var localFileContent = File.ReadAllText(filePath);
localCommit.Should().NotBe(remoteCommit);
localFileContent.Should().NotBe(remoteFileContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void SetUp()
{
ExitCode = 0,
}));
_localGitRepo.Setup(x => x.GitRefExists(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
_localGitRepoFactory
.Setup(x => x.Create(It.IsAny<NativePath>()))
.Returns((NativePath path) => new LocalGitRepo(path, _localGitRepo.Object, Mock.Of<IProcessManager>()));
Expand Down Expand Up @@ -157,7 +159,7 @@ void ResetCalls()
}

// Clone for the first time
var clone = await _manager.PrepareCloneAsync(mapping, new[] { mapping.DefaultRemote }, "main", default);
var clone = await _manager.PrepareCloneAsync(mapping, [mapping.DefaultRemote], "main", default);
clone.Path.Should().Be(clonePath);

_repoCloner.Verify(x => x.CloneNoCheckoutAsync(mapping.DefaultRemote, clonePath, null), Times.Once);
Expand All @@ -176,6 +178,10 @@ void ResetCalls()

// A third clone with a new remote
ResetCalls();
// We want to make it so we don't find all requested refs in the first remote
_localGitRepo.SetupSequence(x => x.GitRefExists(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(false)
.ReturnsAsync(true);
clone = await _manager.PrepareCloneAsync(mapping, new[] { mapping.DefaultRemote, newRemote }, Ref, default);

clone.Path.Should().Be(clonePath);
Expand All @@ -187,6 +193,10 @@ void ResetCalls()

// Same again, should be cached
ResetCalls();
// We want to make it so we don't find all requested refs in the first remote
_localGitRepo.SetupSequence(x => x.GitRefExists(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(false)
.ReturnsAsync(true);
clone = await _manager.PrepareCloneAsync(mapping, new[] { mapping.DefaultRemote, newRemote }, Ref + "3", default);

clone.Path.Should().Be(clonePath);
Expand All @@ -198,6 +208,8 @@ void ResetCalls()

// Call with URI directly
ResetCalls();
_localGitRepo.SetupSequence(x => x.GitRefExists(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
clone = await _manager.PrepareCloneAsync(RepoUri, Ref + "4", default);

clone.Path.Should().Be(clonePath);
Expand All @@ -209,6 +221,8 @@ void ResetCalls()

// Call with the second URI directly
ResetCalls();
_localGitRepo.SetupSequence(x => x.GitRefExists(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
clone = await _manager.PrepareCloneAsync(newRemote, Ref + "5", default);

clone.Path.Should().Be(clonePath);
Expand Down