|
2 | 2 | { |
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
| 5 | + using System.Globalization; |
5 | 6 | using System.IO; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Runtime.InteropServices; |
@@ -485,6 +486,71 @@ public static Repository OpenGitRepo(string pathUnderGitRepo, bool useDefaultCon |
485 | 486 | return gitDir == null ? null : new Repository(gitDir); |
486 | 487 | } |
487 | 488 |
|
| 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 | + |
488 | 554 | /// <summary> |
489 | 555 | /// Tests whether a commit is of a specified version, comparing major and minor components |
490 | 556 | /// with the version.txt file defined by that commit. |
@@ -512,23 +578,7 @@ private static bool CommitMatchesVersion(this Commit commit, SemanticVersion exp |
512 | 578 | return false; |
513 | 579 | } |
514 | 580 |
|
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); |
532 | 582 | } |
533 | 583 |
|
534 | 584 | /// <summary> |
|
0 commit comments