8000 Allow to query the commit history by specifying two named commits as … · joncham/libgit2sharp@1125c04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1125c04

Browse files
committed
Allow to query the commit history by specifying two named commits as boundaries
1 parent 3034aa9 commit 1125c04

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 12 additions & 0 deletions
< 8000 /div>
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ public void CanEnumerateCommitsWithTopoSorting()
168168
}
169169
}
170170

171+
[Test]
172+
public void CanEnumerateUsingTwoCommitsAsBoundaries()
173+
{
174+
using (var repo = new Repository(Constants.TestRepoPath))
175+
{
176+
var commits = repo.Commits.QueryBy(new Filter { Since = "refs/heads/br2", Until = "refs/heads/packed-test" });
177+
178+
IEnumerable<string> abbrevShas = commits.Select(c => c.Id.Sha.Substring(0, 7)).ToArray();
179+
CollectionAssert.AreEquivalent(new[] { "a4a7dce", "c47800c", "9fd738e" }, abbrevShas);
180+
}
181+
}
182+
171183
[Test]
172184
public void CanLookupCommitGeneric()
173185
{

LibGit2Sharp/CommitCollection.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CommitCollection : IQueryableCommitCollection
1313
{
1414
private readonly Repository repo;
1515
private ObjectId pushedObjectId;
16+
private ObjectId hiddenObjectId;
1617
private readonly GitSortOptions sortOptions;
1718

1819
/// <summary>
@@ -53,10 +54,10 @@ public IEnumerator<Commit> GetEnumerator()
5354
{
5455
if (pushedObjectId == null)
5556
{
56-
throw new NotImplementedException();
57+
throw new InvalidOperationException();
5758
}
5859

59-
return new CommitEnumerator(repo, pushedObjectId, sortOptions);
60+
return new CommitEnumerator(repo, pushedObjectId, hiddenObjectId, sortOptions);
6061
}
6162

6263
/// <summary>
@@ -80,21 +81,35 @@ public ICommitCollection QueryBy(Filter filter)
8081
Ensure.ArgumentNotNull(filter, "filter");
8182
Ensure.ArgumentNotNull(filter.Since, "filter.Since");
8283

83-
string shaOrRefName = filter.Since.ToString();
84+
string sinceIdentifier = filter.Since.ToString();
8485

85-
if ((repo.Info.IsEmpty) && PointsAtTheHead(shaOrRefName))
86+
if ((repo.Info.IsEmpty) && PointsAtTheHead(sinceIdentifier))
8687
{
8788
return new EmptyCommitCollection(filter.SortBy);
88-
}
89+
}
90+
91+
ObjectId sinceObjectId = RetrieveCommitId(sinceIdentifier);
92+
ObjectId untilObjectId = null;
93+
94+
if (filter.Until != null)
95+
{
96+
untilObjectId = RetrieveCommitId(filter.Until.ToString());
97+
}
98+
99+
return new CommitCollection(repo, filter.SortBy) { pushedObjectId = sinceObjectId, hiddenObjectId = untilObjectId};
100+
}
89101

90-
GitObject gitObj = repo.Lookup(shaOrRefName);
102+
private ObjectId RetrieveCommitId(string shaOrReferenceName)
103+
{
104+
GitObject gitObj = repo.Lookup(shaOrReferenceName);
91105

92-
if (gitObj == null) // TODO: Should we check the type? Git-log allows TagAnnotation oid as parameter. But what about Blobs and Trees?
106+
// TODO: Should we check the type? Git-log allows TagAnnotation oid as parameter. But what about Blobs and Trees?
107+
if (gitObj == null)
93108
{
94-
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No valid git object pointed at by '{0}' exists in the repository.", filter.Since));
109+
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No valid git object pointed at by '{0}' exists in the repository.", shaOrReferenceName));
95110
}
96111

97-
return new CommitCollection(repo, filter.SortBy) { pushedObjectId = gitObj.Id };
112+
return gitObj.Id;
98113
}
99114

100115
private static bool PointsAtTheHead(string shaOrRefName)
@@ -110,14 +125,15 @@ private class CommitEnumerator : IEnumerator<Commit>
110125
private readonly RevWalkerSafeHandle handle;
111126
private ObjectId currentOid;
112127

113-
public CommitEnumerator(Repository repo, ObjectId pushedOid, GitSortOptions sortingStrategy)
128+
public CommitEnumerator(Repository repo, ObjectId pushedOid, ObjectId hiddenOid, GitSortOptions sortingStrategy)
114129
{
115130
this.repo = repo;
116131
int res = NativeMethods.git_revwalk_new(out handle, repo.Handle);
117132
Ensure.Success(res);
118133

119134
Sort(sortingStrategy);
120135
Push(pushedOid);
136+
Hide(hiddenOid);
121137
}
122138

123139
#region IEnumerator<Commit> Members
@@ -187,6 +203,18 @@ private void Push(ObjectId pushedOid)
187203
Ensure.Success(res);
188204
}
189205

206+
private void Hide(ObjectId hiddenOid)
207+
{
208+
if (hiddenOid == null)
209+
{
210+
return;
211+
}
212+
213+
var oid = hiddenOid.Oid;
214+
int res = NativeMethods.git_revwalk_hide(handle, ref oid);
215+
Ensure.Success(res);
216+
}
217+
190218
private void Sort(GitSortOptions options)
191219
{
192220
NativeMethods.git_revwalk_sorting(handle, options);

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ internal static class NativeMethods
159159
[DllImport(libgit2)]
160160
public static extern void git_revwalk_free(IntPtr walker);
161161

162+
[DllImport(libgit2)]
163+
public static extern int git_revwalk_hide(RevWalkerSafeHandle walker, ref GitOid oid);
164+
162165
[DllImport(libgit2)]
163166
public static extern int git_revwalk_new(out RevWalkerSafeHandle walker, RepositorySafeHandle repo);
164167

LibGit2Sharp/Filter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,14 @@ public Filter()
3030
/// </para>
3131
/// </summary>
3232
public object Since { get; set; }
33+
34+
35+
/// <summary>
36+
/// The pointer to the commit which will be excluded (along with its ancestors) from the enumeration.
37+
/// <para>
38+
/// Can be either a <see cref="string"/> containing the sha or reference canonical name to use, a <see cref="Branch"/> or a <see cref="Reference"/>.
39+
/// </para>
40+
/// </summary>
41+
public object Until { get; set; }
3342
}
3443
}

0 commit comments

Comments
 (0)
0