public static Version GetVersion(Repository repo, string tagPrefix, MajorMinor minMajorMinor, string buildMetadata, ILogger log) { var commit = repo.Commits.FirstOrDefault(); if (commit == default) { var version = new Version(minMajorMinor?.Major ?? 0, minMajorMinor?.Minor ?? 0, buildMetadata); log.Info($"No commits found. Using default version {version}."); return(version); } var tagsAndVersions = repo.Tags .Select(tag => (tag, Version.ParseOrDefault(tag.FriendlyName, tagPrefix))) .OrderByDescending(tagAndVersion => tagAndVersion.Item2) .ToList(); var commitsChecked = new HashSet <string>(); var count = 0; var height = 0; var candidates = new List <Candidate>(); var commitsToCheck = new Stack <(Commit, int, Commit)>(); Commit previousCommit = default; if (log.IsTraceEnabled) { log.Trace($"Starting at commit {commit.ShortSha()} (height {height})..."); } while (true) { var parentCount = 0; if (commitsChecked.Add(commit.Sha)) { ++count; var(tag, commitVersion) = tagsAndVersions.FirstOrDefault(tagAndVersion => tagAndVersion.tag.Target.Sha == commit.Sha); if (commitVersion != default) { var candidate = new Candidate { Commit = commit, Height = height, Tag = tag.FriendlyName, Version = commitVersion, }; if (log.IsTraceEnabled) { log.Trace($"Found version tag {candidate}."); } candidates.Add(candidate); } else { if (tag != default) { var candidate = new Candidate { Commit = commit, Height = height, Tag = tag.FriendlyName, Version = default,
public static Version GetVersion(string workDir, string tagPrefix, MajorMinor minMajorMinor, string buildMeta, VersionPart autoIncrement, string defaultPreReleasePhase, ILogger log) { log = log ?? throw new ArgumentNullException(nameof(log)); defaultPreReleasePhase = string.IsNullOrEmpty(defaultPreReleasePhase) ? "alpha" : defaultPreReleasePhase; var version = GetVersion(workDir, tagPrefix, autoIncrement, defaultPreReleasePhase, log).AddBuildMetadata(buildMeta); var calculatedVersion = version.Satisfying(minMajorMinor, defaultPreReleasePhase); _ = calculatedVersion != version ? log.IsInfoEnabled && log.Info($"Bumping version to {calculatedVersion} to satisfy minimum major minor {minMajorMinor}.") : log.IsDebugEnabled && log.Debug($"The calculated version {calculatedVersion} satisfies the minimum major minor {minMajorMinor}."); _ = log.IsInfoEnabled && log.Info($"Calculated version {calculatedVersion}."); return(calculatedVersion); }
public static bool TryParse(string value, [NotNullWhen(returnValue: true)] out MajorMinor?majorMinor) { majorMinor = null; if (string.IsNullOrWhiteSpace(value)) { return(false); } var numbers = value.Split('.'); var minor = 0; if (numbers.Length > 2 || !int.TryParse(numbers[0], out var major) || (numbers.Length > 1 && !int.TryParse(numbers[1], out minor))) { return(false); } majorMinor = new MajorMinor(major, minor); return(true); }