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

Allow Restore to discard pending changes #6330

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task<string> Restore(string pathToAssetsJson) {
InitializeAssetsRepo(config);
}

CheckoutRepoAtConfig(config);
CheckoutRepoAtConfig(config, true);
await BreadCrumb.Update(config);

return config.AssetsRepoLocation.ToString();
Expand Down Expand Up @@ -214,27 +214,30 @@ public async Task Reset(string pathToAssetsJson)

if (allowReset)
{
try
{
GitHandler.Run("checkout *", config);
GitHandler.Run("clean -xdf", config);
}
catch(GitProcessException e)
{
HideOrigin(config);
throw GenerateInvokeException(e.Result);
}

if (!string.IsNullOrWhiteSpace(config.Tag))
{
CheckoutRepoAtConfig(config);
CheckoutRepoAtConfig(config, true);
await BreadCrumb.Update(config);
}
}

HideOrigin(config);
}

private void Clean(GitAssetsConfiguration config)
{
try
{
GitHandler.Run("checkout *", config);
GitHandler.Run("clean -xdf", config);
}
catch (GitProcessException e)
{
HideOrigin(config);
throw GenerateInvokeException(e.Result);
}
}

/// <summary>
/// Given a CommandResult, generate an HttpException.
/// </summary>
Expand Down Expand Up @@ -297,12 +300,23 @@ private void HideOrigin(GitAssetsConfiguration config)
/// Given a configuration, set the sparse-checkout directory for the config, then attempt checkout of the targeted Tag.
/// </summary>
/// <param name="config"></param>
public void CheckoutRepoAtConfig(GitAssetsConfiguration config)
/// <param name="cleanEnabled">A newly initialized repo should not be 'cleaned', as that will result in a git error. However, a new
/// clone looks the same as being on the wrong tag. This variable allows us to prevent over-active cleaning that would result in exceptions.</param>
public void CheckoutRepoAtConfig(GitAssetsConfiguration config, bool cleanEnabled = true)
{
// we are already on a targeted tag and as such don't want to discard our recordings
if (Assets.TryGetValue(config.AssetsJsonRelativeLocation.ToString(), out var value) && value == config.Tag)
{
return;
}
// if we are NOT on our targeted tag, before we attempt to switch we need to reset without asking for permission
else
{
if (cleanEnabled)
{
Clean(config);
}
}

var checkoutPaths = ResolveCheckoutPaths(config);

Expand Down Expand Up @@ -466,7 +480,7 @@ public bool InitializeAssetsRepo(GitAssetsConfiguration config, bool forceInit =
throw GenerateInvokeException(e.Result);
}

CheckoutRepoAtConfig(config);
CheckoutRepoAtConfig(config, false);
workCompleted = true;
}

Expand Down