Ejemplo n.º 1
0
        /// <summary>
        /// Tests whether two <see cref="SemanticVersion" /> instances are compatible enough that version height is not reset
        /// when progressing from one to the next.
        /// </summary>
        /// <param name="first">The first semantic version.</param>
        /// <param name="second">The second semantic version.</param>
        /// <param name="versionHeightPosition">The position within the version where height is tracked.</param>
        /// <returns><c>true</c> if transitioning from one version to the next should reset the version height; <c>false</c> otherwise.</returns>
        internal static bool WillVersionChangeResetVersionHeight(SemanticVersion first, SemanticVersion second, SemanticVersion.Position versionHeightPosition)
        {
            Requires.NotNull(first, nameof(first));
            Requires.NotNull(second, nameof(second));

            if (first == second)
            {
                return(false);
            }

            if (versionHeightPosition == SemanticVersion.Position.Prerelease)
            {
                // The entire version spec must match exactly.
                return(!first.Equals(second));
            }

            for (SemanticVersion.Position position = SemanticVersion.Position.Major; position <= versionHeightPosition; position++)
            {
                int expectedValue = ReadVersionPosition(second.Version, position);
                int actualValue   = ReadVersionPosition(first.Version, position);
                if (expectedValue != actualValue)
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Tests whether a commit's version-spec matches a given version-spec.
        /// </summary>
        /// <param name="commit">The commit to test.</param>
        /// <param name="expectedVersion">The version to test for in the commit</param>
        /// <param name="comparisonPrecision">The last component of the version to include in the comparison.</param>
        /// <param name="tracker">The caching tracker for storing or fetching version information per commit.</param>
        /// <returns><c>true</c> if the <paramref name="commit"/> matches the major and minor components of <paramref name="expectedVersion"/>.</returns>
        private static bool CommitMatchesVersion(this Commit commit, Version expectedVersion, SemanticVersion.Position comparisonPrecision, GitWalkTracker tracker)
        {
            Requires.NotNull(commit, nameof(commit));
            Requires.NotNull(expectedVersion, nameof(expectedVersion));

            var commitVersionData = tracker.GetVersion(commit);
            var semVerFromFile    = commitVersionData?.Version;

            if (semVerFromFile == null)
            {
                return(false);
            }

            for (SemanticVersion.Position position = SemanticVersion.Position.Major; position <= comparisonPrecision; position++)
            {
                int expectedValue = ReadVersionPosition(expectedVersion, position);
                int actualValue   = ReadVersionPosition(semVerFromFile.Version, position);
                if (expectedValue != actualValue)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Tests whether a commit's version-spec matches a given version-spec.
        /// </summary>
        /// <param name="commit">The commit to test.</param>
        /// <param name="expectedVersion">The version to test for in the commit</param>
        /// <param name="comparisonPrecision">The last component of the version to include in the comparison.</param>
        /// <param name="repoRelativeProjectDirectory">The repo-relative directory from which <paramref name="expectedVersion"/> was originally calculated.</param>
        /// <returns><c>true</c> if the <paramref name="commit"/> matches the major and minor components of <paramref name="expectedVersion"/>.</returns>
        internal static bool CommitMatchesVersion(this Commit commit, Version expectedVersion, SemanticVersion.Position comparisonPrecision, string repoRelativeProjectDirectory)
        {
            Requires.NotNull(commit, nameof(commit));
            Requires.NotNull(expectedVersion, nameof(expectedVersion));

            var commitVersionData = VersionFile.GetVersion(commit, repoRelativeProjectDirectory);
            var semVerFromFile    = commitVersionData?.Version;

            if (semVerFromFile == null)
            {
                return(false);
            }

            for (SemanticVersion.Position position = SemanticVersion.Position.Major; position <= comparisonPrecision; position++)
            {
                int expectedValue = ReadVersionPosition(expectedVersion, position);
                int actualValue   = ReadVersionPosition(semVerFromFile.Version, position);
                if (expectedValue != actualValue)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        internal int ReadVersionPosition(SemanticVersion.Position position)
        {
            switch (position)
            {
            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.");
            }
        }
        private static int ReadVersionPosition(Version version, SemanticVersion.Position position)
        {
            Requires.NotNull(version, nameof(version));

            switch (position)
            {
            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.");
            }
        }
        /// <summary>
        /// Tests whether a commit is of a specified version, comparing major and minor components
        /// with the version.txt file defined by that commit.
        /// </summary>
        /// <param name="commit">The commit to test.</param>
        /// <param name="expectedVersion">The version to test for in the commit</param>
        /// <param name="comparisonPrecision">The last component of the version to include in the comparison.</param>
        /// <param name="tracker">The caching tracker for storing or fetching version information per commit.</param>
        /// <returns><c>true</c> if the <paramref name="commit"/> matches the major and minor components of <paramref name="expectedVersion"/>.</returns>
        private static bool CommitMatchesVersion(this Commit commit, SemanticVersion expectedVersion, SemanticVersion.Position comparisonPrecision, GitWalkTracker tracker)
        {
            Requires.NotNull(commit, nameof(commit));
            Requires.NotNull(expectedVersion, nameof(expectedVersion));

            var commitVersionData = tracker.GetVersion(commit);
            var semVerFromFile    = commitVersionData?.Version;

            if (semVerFromFile == null)
            {
                return(false);
            }

            // If the version height position moved, that's an automatic reset in version height.
            if (commitVersionData.VersionHeightPosition != comparisonPrecision)
            {
                return(false);
            }

            if (comparisonPrecision == SemanticVersion.Position.Prerelease)
            {
                // The entire version spec must match exactly.
                return(semVerFromFile?.Equals(expectedVersion) ?? false);
            }

            for (SemanticVersion.Position position = SemanticVersion.Position.Major; position <= comparisonPrecision; position++)
            {
                int expectedValue = ReadVersionPosition(expectedVersion.Version, position);
                int actualValue   = ReadVersionPosition(semVerFromFile.Version, position);
                if (expectedValue != actualValue)
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Tests whether a commit is of a specified version, comparing major and minor components
        /// with the version.txt file defined by that commit.
        /// </summary>
        /// <param name="commit">The commit to test.</param>
        /// <param name="expectedVersion">The version to test for in the commit</param>
        /// <param name="comparisonPrecision">The last component of the version to include in the comparison.</param>
        /// <param name="tracker">The caching tracker for storing or fetching version information per commit.</param>
        /// <returns><c>true</c> if the <paramref name="commit"/> matches the major and minor components of <paramref name="expectedVersion"/>.</returns>
        private static bool CommitMatchesVersion(this Commit commit, SemanticVersion expectedVersion, SemanticVersion.Position comparisonPrecision, GitWalkTracker tracker)
        {
            Requires.NotNull(commit, nameof(commit));
            Requires.NotNull(expectedVersion, nameof(expectedVersion));

            var commitVersionData = tracker.GetVersion(commit);
            var semVerFromFile    = commitVersionData?.Version;

            if (semVerFromFile == null)
            {
                return(false);
            }

            // If the version height position moved, that's an automatic reset in version height.
            if (commitVersionData.VersionHeightPosition != comparisonPrecision)
            {
                return(false);
            }

            return(!WillVersionChangeResetVersionHeight(commitVersionData.Version, expectedVersion, comparisonPrecision));
        }