diff --git a/Scalar.Common/RepoMetadata.cs b/Scalar.Common/RepoMetadata.cs index 87adbbe9218..a684e6d2aad 100644 --- a/Scalar.Common/RepoMetadata.cs +++ b/Scalar.Common/RepoMetadata.cs @@ -130,61 +130,10 @@ public void SaveCloneMetadata(ITracer tracer, ScalarEnlistment enlistment) { new KeyValuePair(Keys.DiskLayoutMajorVersion, ScalarPlatform.Instance.DiskLayoutUpgrade.Version.CurrentMajorVersion.ToString()), new KeyValuePair(Keys.DiskLayoutMinorVersion, ScalarPlatform.Instance.DiskLayoutUpgrade.Version.CurrentMinorVersion.ToString()), - new KeyValuePair(Keys.GitObjectsRoot, enlistment.GitObjectsRoot), - new KeyValuePair(Keys.LocalCacheRoot, enlistment.LocalCacheRoot), new KeyValuePair(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); @@ -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"; } } diff --git a/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/MountTests.cs b/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/MountTests.cs index 57d859d74a1..c5b0d88559a 100644 --- a/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/MountTests.cs +++ b/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/MountTests.cs @@ -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(); } diff --git a/Scalar.Mount/InProcessMount.cs b/Scalar.Mount/InProcessMount.cs index dceb802151d..59412f9aea0 100644 --- a/Scalar.Mount/InProcessMount.cs +++ b/Scalar.Mount/InProcessMount.cs @@ -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", diff --git a/Scalar/CommandLine/DiagnoseVerb.cs b/Scalar/CommandLine/DiagnoseVerb.cs index ded0b59e8ad..82fac7dd16a 100644 --- a/Scalar/CommandLine/DiagnoseVerb.cs +++ b/Scalar/CommandLine/DiagnoseVerb.cs @@ -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) diff --git a/Scalar/CommandLine/ScalarVerb.cs b/Scalar/CommandLine/ScalarVerb.cs index 406f7cfa32f..eb319cd9bbb 100644 --- a/Scalar/CommandLine/ScalarVerb.cs +++ b/Scalar/CommandLine/ScalarVerb.cs @@ -806,21 +806,23 @@ 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)) @@ -828,11 +830,7 @@ private void InitializeCachePathsFromRepoMetadata( 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)) { @@ -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); } }