Skip to content

Commit 4bda9ff

Browse files
tclemnulltoken
authored andcommitted
Refactor extraction of the boundaries of a commit query
1 parent 1c6a28d commit 4bda9ff

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

LibGit2Sharp/CommitLog.cs

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ namespace LibGit2Sharp
1414
public class CommitLog : IQueryableCommitLog
1515
{
1616
private readonly Repository repo;
17-
private IList<object> includedIdentifier = new List<object> { "HEAD" };
18-
private IList<object> excludedIdentifier = new List<object>();
19-
private readonly GitSortOptions sortOptions;
17+
private readonly Filter queryFilter;
2018

2119
/// <summary>
2220
/// Needed for mocking purposes.
@@ -30,27 +28,27 @@ protected CommitLog()
3028
/// </summary>
3129
/// <param name = "repo">The repository.</param>
3230
internal CommitLog(Repository repo)
33-
: this(repo, GitSortOptions.Time)
31+
: this(repo, new Filter())
3432
{
3533
}
3634

3735
/// <summary>
3836
/// Initializes a new instance of the <see cref = "CommitLog" /> class.
3937
/// </summary>
4038
/// <param name = "repo">The repository.</param>
41-
/// <param name = "sortingStrategy">The sorting strategy which should be applied when enumerating the commits.</param>
42-
internal CommitLog(Repository repo, GitSortOptions sortingStrategy)
39+
/// <param name="queryFilter">The filter to use in querying commits</param>
40+
internal CommitLog(Repository repo, Filter queryFilter)
4341
{
4442
this.repo = repo;
45-
sortOptions = sortingStrategy;
43+
this.queryFilter = queryFilter;
4644
}
4745

4846
/// <summary>
4947
/// Gets the current sorting strategy applied when enumerating the log
5048
/// </summary>
5149
public virtual GitSortOptions SortedBy
5250
{
53-
get { return sortOptions; }
51+
get { return queryFilter.SortBy; }
5452
}
5553

5654
#region IEnumerable<Commit> Members
@@ -61,12 +59,12 @@ public virtual GitSortOptions SortedBy
6159
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the log.</returns>
6260
public virtual IEnumerator<Commit> GetEnumerator()
6361
{
64-
if ((repo.Info.IsEmpty) && includedIdentifier.Any(o => PointsAtTheHead(o.ToString()))) // TODO: ToString() == fragile
62+
if ((repo.Info.IsEmpty) && queryFilter.SinceList.Any(o => PointsAtTheHead(o.ToString()))) // TODO: ToString() == fragile
6563
{
6664
return Enumerable.Empty<Commit>().GetEnumerator();
6765
}
6866

69-
return new CommitEnumerator(repo, includedIdentifier, excludedIdentifier, sortOptions);
67+
return new CommitEnumerator(repo, queryFilter);
7068
}
7169

7270
/// <summary>
@@ -91,38 +89,7 @@ public virtual ICommitLog QueryBy(Filter filter)
9189
Ensure.ArgumentNotNull(filter.Since, "filter.Since");
9290
Ensure.ArgumentNotNullOrEmptyString(filter.Since.ToString(), "filter.Since");
9391

94-
return new CommitLog(repo, filter.SortBy)
95-
{
96-
includedIdentifier = ToList(filter.Since),
97-
excludedIdentifier = ToList(filter.Until)
98-
};
99-
}
100-
101-
private static IList<object> ToList(object obj)
102-
{
103-
var list = new List<object>();
104-
105-
if (obj == null)
106-
{
107-
return list;
108-
}
109-
110-
var types = new[]
111-
{
112-
typeof(string), typeof(ObjectId),
113-
typeof(Commit), typeof(TagAnnotation),
114-
typeof(Tag), typeof(Branch), typeof(DetachedHead),
115-
typeof(Reference), typeof(DirectReference), typeof(SymbolicReference)
116-
};
117-
118-
if (types.Contains(obj.GetType()))
119-
{
120-
list.Add(obj);
121-
return list;
122-
}
123-
124-
list.AddRange(((IEnumerable)obj).Cast<object>());
125-
return list;
92+
return new CommitLog(repo, filter);
12693
}
12794

12895
private static bool PointsAtTheHead(string shaOrRefName)
@@ -222,17 +189,17 @@ private class CommitEnumerator : IEnumerator<Commit>
222189
private readonly RevWalkerSafeHandle handle;
223190
private ObjectId currentOid;
224191

225-
public CommitEnumerator(Repository repo, IList<object> includedIdentifier, IList<object> excludedIdentifier, GitSortOptions sortingStrategy)
192+
public CommitEnumerator(Repository repo, Filter filter)
226193
{
227194
this.repo = repo;
228195
int res = NativeMethods.git_revwalk_new(out handle, repo.Handle);
229196
repo.RegisterForCleanup(handle);
230197

231198
Ensure.Success(res);
232199

233-
Sort(sortingStrategy);
234-
Push(includedIdentifier);
235-
Hide(excludedIdentifier);
200+
Sort(filter.SortBy);
201+
Push(filter.SinceList);
202+
Hide(filter.UntilList);
236203
}
237204

238205
#region IEnumerator<Commit> Members

LibGit2Sharp/Filter.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace LibGit2Sharp
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace LibGit2Sharp
26
{
37
/// <summary>
48
/// Criterias used to filter out and order the commits of the repository when querying its history.
@@ -33,6 +37,11 @@ public Filter()
3337
/// </summary>
3438
public object Since { get; set; }
3539

40+
internal IList<object> SinceList
41+
{
42+
get { return ToList(Since); }
43+
}
44+
3645
/// <summary>
3746
/// A pointer to a commit object or a list of pointers which will be excluded (along with ancestors) from the enumeration.
3847
/// <para>
@@ -42,5 +51,37 @@ public Filter()
4251
/// </para>
4352
/// </summary>
4453
public object Until { get; set; }
54+
55+
internal IList<object> UntilList
56+
{
57+
get { return ToList(Until); }
58+
}
59+
60+
private static IList<object> ToList(object obj)
61+
{
62+
var list = new List<object>();
63+
64+
if (obj == null)
65+
{
66+
return list;
67+
}
68+
69+
var types = new[]
70+
{
71+
typeof(string), typeof(ObjectId),
72+
typeof(Commit), typeof(TagAnnotation),
73+
typeof(Tag), typeof(Branch), typeof(DetachedHead),
74+
typeof(Reference), typeof(DirectReference), typeof(SymbolicReference)
75+
};
76+
77+
if (types.Contains(obj.GetType()))
78+
{
79+
list.Add(obj);
80+
return list;
81+
}
82+
83+
list.AddRange(((IEnumerable)obj).Cast<object>());
84+
return list;
85+
}
4586
}
4687
}

0 commit comments

Comments
 (0)