Skip to content
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
26 changes: 26 additions & 0 deletions src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public void MajorMinorBuildPrereleaseBuildMetadata()
Assert.Equal(0, oracle.VersionHeightOffset);
}

[Theory]
[InlineData("0.1", "0.2")]
[InlineData("0.1.0+{height}", "0.1.5+{height}")]
[InlineData("0.1.5-alpha0.{height}", "0.1.5-alpha1.{height}")]
[InlineData("0.1.5-beta.{height}", "0.1.5-beta1.{height}")]
public void CompareFullVersionResetsHeight(string initial, string next)
{
var options = new VersionOptions
{
Version = SemanticVersion.Parse(initial),
CompareFullVersion = true
};
this.WriteVersionFile(options);
this.InitializeSourceControl();
this.AddCommits(10);

var oracle = VersionOracle.Create(this.RepoPath);
Assert.Equal(11, oracle.VersionHeight);

options.Version = SemanticVersion.Parse(next);

this.WriteVersionFile(options);
oracle = VersionOracle.Create(this.RepoPath);
Assert.Equal(1, oracle.VersionHeight);
}

[Fact]
public void HeightInPrerelease()
{
Expand Down
11 changes: 11 additions & 0 deletions src/NerdBank.GitVersioning/GitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,17 @@ internal static bool CommitMatchesMajorMinorVersion(this Commit commit, Version
return majorMinorFromFile?.Major == expectedVersion.Major && majorMinorFromFile?.Minor == expectedVersion.Minor;
}

internal static bool CommitMatchesFormat(this Commit commit, SemanticVersion expectedVersion, string repoRelativeProjectDirectory)
{
Requires.NotNull(commit, nameof(commit));
Requires.NotNull(expectedVersion, nameof(expectedVersion));

var commitVersionData = VersionFile.GetVersion(commit, repoRelativeProjectDirectory);
var majorMinorFromFile = commitVersionData?.Version;
return majorMinorFromFile.Equals(expectedVersion);
}


private static string FindGitDir(string startingDir)
{
while (startingDir != null)
Expand Down
7 changes: 6 additions & 1 deletion src/NerdBank.GitVersioning/VersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using Newtonsoft.Json;
Expand Down Expand Up @@ -64,6 +63,12 @@ public class VersionOptions : IEquatable<VersionOptions>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? BuildNumberOffset { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the build number is reset when the label changes.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool CompareFullVersion { get; set; } = false;

/// <summary>
/// Gets a number to add to the git height when calculating the <see cref="Version.Build"/> number.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/NerdBank.GitVersioning/VersionOracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@ private static int CalculateVersionHeight(string relativeRepoProjectDirectory, L
}
}

return headCommit?.GetHeight(c => c.CommitMatchesMajorMinorVersion(headCommitVersion, relativeRepoProjectDirectory)) ?? 0;
if (committedVersion != null && committedVersion.CompareFullVersion)
{
return headCommit?.GetHeight(c => c.CommitMatchesFormat(committedVersion?.Version, relativeRepoProjectDirectory)) ?? 0;
}
else
{
return headCommit?.GetHeight(c => c.CommitMatchesMajorMinorVersion(headCommitVersion, relativeRepoProjectDirectory)) ?? 0;
}
}

private static Version GetIdAsVersion(LibGit2Sharp.Commit headCommit, VersionOptions committedVersion, VersionOptions workingVersion, int versionHeight)
Expand Down
5 changes: 5 additions & 0 deletions src/NerdBank.GitVersioning/version.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"description": "A number to add to the git height when calculating the build number.",
"default": 0
},
"compareFullVersion": {
"type": "boolean",
"description": "Compare the whole version string when calculating the value of {height}.",
"default": false
},
"semVer1NumericIdentifierPadding": {
"type": "integer",
"description": "The minimum number of digits to use for numeric identifiers in SemVer 1.",
Expand Down