diff --git a/tools/Google.Cloud.Tools.ReleaseManager/CommandBase.cs b/tools/Google.Cloud.Tools.ReleaseManager/CommandBase.cs index a74bd5931003..b5237e4122e3 100644 --- a/tools/Google.Cloud.Tools.ReleaseManager/CommandBase.cs +++ b/tools/Google.Cloud.Tools.ReleaseManager/CommandBase.cs @@ -14,8 +14,10 @@ using Google.Cloud.Tools.Common; using LibGit2Sharp; +using System; using System.Collections.Generic; using System.Linq; +using System.Threading; namespace Google.Cloud.Tools.ReleaseManager { @@ -28,6 +30,10 @@ public abstract class CommandBase : ICommand // releases, but fundamentally it's "the current source of truth we're basing this release on". internal const string PrimaryBranch = "main"; + // This is lazy so that we can construct instead of CommandBase even when the current directory isn't + // in a repo, but we don't go hunting for the root multiple times. + private readonly Lazy _lazyLayout = new(RootLayout.ForCurrentDirectory, LazyThreadSafetyMode.ExecutionAndPublication); + private readonly int _minArgs; private readonly int _maxArgs; @@ -43,7 +49,7 @@ public abstract class CommandBase : ICommand /// /// Currently this is always derived from the current directory, but we may later have a way of specifying it separately. /// - protected RootLayout RootLayout { get; } + protected RootLayout RootLayout => _lazyLayout.Value; protected CommandBase(string command, string description, int minArgs, int maxArgs, string expectedArguments) { @@ -52,7 +58,6 @@ protected CommandBase(string command, string description, int minArgs, int maxAr _minArgs = minArgs; _maxArgs = maxArgs; ExpectedArguments = expectedArguments; - RootLayout = RootLayout.ForCurrentDirectory(); } protected CommandBase(string command, string description, params string[] argNames)