Skip to content
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

Fixes nbgv get-commits to require version matching #682

Merged
merged 1 commit into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/NerdBank.GitVersioning.Tests/GitExtensionsTests.cs
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
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
Expand Up @@ -152,6 +152,28 @@ internal enum Position
}
}

/// <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
Expand Up @@ -433,24 +433,7 @@ public bool Inherit
/// 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