Skip to content

Commit 2d57861

Browse files
author
pr-autocomplete[bot]
authored
Merge pull request #511 from dotnet/resetVersionOffset
Reset versionHeightOffset in `nbgv prepare-release`
2 parents 53b505d + 4d57018 commit 2d57861

File tree

3 files changed

+111
-17
lines changed

3 files changed

+111
-17
lines changed

src/NerdBank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,44 @@ public void PrepareRelease_JsonOutputWhenUpdatingReleaseBranch()
552552
}
553553
}
554554

555+
[Fact]
556+
public void PrepareRelease_ResetsVersionHeightOffset()
557+
{
558+
// create and configure repository
559+
this.InitializeSourceControl();
560+
561+
var initialVersionOptions = new VersionOptions()
562+
{
563+
Version = SemanticVersion.Parse("1.0-beta"),
564+
VersionHeightOffset = 5,
565+
};
566+
567+
var expectedReleaseVersionOptions = new VersionOptions()
568+
{
569+
Version = SemanticVersion.Parse("1.0"),
570+
VersionHeightOffset = 5,
571+
};
572+
573+
var expectedMainVersionOptions = new VersionOptions()
574+
{
575+
Version = SemanticVersion.Parse("1.1-alpha"),
576+
};
577+
578+
// create version.json
579+
this.WriteVersionFile(initialVersionOptions);
580+
581+
var tipBeforePrepareRelease = this.Repo.Head.Tip;
582+
583+
var releaseManager = new ReleaseManager();
584+
releaseManager.PrepareRelease(this.RepoPath);
585+
586+
var newVersion = VersionFile.GetVersion(this.RepoPath);
587+
Assert.Equal(expectedMainVersionOptions, newVersion);
588+
589+
var releaseVersion = VersionFile.GetVersion(this.Repo.Branches["v1.0"].Tip);
590+
Assert.Equal(expectedReleaseVersionOptions, releaseVersion);
591+
}
592+
555593
protected override void InitializeSourceControl()
556594
{
557595
base.InitializeSourceControl();

src/NerdBank.GitVersioning/GitExtensions.cs

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Globalization;
56
using System.IO;
67
using System.Linq;
78
using System.Runtime.InteropServices;
@@ -485,6 +486,71 @@ public static Repository OpenGitRepo(string pathUnderGitRepo, bool useDefaultCon
485486
return gitDir == null ? null : new Repository(gitDir);
486487
}
487488

489+
/// <summary>
490+
/// Tests whether two <see cref="VersionOptions" /> instances are compatible enough that version height is not reset
491+
/// when progressing from one to the next.
492+
/// </summary>
493+
/// <param name="first">One set of version options.</param>
494+
/// <param name="second">Another set of version options.</param>
495+
/// <returns><c>true</c> if transitioning from one version to the next should reset the version height; <c>false</c> otherwise.</returns>
496+
internal static bool WillVersionChangeResetVersionHeight(VersionOptions first, VersionOptions second)
497+
{
498+
Requires.NotNull(first, nameof(first));
499+
Requires.NotNull(second, nameof(second));
500+
501+
// If the version height position moved, that's an automatic reset in version height.
502+
if (first.VersionHeightPosition != second.VersionHeightPosition)
503+
{
504+
return true;
505+
}
506+
507+
if (!first.VersionHeightPosition.HasValue)
508+
{
509+
// There's no version height anywhere, so go ahead and say it would be reset.
510+
// This is useful to our `nbgv prepare-release` command to know that it can remove the version height adjustment property.
511+
return true;
512+
}
513+
514+
return WillVersionChangeResetVersionHeight(first.Version, second.Version, first.VersionHeightPosition.Value);
515+
}
516+
517+
/// <summary>
518+
/// Tests whether two <see cref="SemanticVersion" /> instances are compatible enough that version height is not reset
519+
/// when progressing from one to the next.
520+
/// </summary>
521+
/// <param name="first">The first semantic version.</param>
522+
/// <param name="second">The second semantic version.</param>
523+
/// <param name="versionHeightPosition">The position within the version where height is tracked.</param>
524+
/// <returns><c>true</c> if transitioning from one version to the next should reset the version height; <c>false</c> otherwise.</returns>
525+
internal static bool WillVersionChangeResetVersionHeight(SemanticVersion first, SemanticVersion second, SemanticVersion.Position versionHeightPosition)
526+
{
527+
Requires.NotNull(first, nameof(first));
528+
Requires.NotNull(second, nameof(second));
529+
530+
if (first == second)
531+
{
532+
return false;
533+
}
534+
535+
if (versionHeightPosition == SemanticVersion.Position.Prerelease)
536+
{
537+
// The entire version spec must match exactly.
538+
return !first.Equals(second);
539+
}
540+
541+
for (SemanticVersion.Position position = SemanticVersion.Position.Major; position <= versionHeightPosition; position++)
542+
{
543+
int expectedValue = ReadVersionPosition(second.Version, position);
544+
int actualValue = ReadVersionPosition(first.Version, position);
545+
if (expectedValue != actualValue)
546+
{
547+
return true;
548+
}
549+
}
550+
551+
return false;
552+
}
553+
488554
/// <summary>
489555
/// Tests whether a commit is of a specified version, comparing major and minor components
490556
/// with the version.txt file defined by that commit.
@@ -512,23 +578,7 @@ private static bool CommitMatchesVersion(this Commit commit, SemanticVersion exp
512578
return false;
513579
}
514580

515-
if (comparisonPrecision == SemanticVersion.Position.Prerelease)
516-
{
517-
// The entire version spec must match exactly.
518-
return semVerFromFile?.Equals(expectedVersion) ?? false;
519-
}
520-
521-
for (SemanticVersion.Position position = SemanticVersion.Position.Major; position <= comparisonPrecision; position++)
522-
{
523-
int expectedValue = ReadVersionPosition(expectedVersion.Version, position);
524-
int actualValue = ReadVersionPosition(semVerFromFile.Version, position);
525-
if (expectedValue != actualValue)
526-
{
527-
return false;
528-
}
529-
}
530-
531-
return true;
581+
return !WillVersionChangeResetVersionHeight(commitVersionData.Version, expectedVersion, comparisonPrecision);
532582
}
533583

534584
/// <summary>

src/NerdBank.GitVersioning/ReleaseManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ private void UpdateVersion(string projectDirectory, Repository repository, Seman
337337

338338
if (!EqualityComparer<SemanticVersion>.Default.Equals(versionOptions.Version, newVersion))
339339
{
340+
if (versionOptions.VersionHeightPosition.HasValue && GitExtensions.WillVersionChangeResetVersionHeight(versionOptions.Version, newVersion, versionOptions.VersionHeightPosition.Value))
341+
{
342+
// The version will be reset by this change, so remove the version height offset property.
343+
versionOptions.VersionHeightOffset = null;
344+
}
345+
340346
versionOptions.Version = newVersion;
341347
var filePath = VersionFile.SetVersion(projectDirectory, versionOptions, includeSchemaProperty: true);
342348

0 commit comments

Comments
 (0)