-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathCommitCollection.cs
More file actions
65 lines (56 loc) · 2.28 KB
/
CommitCollection.cs
File metadata and controls
65 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using GitVersion.Extensions;
using LibGit2Sharp;
namespace GitVersion.Git;
internal sealed class CommitCollection : ICommitCollection
{
public static Dictionary<LibGit2Sharp.Commit, ICommit> s_commits = new Dictionary<LibGit2Sharp.Commit, ICommit>();
private readonly ICommitLog innerCollection;
private readonly Lazy<IReadOnlyCollection<ICommit>> commits;
internal CommitCollection(ICommitLog collection)
{
this.innerCollection = collection.NotNull();
this.commits = new Lazy<IReadOnlyCollection<ICommit>>(() => {
List<ICommit> commits = new List<ICommit>();
foreach (var c in this.innerCollection) {
ICommit gvCommit;
if (s_commits.TryGetValue(c, out gvCommit))
{
commits.Add(gvCommit);
}
else
{
gvCommit = new Commit(c);
commits.Add(gvCommit);
s_commits[c] = gvCommit;
}
}
return commits;
});
}
public IEnumerator<ICommit> GetEnumerator()
=> this.commits.Value.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerable<ICommit> GetCommitsPriorTo(DateTimeOffset olderThan)
=> this.SkipWhile(c => c.When > olderThan);
public IEnumerable<ICommit> QueryBy(CommitFilter commitFilter)
{
var includeReachableFrom = GetReacheableFrom(commitFilter.IncludeReachableFrom);
var excludeReachableFrom = GetReacheableFrom(commitFilter.ExcludeReachableFrom);
var filter = new LibGit2Sharp.CommitFilter
{
IncludeReachableFrom = includeReachableFrom,
ExcludeReachableFrom = excludeReachableFrom,
FirstParentOnly = commitFilter.FirstParentOnly,
SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy
};
var commitLog = ((IQueryableCommitLog)this.innerCollection).QueryBy(filter);
return new CommitCollection(commitLog);
static object? GetReacheableFrom(object? item) =>
item switch
{
Commit c => (LibGit2Sharp.Commit)c,
Branch b => (LibGit2Sharp.Branch)b,
_ => null
};
}
}