Skip to content

Commit 7edcfbe

Browse files
committed
Refactor: Add IRepoInspector.GetParents(), deprecating GetParent().
This is required for #40, as fixing that issue will require walking all parents, not just the first parent. The `GitRepoInspector`'s implementation of `GetParents()` matches the current behavior of only using the first parent. This will be fixed in 2.0.0.
1 parent befacd4 commit 7edcfbe

8 files changed

+72
-43
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<MinVerTagPrefix>v</MinVerTagPrefix>
4747
<!-- delete/set to patch the line below once almost out of v0.x.y (preferably once on a beta or rc). -->
4848
<MinVerAutoIncrement>patch</MinVerAutoIncrement>
49-
<MinVerMinimumMajorMinor>1.1</MinVerMinimumMajorMinor>
49+
<MinVerMinimumMajorMinor>1.2</MinVerMinimumMajorMinor>
5050
</PropertyGroup>
5151
<ItemGroup>
5252
<PackageReference Include="MinVer" Version="2.5.0">

src/Verlite.Core/GitRepoInspector.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
67

@@ -226,9 +227,18 @@ private async Task CacheParents()
226227

227228
/// <inheritdoc/>
228229
public async Task<Commit?> GetParent(Commit commit)
230+
{
231+
var parents = await GetParents(commit);
232+
return parents
233+
.Select(p => (Commit?)p)
234+
.FirstOrDefault();
235+
}
236+
237+
/// <inheritdoc/>
238+
public async Task<IReadOnlyList<Commit>> GetParents(Commit commit)
229239
{
230240
if (CachedParents.TryGetValue(commit, out Commit ret))
231-
return ret;
241+
return new[] { ret };
232242

233243
var contents = await GetCommitObject(commit);
234244
var parent = ParseCommitObjectParent(contents);
@@ -237,7 +247,7 @@ private async Task CacheParents()
237247
CachedParents[commit] = parent.Value;
238248

239249
Log?.Verbatim($"GetParent() -> {parent}");
240-
return parent;
250+
return parent is not null ? new Commit[] { parent.Value } : Array.Empty<Commit>();
241251
}
242252

243253
private static readonly Regex RefsTagRegex = new Regex(

src/Verlite.Core/IRepoInspector.cs

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics.CodeAnalysis;
34
using System.Runtime.Serialization;
45
using System.Threading.Tasks;
@@ -74,8 +75,15 @@ public interface IRepoInspector
7475
/// </summary>
7576
/// <param name="commit">The commit.</param>
7677
/// <returns>A task containing the primary parent commit, or <c>null</c> if it has none.</returns>
78+
[Obsolete("Deprecated in favor of GetParents(). See GitHub issue #40.", error: false)]
7779
Task<Commit?> GetParent(Commit commit);
7880
/// <summary>
81+
/// Gets all parents of the specified commit.
82+
/// </summary>
83+
/// <param name="commit">The commit.</param>
84+
/// <returns>A task containing a list of parent commits.</returns>
85+
Task<IReadOnlyList<Commit>> GetParents(Commit commit);
86+
/// <summary>
7987
/// Query tags from a desired target.
8088
/// </summary>
8189
/// <param name="queryTarget">The target to query.</param>

src/Verlite.Core/PublicAPI.Shipped.txt

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Verlite.GitRepoInspector.CanDeepen.set -> void
6060
Verlite.GitRepoInspector.FetchTag(Verlite.Tag tag, string! remote) -> System.Threading.Tasks.Task!
6161
Verlite.GitRepoInspector.GetHead() -> System.Threading.Tasks.Task<Verlite.Commit?>!
6262
Verlite.GitRepoInspector.GetParent(Verlite.Commit commit) -> System.Threading.Tasks.Task<Verlite.Commit?>!
63+
Verlite.GitRepoInspector.GetParents(Verlite.Commit commit) -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Verlite.Commit>!>!
6364
Verlite.GitRepoInspector.GetTags(Verlite.QueryTarget queryTarget) -> System.Threading.Tasks.Task<Verlite.TagContainer!>!
6465
Verlite.GitRepoInspector.Root.get -> string!
6566
Verlite.HeightCalculator
@@ -73,6 +74,7 @@ Verlite.IRepoInspector
7374
Verlite.IRepoInspector.FetchTag(Verlite.Tag tag, string! remote) -> System.Threading.Tasks.Task!
7475
Verlite.IRepoInspector.GetHead() -> System.Threading.Tasks.Task<Verlite.Commit?>!
7576
Verlite.IRepoInspector.GetParent(Verlite.Commit commit) -> System.Threading.Tasks.Task<Verlite.Commit?>!
77+
Verlite.IRepoInspector.GetParents(Verlite.Commit commit) -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Verlite.Commit>!>!
7678
Verlite.IRepoInspector.GetTags(Verlite.QueryTarget queryTarget) -> System.Threading.Tasks.Task<Verlite.TagContainer!>!
7779
Verlite.ITagFilter
7880
Verlite.ITagFilter.PassesFilter(Verlite.TaggedVersion! taggedVersion) -> System.Threading.Tasks.Task<bool>!

tests/UnitTests/HeightCalculationTests.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public async Task HeadOnTagReturnsIt()
1818
{
1919
var repo = new MockRepoInspector(new MockRepoCommit[]
2020
{
21-
new("commit_a", "v1.0.0"),
22-
new("commit_b", "v1.0.0-alpha.1"),
21+
new("commit_a") { Tags = new[]{ "v1.0.0" } },
22+
new("commit_b") { Tags = new[]{ "v1.0.0-alpha.1" } },
2323
new("commit_c"),
2424
});
2525

@@ -80,7 +80,7 @@ public async Task NoSemVerTagReturnsNoTag()
8080
{
8181
var repo = new MockRepoInspector(new MockRepoCommit[]
8282
{
83-
new("commit_a", "v1.0.0.0"),
83+
new("commit_a") { Tags = new[]{ "v1.0.0.0" } },
8484
new("commit_b"),
8585
new("commit_c"),
8686
});
@@ -97,9 +97,9 @@ public async Task MatchingPrefixNonVersionTagsIgnored()
9797
{
9898
var repo = new MockRepoInspector(new MockRepoCommit[]
9999
{
100-
new("commit_a", "vast-tag"),
101-
new("commit_b", "very-nice-tag"),
102-
new("commit_c", "v"),
100+
new("commit_a") { Tags = new[]{ "vast-tag" } },
101+
new("commit_b") { Tags = new[]{ "very-nice-tag" } },
102+
new("commit_c") { Tags = new[]{ "v" } },
103103
});
104104

105105
var (height, tag) = await HeightCalculator.FromRepository(repo, "v", true);
@@ -113,7 +113,7 @@ public async Task MultipleTagsReturnsHighestTag()
113113
{
114114
var repo = new MockRepoInspector(new MockRepoCommit[]
115115
{
116-
new("commit_a", "v1.0.0-alpha.1", "v1.0.0", "v1.0.0-rc.1"),
116+
new("commit_a") { Tags = new[]{ "v1.0.0-alpha.1", "v1.0.0", "v1.0.0-rc.1" } },
117117
new("commit_b"),
118118
new("commit_c"),
119119
});
@@ -135,7 +135,7 @@ public async Task MultipleTagsReturnsHighestTagWhenAutoVersioning()
135135
var repo = new MockRepoInspector(new MockRepoCommit[]
136136
{
137137
new("commit_a"),
138-
new("commit_b", "v1.0.0-alpha.1", "v1.0.0-rc.2", "v1.0.0-rc.10"),
138+
new("commit_b") { Tags = new[]{ "v1.0.0-alpha.1", "v1.0.0-rc.2", "v1.0.0-rc.10" } },
139139
new("commit_c"),
140140
});
141141

@@ -158,7 +158,7 @@ public async Task QueryRemoteTagsDoesNotFetchTag()
158158
{
159159
new("commit_a"),
160160
new("commit_b"),
161-
new("commit_c", "v1.0.0-alpha.1"),
161+
new("commit_c") { Tags = new[]{ "v1.0.0-alpha.1" } },
162162
});
163163

164164
(_, _) = await HeightCalculator.FromRepository(repo, "v", true);
@@ -176,9 +176,9 @@ public async Task FindsCommitsWithPrefixesOnly(string prefix, int resultHeight,
176176
{
177177
var repo = new MockRepoInspector(new MockRepoCommit[]
178178
{
179-
new("commit_a", "abc/version/2.0.0"),
180-
new("commit_b", "version/3.0.0"),
181-
new("commit_c", "4.0.0"),
179+
new("commit_a") { Tags = new[]{ "abc/version/2.0.0" } },
180+
new("commit_b") { Tags = new[]{ "version/3.0.0" } },
181+
new("commit_c") { Tags = new[]{ "4.0.0" } },
182182
new("commit_d"),
183183
});
184184

@@ -209,8 +209,8 @@ public async Task FilterCanIgnoreTags()
209209
{
210210
var repo = new MockRepoInspector(new MockRepoCommit[]
211211
{
212-
new("commit_a", "v1.0.0"),
213-
new("commit_b", "v1.0.0-alpha.1"),
212+
new("commit_a") { Tags = new[]{ "v1.0.0" } },
213+
new("commit_b") { Tags = new[]{ "v1.0.0-alpha.1" } },
214214
new("commit_c"),
215215
});
216216

@@ -232,8 +232,8 @@ public async Task FilterDoesntIgnoreOtherTagsOnSameCommit()
232232
{
233233
var repo = new MockRepoInspector(new MockRepoCommit[]
234234
{
235-
new("commit_a", "v1.0.0", "v1.0.1"),
236-
new("commit_b", "v1.0.0-alpha.1"),
235+
new("commit_a") { Tags = new[]{ "v1.0.0", "v1.0.1" } },
236+
new("commit_b") { Tags = new[]{ "v1.0.0-alpha.1" } },
237237
new("commit_c"),
238238
});
239239

tests/UnitTests/Mocks/MockRepoInspector.cs

+23-17
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,13 @@ namespace UnitTests
1010
{
1111
public class MockRepoCommit
1212
{
13-
public Commit Id { get; }
14-
public IReadOnlyList<Tag> Tags { get; }
13+
public Commit Id { get; init; }
14+
public IReadOnlyList<Commit>? Parents { get; init; }
15+
public IReadOnlyList<string> Tags { get; init; } = Array.Empty<string>();
1516

16-
public MockRepoCommit(string commitId, params string[] tags)
17+
public MockRepoCommit(string commitId)
1718
{
1819
Id = new Commit(commitId);
19-
if (tags.Length == 0)
20-
Tags = Array.Empty<Tag>();
21-
else
22-
{
23-
var tagsList = new List<Tag>();
24-
foreach (string tag in tags)
25-
tagsList.Add(new Tag(tag, Id));
26-
Tags = tagsList;
27-
}
2820
}
2921
}
3022

@@ -33,7 +25,7 @@ public sealed class MockRepoInspector : IRepoInspector
3325
private class InternalCommit
3426
{
3527
public Commit Id { get; set; }
36-
public Commit? ParentId { get; set; }
28+
public IReadOnlyList<Commit> Parents { get; set; } = Array.Empty<Commit>();
3729
}
3830
private List<InternalCommit> Commits { get; } = new();
3931
internal List<Tag> LocalTags { get; } = new();
@@ -49,15 +41,18 @@ public MockRepoInspector(IReadOnlyList<MockRepoCommit> commits)
4941
Commits.Add(new InternalCommit()
5042
{
5143
Id = commit.Id,
52-
ParentId = parent,
44+
Parents = commit.Parents ??
45+
(parent is not null
46+
? new Commit[] { parent!.Value }
47+
: Array.Empty<Commit>()),
5348
});
5449

5550
foreach (var tag in commit.Tags)
5651
{
57-
if (!addedTags.Add(tag.Name))
52+
if (!addedTags.Add(tag))
5853
throw new ArgumentException("Commits contained duplicate tag!", nameof(commits));
5954
}
60-
RemoteTags.AddRange(commit.Tags);
55+
RemoteTags.AddRange(commit.Tags.Select(t => new Tag(t, commit.Id)));
6156

6257
parent = commit.Id;
6358
}
@@ -78,7 +73,18 @@ async Task IRepoInspector.FetchTag(Tag tag, string remote)
7873

7974
async Task<Commit?> IRepoInspector.GetParent(Commit commit)
8075
{
81-
return Commits.Where(ic => ic.Id == commit).FirstOrDefault()?.ParentId;
76+
var parents = await (this as IRepoInspector).GetParents(commit);
77+
return parents
78+
.Select(p => (Commit?)p)
79+
.FirstOrDefault();
80+
}
81+
82+
async Task<IReadOnlyList<Commit>> IRepoInspector.GetParents(Commit commit)
83+
{
84+
return Commits
85+
.Where(ic => ic.Id == commit)
86+
.First()
87+
.Parents;
8288
}
8389

8490
async Task<TagContainer> IRepoInspector.GetTags(QueryTarget queryTarget)

tests/UnitTests/UnitTests.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111

1212
<ItemGroup>
13+
<Compile Remove="StrykerOutput\**" />
1314
<Compile Remove="TestResults\**" />
15+
<EmbeddedResource Remove="StrykerOutput\**" />
1416
<EmbeddedResource Remove="TestResults\**" />
17+
<None Remove="StrykerOutput\**" />
1518
<None Remove="TestResults\**" />
1619
</ItemGroup>
1720

tests/UnitTests/VersionCalculationTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public async Task FirstCommitTaggedHasExactVersion()
131131
{
132132
var repo = new MockRepoInspector(new MockRepoCommit[]
133133
{
134-
new("commit_a", "v5.4.3-rc.2.1"),
134+
new("commit_a") { Tags = new[] { "v5.4.3-rc.2.1" } },
135135
});
136136

137137
var semver = await VersionCalculator.FromRepository(repo, new() { QueryRemoteTags = true });
@@ -145,7 +145,7 @@ public async Task CommitAfterPrereleaseTagAppendsHeight()
145145
var repo = new MockRepoInspector(new MockRepoCommit[]
146146
{
147147
new("commit_b"),
148-
new("commit_a", "v4.3.2-rc.1"),
148+
new("commit_a") { Tags = new[] { "v4.3.2-rc.1" } },
149149
});
150150

151151
var semver = await VersionCalculator.FromRepository(repo, new() { QueryRemoteTags = true });
@@ -159,7 +159,7 @@ public async Task CommitAfterRtmTagAppendsHeightAndBumpsMinor()
159159
var repo = new MockRepoInspector(new MockRepoCommit[]
160160
{
161161
new("commit_b"),
162-
new("commit_a", "v3.2.1"),
162+
new("commit_a") { Tags = new[] { "v3.2.1" } },
163163
});
164164

165165
var semver = await VersionCalculator.FromRepository(repo, new() { QueryRemoteTags = true });
@@ -173,8 +173,8 @@ public async Task LatestTagIsFetchedWithQueryRemoteTags()
173173
var repo = new MockRepoInspector(new MockRepoCommit[]
174174
{
175175
new("commit_c"),
176-
new("commit_b", "v3.2.1"),
177-
new("commit_a", "v3.2.0"),
176+
new("commit_b") { Tags = new[] { "v3.2.1" } },
177+
new("commit_a") { Tags = new[] { "v3.2.0" } },
178178
});
179179

180180
_ = await VersionCalculator.FromRepository(repo, new() { QueryRemoteTags = true });
@@ -188,8 +188,8 @@ public async Task LatestTagIsNotFetchedWithoutQueryRemoteTags()
188188
var repo = new MockRepoInspector(new MockRepoCommit[]
189189
{
190190
new("commit_c"),
191-
new("commit_b", "v3.2.1"),
192-
new("commit_a", "v3.2.0"),
191+
new("commit_b") { Tags = new[] { "v3.2.1" } },
192+
new("commit_a") { Tags = new[] { "v3.2.0" } },
193193
});
194194

195195
_ = await VersionCalculator.FromRepository(repo, new() { QueryRemoteTags = false });

0 commit comments

Comments
 (0)