Skip to content

Commit

Permalink
RepoMetadata: remove LocalCacheRoot and GitObjectsRoot
Browse files Browse the repository at this point in the history
Update Scalar to depend on the local git config for the git
objects root and the local cache root and remove them from
RepoMetadata.dat
  • Loading branch information
wilbaker committed Oct 1, 2019
1 parent c1e18fc commit dffd208
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 132 deletions.
53 changes: 0 additions & 53 deletions Scalar.Common/RepoMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,61 +130,10 @@ public void SaveCloneMetadata(ITracer tracer, ScalarEnlistment enlistment)
{
new KeyValuePair<string, string>(Keys.DiskLayoutMajorVersion, ScalarPlatform.Instance.DiskLayoutUpgrade.Version.CurrentMajorVersion.ToString()),
new KeyValuePair<string, string>(Keys.DiskLayoutMinorVersion, ScalarPlatform.Instance.DiskLayoutUpgrade.Version.CurrentMinorVersion.ToString()),
new KeyValuePair<string, string>(Keys.GitObjectsRoot, enlistment.GitObjectsRoot),
new KeyValuePair<string, string>(Keys.LocalCacheRoot, enlistment.LocalCacheRoot),
new KeyValuePair<string, string>(Keys.EnlistmentId, CreateNewEnlistmentId(tracer)),
});
}

public bool TryGetGitObjectsRoot(out string gitObjectsRoot, out string error)
{
gitObjectsRoot = null;

try
{
if (!this.repoMetadata.TryGetValue(Keys.GitObjectsRoot, out gitObjectsRoot))
{
error = "Git objects root not found";
return false;
}
}
catch (FileBasedCollectionException ex)
{
error = ex.Message;
return false;
}

error = null;
return true;
}

public void SetGitObjectsRoot(string gitObjectsRoot)
{
this.repoMetadata.SetValueAndFlush(Keys.GitObjectsRoot, gitObjectsRoot);
}

public bool TryGetLocalCacheRoot(out string localCacheRoot, out string error)
{
localCacheRoot = null;

try
{
if (!this.repoMetadata.TryGetValue(Keys.LocalCacheRoot, out localCacheRoot))
{
error = "Local cache root not found";
return false;
}
}
catch (FileBasedCollectionException ex)
{
error = ex.Message;
return false;
}

error = null;
return true;
}

public void SetEntry(string keyName, string valueName)
{
this.repoMetadata.SetValueAndFlush(keyName, valueName);
Expand All @@ -203,8 +152,6 @@ public static class Keys
{
public const string DiskLayoutMajorVersion = "DiskLayoutVersion";
public const string DiskLayoutMinorVersion = "DiskLayoutMinorVersion";
public const string GitObjectsRoot = "GitObjectsRoot";
public const string LocalCacheRoot = "LocalCacheRoot";
public const string EnlistmentId = "EnlistmentId";
}
}
Expand Down
57 changes: 6 additions & 51 deletions Scalar.FunctionalTests/Tests/EnlistmentPerFixture/MountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,60 +119,15 @@ public void MountFailsWhenNoOnDiskVersion()
}

[TestCase]
public void MountFailsWhenNoLocalCacheRootInRepoMetadata()
public void MountFailsWhenNoGitObjectsRootInGitConfig()
{
this.Enlistment.UnmountScalar();
string gitObjectsRoot = GitProcess.Invoke(this.Enlistment.RepoRoot, "config --local gvfs.sharedCache");
string.IsNullOrWhiteSpace(gitObjectsRoot).ShouldBeFalse("gitObjects root should be set");

string majorVersion;
string minorVersion;
ScalarHelpers.GetPersistedDiskLayoutVersion(this.Enlistment.DotScalarRoot, out majorVersion, out minorVersion);
majorVersion.ShouldNotBeNull();
minorVersion.ShouldNotBeNull();

string objectsRoot = ScalarHelpers.GetPersistedGitObjectsRoot(this.Enlistment.DotScalarRoot).ShouldNotBeNull();

string metadataPath = Path.Combine(this.Enlistment.DotScalarRoot, ScalarHelpers.RepoMetadataName);
string metadataBackupPath = metadataPath + ".backup";
this.fileSystem.MoveFile(metadataPath, metadataBackupPath);

this.fileSystem.CreateEmptyFile(metadataPath);
ScalarHelpers.SaveDiskLayoutVersion(this.Enlistment.DotScalarRoot, majorVersion, minorVersion);
ScalarHelpers.SaveGitObjectsRoot(this.Enlistment.DotScalarRoot, objectsRoot);

this.MountShouldFail("Failed to determine local cache path from repo metadata");

this.fileSystem.DeleteFile(metadataPath);
this.fileSystem.MoveFile(metadataBackupPath, metadataPath);

this.Enlistment.MountScalar();
}

[TestCase]
public void MountFailsWhenNoGitObjectsRootInRepoMetadata()
{
this.Enlistment.UnmountScalar();

string majorVersion;
string minorVersion;
ScalarHelpers.GetPersistedDiskLayoutVersion(this.Enlistment.DotScalarRoot, out majorVersion, out minorVersion);
majorVersion.ShouldNotBeNull();
minorVersion.ShouldNotBeNull();

string localCacheRoot = ScalarHelpers.GetPersistedLocalCacheRoot(this.Enlistment.DotScalarRoot).ShouldNotBeNull();

string metadataPath = Path.Combine(this.Enlistment.DotScalarRoot, ScalarHelpers.RepoMetadataName);
string metadataBackupPath = metadataPath + ".backup";
this.fileSystem.MoveFile(metadataPath, metadataBackupPath);

this.fileSystem.CreateEmptyFile(metadataPath);
ScalarHelpers.SaveDiskLayoutVersion(this.Enlistment.DotScalarRoot, majorVersion, minorVersion);
ScalarHelpers.SaveLocalCacheRoot(this.Enlistment.DotScalarRoot, localCacheRoot);

this.MountShouldFail("Failed to determine git objects root from repo metadata");

this.fileSystem.DeleteFile(metadataPath);
this.fileSystem.MoveFile(metadataBackupPath, metadataPath);

GitProcess.Invoke(this.Enlistment.RepoRoot, "config --local --unset-all gvfs.sharedCache");
this.MountShouldFail("Failed to determine git objects root from git config");
GitProcess.Invoke(this.Enlistment.RepoRoot, $"config--local gvfs.sharedCache {gitObjectsRoot}");
this.Enlistment.MountScalar();
}

Expand Down
13 changes: 5 additions & 8 deletions Scalar.Mount/InProcessMount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,14 @@ public void Mount(EventLevel verbosity, Keywords keywords)
}

string gitObjectsRoot;
if (!RepoMetadata.Instance.TryGetGitObjectsRoot(out gitObjectsRoot, out error))
GitProcess process = new GitProcess(this.enlistment);
GitProcess.ConfigResult result = process.GetFromLocalConfig(ScalarConstants.GitConfig.ObjectCache);
if (!result.TryParseAsString(out gitObjectsRoot, out error))
{
this.FailMountAndExit("Failed to determine git objects root from repo metadata: " + error);
}

string localCacheRoot;
if (!RepoMetadata.Instance.TryGetLocalCacheRoot(out localCacheRoot, out error))
{
this.FailMountAndExit("Failed to determine local cache path from repo metadata: " + error);
this.FailMountAndExit("Failed to determine git objects root from git config: " + error);
}

string localCacheRoot = Path.GetDirectoryName(gitObjectsRoot);
this.tracer.RelatedEvent(
EventLevel.Informational,
"CachePathsLoaded",
Expand Down
13 changes: 6 additions & 7 deletions Scalar/CommandLine/DiagnoseVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,14 @@ private void GetLocalCachePaths(ScalarEnlistment enlistment, out string localCac
using (ITracer tracer = new JsonTracer(ScalarConstants.ScalarEtwProviderName, "DiagnoseVerb"))
{
string error;
if (RepoMetadata.TryInitialize(tracer, Path.Combine(enlistment.EnlistmentRoot, ScalarPlatform.Instance.Constants.DotScalarRoot), out error))
GitProcess process = new GitProcess(enlistment);
GitProcess.ConfigResult result = process.GetFromLocalConfig(ScalarConstants.GitConfig.ObjectCache);
if (!result.TryParseAsString(out gitObjectsRoot, out error))
{
RepoMetadata.Instance.TryGetLocalCacheRoot(out localCacheRoot, out error);
RepoMetadata.Instance.TryGetGitObjectsRoot(out gitObjectsRoot, out error);
}
else
{
this.WriteMessage("Failed to determine local cache path and git objects root, RepoMetadata error: " + error);
this.WriteMessage("Failed to determine local cache path and git objects root: " + error);
}

localCacheRoot = Path.GetDirectoryName(gitObjectsRoot);
}
}
catch (Exception e)
Expand Down
21 changes: 8 additions & 13 deletions Scalar/CommandLine/ScalarVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,33 +806,31 @@ protected void InitializeLocalCacheAndObjectsPaths(
this.ReportErrorAndExit(tracer, "Failed to initialize repo metadata: " + error);
}

this.InitializeCachePathsFromRepoMetadata(tracer, enlistment);
this.InitializeCachePaths(tracer, enlistment);
this.EnsureLocalCacheIsHealthy(tracer, enlistment, retryConfig, serverScalarConfig, cacheServer);

RepoMetadata.Shutdown();
}

private void InitializeCachePathsFromRepoMetadata(
private void InitializeCachePaths(
ITracer tracer,
ScalarEnlistment enlistment)
{
string error;
string gitObjectsRoot;
if (!RepoMetadata.Instance.TryGetGitObjectsRoot(out gitObjectsRoot, out error))
GitProcess process = new GitProcess(enlistment);
GitProcess.ConfigResult result = process.GetFromLocalConfig(ScalarConstants.GitConfig.ObjectCache);
if (!result.TryParseAsString(out gitObjectsRoot, out error))
{
this.ReportErrorAndExit(tracer, "Failed to determine git objects root from repo metadata: " + error);
this.ReportErrorAndExit("Failed to determine git objects root from git config: " + error);
}

if (string.IsNullOrWhiteSpace(gitObjectsRoot))
{
this.ReportErrorAndExit(tracer, "Invalid git objects root (empty or whitespace)");
}

string localCacheRoot;
if (!RepoMetadata.Instance.TryGetLocalCacheRoot(out localCacheRoot, out error))
{
this.ReportErrorAndExit(tracer, "Failed to determine local cache path from repo metadata: " + error);
}
string localCacheRoot = Path.GetDirectoryName(gitObjectsRoot);

if (string.IsNullOrWhiteSpace(localCacheRoot))
{
Expand Down Expand Up @@ -984,14 +982,11 @@ private void EnsureLocalCacheIsHealthy(
this.ReportErrorAndExit(tracer, "Failed to create objects, pack, and sizes folders");
}

tracer.RelatedInfo($"{nameof(this.EnsureLocalCacheIsHealthy)}: Creating new alternates file");
tracer.RelatedInfo($"{nameof(this.EnsureLocalCacheIsHealthy)}: Creating new alternates file and setting object cache in git config");
if (!this.TrySetObjectCacheLocation(fileSystem, enlistment, out error))
{
this.ReportErrorAndExit(tracer, $"Failed to update alterates file with new objects path: {error}");
}

tracer.RelatedInfo($"{nameof(this.EnsureLocalCacheIsHealthy)}: Saving git objects root ({enlistment.GitObjectsRoot}) in repo metadata");
RepoMetadata.Instance.SetGitObjectsRoot(enlistment.GitObjectsRoot);
}
}

Expand Down

0 comments on commit dffd208

Please sign in to comment.