From aabb160c39dc42b434d573866f7f3d185f54c5ec Mon Sep 17 00:00:00 2001 From: jianghy Date: Sat, 5 Mar 2022 18:29:49 +0800 Subject: [PATCH 1/2] fix #2927: invalid use of Lazy, #2933 fix doesn't work actualy --- src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs | 6 ------ src/GitVersion.Core/Core/GitPreparer.cs | 7 +++++-- src/GitVersion.Core/Core/GitVersionCalculateTool.cs | 7 +------ 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs b/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs index 5600abb0ca..35540a74f7 100644 --- a/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs +++ b/src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs @@ -562,9 +562,6 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndNotTa environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true"); this.sp = GetServiceProvider(gitVersionOptions, environment: environment); - - var _ = this.sp.GetRequiredService>()?.Value; - var sut = sp.GetRequiredService(); // Execute & Verify @@ -594,9 +591,6 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagge environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true"); this.sp = GetServiceProvider(gitVersionOptions, environment: environment); - - var _ = this.sp.GetRequiredService>()?.Value; - var sut = sp.GetRequiredService(); // Execute diff --git a/src/GitVersion.Core/Core/GitPreparer.cs b/src/GitVersion.Core/Core/GitPreparer.cs index f01922af08..d940c0d8a0 100644 --- a/src/GitVersion.Core/Core/GitPreparer.cs +++ b/src/GitVersion.Core/Core/GitPreparer.cs @@ -18,11 +18,13 @@ public class GitPreparer : IGitPreparer private readonly IRepositoryStore repositoryStore; private readonly ICurrentBuildAgent buildAgent; private readonly RetryAction retryAction; + private readonly Lazy versionContext; + private GitVersionContext context => this.versionContext.Value; private const string DefaultRemoteName = "origin"; public GitPreparer(ILog log, IEnvironment environment, ICurrentBuildAgent buildAgent, IOptions options, - IMutatingGitRepository repository, IGitRepositoryInfo repositoryInfo, IRepositoryStore repositoryStore) + IMutatingGitRepository repository, IGitRepositoryInfo repositoryInfo, IRepositoryStore repositoryStore, Lazy versionContext) { this.log = log.NotNull(); this.environment = environment.NotNull(); @@ -32,6 +34,7 @@ public GitPreparer(ILog log, IEnvironment environment, ICurrentBuildAgent buildA this.repositoryStore = repositoryStore.NotNull(); this.buildAgent = buildAgent.NotNull(); this.retryAction = new RetryAction(); + this.versionContext = versionContext.NotNull(); } public void Prepare() @@ -241,7 +244,7 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool this.log.Warning($"Choosing {branchWithoutSeparator.Name.Canonical} as it is the only branch without / or - in it. " + moveBranchMsg); Checkout(branchWithoutSeparator.Name.Canonical); } - else + else if(!this.context.IsCurrentCommitTagged) { throw new WarningException("Failed to try and guess branch to use. " + moveBranchMsg); } diff --git a/src/GitVersion.Core/Core/GitVersionCalculateTool.cs b/src/GitVersion.Core/Core/GitVersionCalculateTool.cs index 5c975d09d7..6dcb80c083 100644 --- a/src/GitVersion.Core/Core/GitVersionCalculateTool.cs +++ b/src/GitVersion.Core/Core/GitVersionCalculateTool.cs @@ -40,12 +40,7 @@ public GitVersionCalculateTool(ILog log, INextVersionCalculator nextVersionCalcu public VersionVariables CalculateVersionVariables() { - bool isCurrentCommitTagged = this.versionContext.IsValueCreated && this.versionContext.Value.IsCurrentCommitTagged; - - if (!isCurrentCommitTagged) - { - this.gitPreparer.Prepare(); //we need to prepare the repository before using it for version calculation - } + this.gitPreparer.Prepare(); //we need to prepare the repository before using it for version calculation var gitVersionOptions = this.options.Value; From 93996d52d03d7e343bbb1fafdcd53aa1924c3c78 Mon Sep 17 00:00:00 2001 From: jianghongyuan Date: Tue, 28 Dec 2021 17:59:37 +0800 Subject: [PATCH 2/2] format code --- src/GitVersion.Core/Core/GitPreparer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersion.Core/Core/GitPreparer.cs b/src/GitVersion.Core/Core/GitPreparer.cs index d940c0d8a0..4f5bc4f86c 100644 --- a/src/GitVersion.Core/Core/GitPreparer.cs +++ b/src/GitVersion.Core/Core/GitPreparer.cs @@ -244,7 +244,7 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool this.log.Warning($"Choosing {branchWithoutSeparator.Name.Canonical} as it is the only branch without / or - in it. " + moveBranchMsg); Checkout(branchWithoutSeparator.Name.Canonical); } - else if(!this.context.IsCurrentCommitTagged) + else if (!this.context.IsCurrentCommitTagged) { throw new WarningException("Failed to try and guess branch to use. " + moveBranchMsg); }