From 0508819b5c03e9a695f1863a3e5c43ec7e0997d8 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Fri, 31 Jan 2020 00:03:01 +0100 Subject: [PATCH 1/3] change prerelease tag to local.timestamp when building locally --- src/GitVersionCore/GitVersionCalculator.cs | 11 ++++++----- src/GitVersionCore/GitVersionContext.cs | 8 +++++--- src/GitVersionCore/GitVersionFinder.cs | 2 +- .../VersionCalculation/NextVersionCalculator.cs | 6 ++++++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/GitVersionCore/GitVersionCalculator.cs b/src/GitVersionCore/GitVersionCalculator.cs index 86a46c59f4..c15352afc1 100644 --- a/src/GitVersionCore/GitVersionCalculator.cs +++ b/src/GitVersionCore/GitVersionCalculator.cs @@ -43,6 +43,7 @@ public VersionVariables CalculateVersionVariables() // Normalize if we are running on build server var normalizeGitDirectory = !arguments.NoNormalize && buildServer != null; var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes(); + var isLocalBuild = buildServer == null; var currentBranch = ResolveCurrentBranch(buildServer, arguments.TargetBranch, !string.IsNullOrWhiteSpace(arguments.DynamicRepositoryLocation)); @@ -59,7 +60,7 @@ public VersionVariables CalculateVersionVariables() throw new Exception($"Failed to prepare or find the .git directory in path '{arguments.TargetPath}'."); } - return GetCachedGitVersionInfo(arguments.TargetBranch, arguments.CommitId, arguments.OverrideConfig, arguments.NoCache); + return GetCachedGitVersionInfo(arguments.TargetBranch, arguments.CommitId, arguments.OverrideConfig, arguments.NoCache, isLocalBuild); } private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository) @@ -75,13 +76,13 @@ private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranc return currentBranch; } - private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache) + private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache, bool isCiBuild) { var cacheKey = cacheKeyFactory.Create(overrideConfig); var versionVariables = noCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(cacheKey); if (versionVariables == null) { - versionVariables = ExecuteInternal(targetBranch, commitId, overrideConfig); + versionVariables = ExecuteInternal(targetBranch, commitId, overrideConfig, isCiBuild); if (!noCache) { @@ -99,13 +100,13 @@ private VersionVariables GetCachedGitVersionInfo(string targetBranch, string com return versionVariables; } - private VersionVariables ExecuteInternal(string targetBranch, string commitId, Config overrideConfig) + private VersionVariables ExecuteInternal(string targetBranch, string commitId, Config overrideConfig, bool isLocalBuild) { var configuration = configProvider.Provide(overrideConfig: overrideConfig); return gitPreparer.GetDotGitDirectory().WithRepository(repo => { - var gitVersionContext = new GitVersionContext(repo, log, targetBranch, configuration, commitId: commitId); + var gitVersionContext = new GitVersionContext(repo, log, targetBranch, configuration, commitId: commitId, isLocalBuild: isLocalBuild); var semanticVersion = gitVersionFinder.FindVersion(gitVersionContext); return variableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged); diff --git a/src/GitVersionCore/GitVersionContext.cs b/src/GitVersionCore/GitVersionContext.cs index a97d4b93fc..fa98e3d1c6 100644 --- a/src/GitVersionCore/GitVersionContext.cs +++ b/src/GitVersionCore/GitVersionContext.cs @@ -14,18 +14,19 @@ public class GitVersionContext { private readonly ILog log; - public GitVersionContext(IRepository repository, ILog log, string targetBranch, Config configuration, bool onlyTrackedBranches = false, string commitId = null) - : this(repository, log, GetTargetBranch(repository, targetBranch), configuration, onlyTrackedBranches, commitId) + public GitVersionContext(IRepository repository, ILog log, string targetBranch, Config configuration, bool onlyTrackedBranches = false, string commitId = null, bool isLocalBuild = false) + : this(repository, log, GetTargetBranch(repository, targetBranch), configuration, onlyTrackedBranches, commitId, isLocalBuild) { } - public GitVersionContext(IRepository repository, ILog log, Branch currentBranch, Config configuration, bool onlyTrackedBranches = false, string commitId = null) + public GitVersionContext(IRepository repository, ILog log, Branch currentBranch, Config configuration, bool onlyTrackedBranches = false, string commitId = null, bool isLocalBuild = false) { this.log = log; Repository = repository; RepositoryMetadataProvider = new GitRepoMetadataProvider(repository, log, configuration); FullConfiguration = configuration; OnlyTrackedBranches = onlyTrackedBranches; + IsLocalBuild = isLocalBuild; if (currentBranch == null) throw new InvalidOperationException("Need a branch to operate on"); @@ -85,6 +86,7 @@ public GitVersionContext(IRepository repository, ILog log, Branch currentBranch, public Commit CurrentCommit { get; } public bool IsCurrentCommitTagged { get; } public GitRepoMetadataProvider RepositoryMetadataProvider { get; } + public bool IsLocalBuild { get; } private void CalculateEffectiveConfiguration() { diff --git a/src/GitVersionCore/GitVersionFinder.cs b/src/GitVersionCore/GitVersionFinder.cs index 44e44cd7de..6fedda956d 100644 --- a/src/GitVersionCore/GitVersionFinder.cs +++ b/src/GitVersionCore/GitVersionFinder.cs @@ -30,7 +30,7 @@ public SemanticVersion FindVersion(GitVersionContext context) var filePath = Path.Combine(context.Repository.GetRepositoryDirectory(), "NextVersion.txt"); if (File.Exists(filePath)) - { + { throw new WarningException("NextVersion.txt has been deprecated. See http://gitversion.readthedocs.org/en/latest/configuration/ for replacement"); } diff --git a/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs index a57f206e1e..c7aaad142a 100644 --- a/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs @@ -114,6 +114,12 @@ private void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion sema number = 1; } + if (context.IsLocalBuild) + { + tagToUse = $"local.{DateTime.Now:yyyyMMddHHmmss}"; + number = null; + } + semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number); } From 7f7043563c23e2057efd238e29eceedc8f46f8f4 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Fri, 31 Jan 2020 00:41:35 +0100 Subject: [PATCH 2/3] check taggedSemanticVersion and whether repository is dirty --- .../VersionCalculation/NextVersionCalculator.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs index c7aaad142a..91c890fc78 100644 --- a/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs @@ -7,6 +7,7 @@ using GitVersion.Configuration; using GitVersion.Logging; using GitVersion.Extensions; +using LibGit2Sharp; namespace GitVersion.VersionCalculation { @@ -77,6 +78,12 @@ public SemanticVersion FindVersion(GitVersionContext context) } } + if (context.IsLocalBuild && taggedSemanticVersion == null && context.Repository.RetrieveStatus().IsDirty) + { + var tagToUse = $"local.{DateTime.Now:yyyyMMddHHmmss}"; + semver.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, null); + } + return taggedSemanticVersion ?? semver; } @@ -114,12 +121,6 @@ private void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion sema number = 1; } - if (context.IsLocalBuild) - { - tagToUse = $"local.{DateTime.Now:yyyyMMddHHmmss}"; - number = null; - } - semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number); } From 95076a86a84f0d23504980a75f0a48998d315ef4 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Fri, 31 Jan 2020 07:25:05 +0100 Subject: [PATCH 3/3] test that local matches down to the hour --- src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs index 53d14754e1..444a1dad36 100644 --- a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs +++ b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs @@ -33,7 +33,8 @@ public void RunExecViaCommandLine() var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, ExecCommand.BuildTool, "RunExecViaCommandLine.csproj /target:OutputResults"); result.ExitCode.ShouldBe(0, result.Log); - result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1"); + var now = DateTime.Now.ToString("yyyyMMddHH"); + result.Log.ShouldMatch($".+GitVersion_FullSemVer: 1.2.4-local.{now}\\d+\\+1.+"); }