|
1 | 1 |
|
2 | 2 | using System;
|
3 | 3 | using System.Collections.Generic;
|
| 4 | +using System.Diagnostics; |
4 | 5 | using System.Linq;
|
5 | 6 | using System.Threading.Tasks;
|
6 | 7 |
|
@@ -57,48 +58,120 @@ private static IEnumerable<TaggedVersion> SelectWhereSemver(
|
57 | 58 | if (head is null)
|
58 | 59 | return (1, null);
|
59 | 60 |
|
60 |
| - var current = head.Value; |
61 |
| - int height = 0; |
62 |
| - while (true) |
| 61 | + return await FromCommit( |
| 62 | + commit: head.Value, |
| 63 | + commitDescriptor: "HEAD", |
| 64 | + options: new FromCommitOptions(repo, tags, tagPrefix, log, tagFilter)); |
| 65 | + } |
| 66 | + |
| 67 | + private class FromCommitOptions |
| 68 | + { |
| 69 | + public IRepoInspector Repo { get; } |
| 70 | + public TagContainer Tags { get; } |
| 71 | + public string TagPrefix { get; } |
| 72 | + public ILogger? Log { get; } |
| 73 | + public ITagFilter? TagFilter { get; } |
| 74 | + |
| 75 | + public FromCommitOptions( |
| 76 | + IRepoInspector repo, |
| 77 | + TagContainer tags, |
| 78 | + string tagPrefix, |
| 79 | + ILogger? log, |
| 80 | + ITagFilter? tagFilter) |
63 | 81 | {
|
64 |
| - var currentTags = tags.FindCommitTags(current); |
| 82 | + Repo = repo; |
| 83 | + Tags = tags; |
| 84 | + TagPrefix = tagPrefix; |
| 85 | + Log = log; |
| 86 | + TagFilter = tagFilter; |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + private static async Task<(int height, TaggedVersion? version)> FromCommit( |
| 92 | + Commit commit, |
| 93 | + string commitDescriptor, |
| 94 | + FromCommitOptions options) |
| 95 | + { |
| 96 | + var visited = new HashSet<Commit>(); |
| 97 | + var toVisit = new Stack<(Commit commit, int height, string descriptor, int heightSinceBranch)>(); |
| 98 | + toVisit.Push((commit, 0, commitDescriptor, 0)); |
| 99 | + |
| 100 | + var candidates = new List<(int height, TaggedVersion? version)>(); |
| 101 | + |
| 102 | + while (toVisit.Count > 0) |
| 103 | + { |
| 104 | + var (current, height, rootDescriptor, heightSinceBranch) = toVisit.Pop(); |
| 105 | + |
| 106 | + var descriptor = heightSinceBranch == 0 ? rootDescriptor : $"{rootDescriptor}~{heightSinceBranch}"; |
| 107 | + |
| 108 | + // already visited in an ultimately prior parent |
| 109 | + if (!visited.Add(current)) |
| 110 | + { |
| 111 | + options.Log?.Verbatim($"{descriptor} found in prior parent, discontinuing branch."); |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + var currentTags = options.Tags.FindCommitTags(current); |
65 | 116 | var versions = currentTags
|
66 |
| - .Where(t => t.Name.StartsWith(tagPrefix, StringComparison.Ordinal)) |
67 |
| - .SelectWhereSemver(tagPrefix, log) |
| 117 | + .Where(t => t.Name.StartsWith(options.TagPrefix, StringComparison.Ordinal)) |
| 118 | + .SelectWhereSemver(options.TagPrefix, options.Log) |
68 | 119 | .OrderByDescending(v => v.Version)
|
69 | 120 | .ToList();
|
70 | 121 |
|
71 |
| - log?.Verbatim($"HEAD^{height} {current} has {currentTags.Count} total tags with {versions.Count} versions."); |
| 122 | + options.Log?.Verbatim($"{descriptor} has {currentTags.Count} total tags with {versions.Count} versions."); |
72 | 123 |
|
73 | 124 | foreach (var tag in currentTags)
|
74 |
| - log?.Verbatim($" found tag: {tag.Name}"); |
| 125 | + options.Log?.Verbatim($" found tag: {tag.Name}"); |
75 | 126 |
|
76 | 127 | List<TaggedVersion>? filteredVersions = null;
|
77 | 128 | foreach (var version in versions)
|
78 | 129 | {
|
79 |
| - bool passesFilter = tagFilter is null || await tagFilter.PassesFilter(version); |
| 130 | + bool passesFilter = options.TagFilter is null || await options.TagFilter.PassesFilter(version); |
80 | 131 |
|
81 | 132 | if (passesFilter)
|
82 | 133 | {
|
83 |
| - log?.Verbatim($" version candidate: {version.Version}"); |
| 134 | + options.Log?.Verbatim($" version candidate: {version.Version}"); |
84 | 135 | filteredVersions ??= new();
|
85 | 136 | filteredVersions.Add(version);
|
86 | 137 | }
|
87 | 138 | else
|
88 |
| - log?.Verbatim($" version filtered: {version.Version} (from tag {version.Tag.Name})"); |
| 139 | + options.Log?.Verbatim($" version filtered: {version.Version} (from tag {version.Tag.Name})"); |
89 | 140 | }
|
90 | 141 |
|
91 | 142 | if (filteredVersions is not null)
|
92 |
| - return (height, filteredVersions.First()); |
| 143 | + { |
| 144 | + var candidateVersion = filteredVersions[0]; |
| 145 | + candidates.Add((height, candidateVersion)); |
| 146 | + options.Log?.Verbose($"Candidate version {candidateVersion.Version} found with {height} height at {descriptor}."); |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + var parents = await options.Repo.GetParents(current); |
93 | 151 |
|
94 |
| - height++; |
95 |
| - var parent = await repo.GetParent(current); |
96 |
| - if (parent is null) |
97 |
| - break; |
98 |
| - current = parent.Value; |
| 152 | + if (parents.Count == 0) |
| 153 | + { |
| 154 | + int phantomCommitHeight = height + 1; |
| 155 | + candidates.Add((phantomCommitHeight, null)); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + for (int i = parents.Count; i-- > 0;) |
| 160 | + { |
| 161 | + if (i == 0) |
| 162 | + toVisit.Push((parents[i], height + 1, rootDescriptor, heightSinceBranch + 1)); |
| 163 | + else |
| 164 | + toVisit.Push((parents[i], height + 1, $"{rootDescriptor}^{i}", 0)); |
| 165 | + } |
| 166 | + } |
99 | 167 | }
|
100 | 168 |
|
101 |
| - return (height, null); |
| 169 | + Debug.Assert(candidates.Count > 0); |
| 170 | + |
| 171 | + return candidates |
| 172 | + .OrderByDescending(x => x.version is not null) |
| 173 | + .ThenByDescending(x => x.version?.Version) |
| 174 | + .First(); |
102 | 175 | }
|
103 | 176 | }
|
104 | 177 | }
|
0 commit comments