Skip to content

Commit

Permalink
Fixes nbgv get-commits to require version matching
Browse files Browse the repository at this point in the history
We previously were only looking at version height and (optional) commit ID. We were (amazingly enough) not bothering to look at the major.minor version specified in the version.json file.
  • Loading branch information
AArnott committed Nov 1, 2021
1 parent c60a072 commit 7ac61d8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/NerdBank.GitVersioning.Tests/GitExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ public void GetCommitsFromVersion_WithPathFilters()
LibGit2GitExtensions.GetCommitsFromVersion(this.Context, new Version(1, 2, 3)).OrderBy(c => c.Sha));
}

[Fact]
public void GetCommitsFromVersion_WithMajorMinorChecks()
{
Commit v1_0_50 = this.WriteVersionFile(new VersionOptions { Version = SemanticVersion.Parse("1.0.50-preview.{height}") });
Commit v1_1_50 = this.WriteVersionFile(new VersionOptions { Version = SemanticVersion.Parse("1.1.50-preview.{height}") });

Assert.Empty(LibGit2GitExtensions.GetCommitsFromVersion(this.Context, new Version(1, 0)));
Assert.Empty(LibGit2GitExtensions.GetCommitsFromVersion(this.Context, new Version(1, 0, 49)));
Assert.Equal(v1_0_50, Assert.Single(LibGit2GitExtensions.GetCommitsFromVersion(this.Context, new Version(1, 0, 50))));
Assert.Equal(v1_1_50, Assert.Single(LibGit2GitExtensions.GetCommitsFromVersion(this.Context, new Version(1, 1, 50))));
}

[Theory]
[InlineData("2.2", "2.2-alpha.{height}", 1, 1, true)]
[InlineData("2.2", "2.3", 1, 1, true)]
Expand Down
2 changes: 1 addition & 1 deletion src/NerdBank.GitVersioning/LibGit2/LibGit2GitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static IEnumerable<Commit> GetCommitsFromVersion(LibGit2Context context,
var tracker = new GitWalkTracker(context);
var possibleCommits = from commit in GetCommitsReachableFromRefs(context.Repository)
let commitVersionOptions = tracker.GetVersion(commit)
where commitVersionOptions is not null
where commitVersionOptions?.Version?.IsMatchingVersion(version) is true
where !IsCommitIdMismatch(version, commitVersionOptions, commit)
where !IsVersionHeightMismatch(version, commitVersionOptions, commit, tracker)
select commit;
Expand Down
76 changes: 51 additions & 25 deletions src/NerdBank.GitVersioning/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ internal SemanticVersion.Position? VersionHeightPosition
}
}

/// <summary>
/// Gets the position in a computed version that the first 16 bits of a git commit ID should appear, if any.
/// </summary>
internal SemanticVersion.Position? GitCommitIdPosition
{
get
{
// We can only store the git commit ID info after there was a place to put the version height.
// We don't want to store the commit ID (which is effectively a random integer) in the revision slot
// if the version height does not appear, or only appears later (in the -prerelease tag) since that
// would mess up version ordering.
if (this.VersionHeightPosition == SemanticVersion.Position.Build)
{
return SemanticVersion.Position.Revision;
}
else
{
return null;
}
}
}

/// <summary>
/// Gets a value indicating whether this instance is the default "0.0" instance.
/// </summary>
Expand Down Expand Up @@ -286,40 +308,44 @@ internal static bool WillVersionChangeResetVersionHeight(SemanticVersion first,
return false;
}

internal static int ReadVersionPosition(Version version, SemanticVersion.Position position)
internal static int ReadVersionPosition(Version version, Position position)
{
Requires.NotNull(version, nameof(version));

switch (position)
return position switch
{
case SemanticVersion.Position.Major:
return version.Major;
case SemanticVersion.Position.Minor:
return version.Minor;
case SemanticVersion.Position.Build:
return version.Build;
case SemanticVersion.Position.Revision:
return version.Revision;
default:
throw new ArgumentOutOfRangeException(nameof(position), position, "Must be one of the 4 integer parts.");
}
Position.Major => version.Major,
Position.Minor => version.Minor,
Position.Build => version.Build,
Position.Revision => version.Revision,
_ => throw new ArgumentOutOfRangeException(nameof(position), position, "Must be one of the 4 integer parts."),
};
}

internal int ReadVersionPosition(SemanticVersion.Position position)
internal int ReadVersionPosition(Position position) => ReadVersionPosition(this.Version, position);

/// <summary>
/// Checks whether a given version may have been produced by this semantic version.
/// </summary>
/// <param name="version">The version to test.</param>
/// <returns><see langword="true"/> if the <paramref name="version"/> is a match; <see langword="false"/> otherwise.</returns>
internal bool IsMatchingVersion(Version version)
{
switch (position)
Position lastPositionToConsider = Position.Revision;
if (this.VersionHeightPosition < lastPositionToConsider)
{
case SemanticVersion.Position.Major:
return this.Version.Major;
case SemanticVersion.Position.Minor:
return this.Version.Minor;
case SemanticVersion.Position.Build:
return this.Version.Build;
case SemanticVersion.Position.Revision:
return this.Version.Revision;
default:
throw new ArgumentOutOfRangeException(nameof(position), position, "Must be one of the 4 integer parts.");
lastPositionToConsider = this.VersionHeightPosition.Value - 1;
}

for (Position i = Position.Major; i <= lastPositionToConsider; i++)
{
if (this.ReadVersionPosition(i) != ReadVersionPosition(version, i))
{
return false;
}
}

return true;
}

/// <summary>
Expand Down
19 changes: 1 addition & 18 deletions src/NerdBank.GitVersioning/VersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,24 +433,7 @@ internal SemanticVersion.Position? VersionHeightPosition
/// Gets the position in a computed version that the first 16 bits of a git commit ID should appear, if any.
/// </summary>
[JsonIgnore]
internal SemanticVersion.Position? GitCommitIdPosition
{
get
{
// We can only store the git commit ID info after there was a place to put the version height.
// We don't want to store the commit ID (which is effectively a random integer) in the revision slot
// if the version height does not appear, or only appears later (in the -prerelease tag) since that
// would mess up version ordering.
if (this.VersionHeightPosition == SemanticVersion.Position.Build)
{
return SemanticVersion.Position.Revision;
}
else
{
return null;
}
}
}
internal SemanticVersion.Position? GitCommitIdPosition => this.version?.GitCommitIdPosition;

/// <summary>
/// Gets the debugger display for this instance.
Expand Down

0 comments on commit 7ac61d8

Please sign in to comment.