Skip to content

change prerelease tag to local.timestamp when building locally #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/GitVersionCore/GitVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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)
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we stick to either isLocalBuild or isCiBuild to avoid confusion?

Suggested change
private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache, bool isCiBuild)
private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache, bool isLocalBuild)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was indecisive on wording I think isLocalBuild is best. I must have missed this one.

{
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)
{
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GitVersion.Configuration;
using GitVersion.Logging;
using GitVersion.Extensions;
using LibGit2Sharp;

namespace GitVersion.VersionCalculation
{
Expand Down Expand Up @@ -77,6 +78,12 @@ public SemanticVersion FindVersion(GitVersionContext context)
}
}

if (context.IsLocalBuild && taggedSemanticVersion == null && context.Repository.RetrieveStatus().IsDirty)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why this is superior to our current solution we have with our own msbuild target.
We can check if the repo is dirty in GitVersion 😍

cc @zionyx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I wonder if that's what we want.
We properly want to have all local build marked with local.timestamp when I think about our use case for local nuget repository source.

{
var tagToUse = $"local.{DateTime.Now:yyyyMMddHHmmss}";
semver.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, null);
}

return taggedSemanticVersion ?? semver;
}

Expand Down
3 changes: 2 additions & 1 deletion src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.+");
Copy link
Contributor Author

@jetersen jetersen Jan 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great we already had a test that constituted as a dirty repo 😍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should properly add a dirty one and none dirty? to see when build is local 👍

}


Expand Down