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/VersionCalculation/NextVersionCalculator.cs b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs index a57f206e1e..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; } 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.+"); }