Skip to content

Commit

Permalink
CommandLine: Set local object cache location in git config
Browse files Browse the repository at this point in the history
Copied changes from derrickstolee:gvfs-helper
that updated Scalar verbs to set the object cache in
git's config file.
  • Loading branch information
wilbaker committed Oct 1, 2019
1 parent ae71470 commit c1e18fc
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 28 deletions.
12 changes: 0 additions & 12 deletions Scalar.Common/Http/CacheServerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static string GetUrlFromConfig(Enlistment enlistment)
// TODO 1057500: Remove support for encoded-repo-url cache config setting
return
GetValueFromConfig(git, ScalarConstants.GitConfig.CacheServer, localOnly: true)
?? GetValueFromConfig(git, GetDeprecatedCacheConfigSettingName(enlistment), localOnly: false)
?? enlistment.RepoUrl;
}

Expand Down Expand Up @@ -144,17 +143,6 @@ private static string GetValueFromConfig(GitProcess git, string configName, bool
return value;
}

private static string GetDeprecatedCacheConfigSettingName(Enlistment enlistment)
{
string sectionUrl =
enlistment.RepoUrl.ToLowerInvariant()
.Replace("https://", string.Empty)
.Replace("http://", string.Empty)
.Replace('/', '.');

return ScalarConstants.GitConfig.ScalarPrefix + sectionUrl + ScalarConstants.GitConfig.DeprecatedCacheEndpointSuffix;
}

private CacheServerInfo CreateNone()
{
return new CacheServerInfo(this.enlistment.RepoUrl, CacheServerInfo.ReservedNames.None);
Expand Down
2 changes: 1 addition & 1 deletion Scalar.Common/ScalarConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class GitConfig
public const string MountId = ScalarPrefix + "mount-id";
public const string EnlistmentId = ScalarPrefix + "enlistment-id";
public const string CacheServer = ScalarPrefix + "cache-server";
public const string DeprecatedCacheEndpointSuffix = ".cache-server-url";
public const string ObjectCache = "gvfs.sharedCache";
public const string ScalarTelemetryId = GitConfig.ScalarPrefix + "telemetry-id";
public const string ScalarTelemetryPipe = GitConfig.ScalarPrefix + "telemetry-pipe";
public const string IKey = GitConfig.ScalarPrefix + "ikey";
Expand Down
10 changes: 0 additions & 10 deletions Scalar.UnitTests/Common/CacheServerResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ public void CanGetCacheServerFromNewConfig()
CacheServerResolver.GetUrlFromConfig(enlistment).ShouldEqual(CacheServerUrl);
}

[TestCase]
public void CanGetCacheServerFromOldConfig()
{
MockScalarEnlistment enlistment = this.CreateEnlistment(null, CacheServerUrl);
CacheServerInfo cacheServer = CacheServerResolver.GetCacheServerFromConfig(enlistment);

cacheServer.Url.ShouldEqual(CacheServerUrl);
CacheServerResolver.GetUrlFromConfig(enlistment).ShouldEqual(CacheServerUrl);
}

[TestCase]
public void CanGetCacheServerWithNoConfig()
{
Expand Down
2 changes: 1 addition & 1 deletion Scalar/CommandLine/CloneVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ private Result CreateClone()
}

string errorMessage;
if (!this.TryCreateAlternatesFile(this.fileSystem, this.enlistment, out errorMessage))
if (!this.TrySetObjectCacheLocation(this.fileSystem, this.enlistment, out errorMessage))
{
return new Result("Error configuring alternate: " + errorMessage);
}
Expand Down
12 changes: 8 additions & 4 deletions Scalar/CommandLine/ScalarVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,18 @@ protected void ValidateClientVersions(ITracer tracer, ScalarEnlistment enlistmen
}
}

protected bool TryCreateAlternatesFile(PhysicalFileSystem fileSystem, ScalarEnlistment enlistment, out string errorMessage)
protected bool TrySetObjectCacheLocation(PhysicalFileSystem fileSystem, ScalarEnlistment enlistment, out string errorMessage)
{
try
{
string alternatesFilePath = this.GetAlternatesPath(enlistment);
string tempFilePath = alternatesFilePath + ".tmp";
fileSystem.WriteAllText(tempFilePath, enlistment.GitObjectsRoot);
string gitObjectsPath = enlistment.GitObjectsRoot.Replace(Path.PathSeparator, ScalarConstants.GitPathSeparator);
fileSystem.WriteAllText(tempFilePath, gitObjectsPath);
fileSystem.MoveAndOverwriteFile(tempFilePath, alternatesFilePath);

GitProcess process = new GitProcess(enlistment);
process.SetInLocalConfig(ScalarConstants.GitConfig.ObjectCache, gitObjectsPath);
}
catch (SecurityException e)
{
Expand Down Expand Up @@ -914,7 +918,7 @@ private void EnsureLocalCacheIsHealthy(
{
tracer.RelatedInfo($"{nameof(this.EnsureLocalCacheIsHealthy)}: GitObjectsRoot ({enlistment.GitObjectsRoot}) missing from alternates files, recreating alternates");
string error;
if (!this.TryCreateAlternatesFile(fileSystem, enlistment, out error))
if (!this.TrySetObjectCacheLocation(fileSystem, enlistment, out error))
{
this.ReportErrorAndExit(tracer, $"Failed to update alternates file to include git objects root: {error}");
}
Expand Down Expand Up @@ -981,7 +985,7 @@ private void EnsureLocalCacheIsHealthy(
}

tracer.RelatedInfo($"{nameof(this.EnsureLocalCacheIsHealthy)}: Creating new alternates file");
if (!this.TryCreateAlternatesFile(fileSystem, enlistment, out error))
if (!this.TrySetObjectCacheLocation(fileSystem, enlistment, out error))
{
this.ReportErrorAndExit(tracer, $"Failed to update alterates file with new objects path: {error}");
}
Expand Down

0 comments on commit c1e18fc

Please sign in to comment.