Skip to content

Commit 0a6c5bf

Browse files
committed
Add RevWalker
1 parent 8c32b61 commit 0a6c5bf

File tree

5 files changed

+293
-57
lines changed

5 files changed

+293
-57
lines changed

LibGit2Sharp.Tests/RepositoryFixture.cs

+41
Original file line numberDiff line numberDiff line change
@@ -775,5 +775,46 @@ public void ReadingReferenceTargetFromListRemoteReferencesThrows(string url)
775775
});
776776
}
777777
}
778+
779+
[Fact]
780+
public void RevWalkRepository()
781+
{
782+
string path = SandboxBareTestRepo();
783+
using (var repo = new Repository(path))
784+
using (var walker = new RevWalker(repo))
785+
{
786+
walker.Sorting(CommitSortStrategies.Topological);
787+
788+
Assert.Empty(GetCommits().ToList());
789+
790+
walker.PushRef("HEAD");
791+
Assert.Equal(7, GetCommits().Count());
792+
793+
walker.HideRef("HEAD");
794+
Assert.Empty(GetCommits());
795+
796+
walker.Reset();
797+
798+
walker.PushGlob("refs/heads/*");
799+
Assert.Equal(12, GetCommits().Count());
800+
801+
walker.HideGlob("refs/*");
802+
Assert.Empty(GetCommits());
803+
804+
IEnumerable<Commit> GetCommits()
805+
{
806+
while (true)
807+
{
808+
var objectId = walker.Next();
809+
if (objectId == null)
810+
break;
811+
812+
var commit = repo.Lookup<Commit>(objectId);
813+
yield return commit;
814+
}
815+
}
816+
}
817+
818+
}
778819
}
779820
}

LibGit2Sharp/CommitLog.cs

+25-52
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,34 @@ public IEnumerable<LogEntry> QueryBy(string path, CommitFilter filter)
108108
private class CommitEnumerator : IEnumerator<Commit>
109109
{
110110
private readonly Repository repo;
111-
private readonly RevWalkerHandle handle;
111+
private readonly RevWalker walker;
112112
private ObjectId currentOid;
113113

114114
public CommitEnumerator(Repository repo, CommitFilter filter)
115115
{
116116
this.repo = repo;
117-
handle = Proxy.git_revwalk_new(repo.Handle);
118-
repo.RegisterForCleanup(handle);
119117

120-
Sort(filter.SortBy);
121-
Push(filter.SinceList);
122-
Hide(filter.UntilList);
123-
FirstParentOnly(filter.FirstParentOnly);
118+
walker = new RevWalker(repo);
119+
120+
walker.Sorting(filter.SortBy);
121+
122+
foreach (ObjectId actedOn in repo.Committishes(filter.SinceList).TakeWhile(o => o != null))
123+
{
124+
walker.Push(actedOn);
125+
}
126+
127+
if(filter.UntilList != null)
128+
{
129+
foreach (ObjectId actedOn in repo.Committishes(filter.UntilList).TakeWhile(o => o != null))
130+
{
131+
walker.Hide(actedOn);
132+
}
133+
}
134+
135+
if (filter.FirstParentOnly)
136+
{
137+
walker.SimplifyFirstParent();
138+
}
124139
}
125140

126141
#region IEnumerator<Commit> Members
@@ -137,21 +152,19 @@ object IEnumerator.Current
137152

138153
public bool MoveNext()
139154
{
140-
ObjectId id = Proxy.git_revwalk_next(handle);
141-
155+
ObjectId id = walker.Next();
142156
if (id == null)
143157
{
144158
return false;
145159
}
146160

147161
currentOid = id;
148-
149162
return true;
150163
}
151164

152165
public void Reset()
153166
{
154-
Proxy.git_revwalk_reset(handle);
167+
walker.Reset();
155168
}
156169

157170
#endregion
@@ -164,47 +177,7 @@ public void Dispose()
164177

165178
private void Dispose(bool disposing)
166179
{
167-
handle.SafeDispose();
168-
}
169-
170-
private delegate void HidePushSignature(RevWalkerHandle handle, ObjectId id);
171-
172-
private void InternalHidePush(IList<object> identifier, HidePushSignature hidePush)
173-
{
174-
IEnumerable<ObjectId> oids = repo.Committishes(identifier).TakeWhile(o => o != null);
175-
176-
foreach (ObjectId actedOn in oids)
177-
{
178-
hidePush(handle, actedOn);
179-
}
180-
}
181-
182-
private void Push(IList<object> identifier)
183-
{
184-
InternalHidePush(identifier, Proxy.git_revwalk_push);
185-
}
186-
187-
private void Hide(IList<object> identifier)
188-
{
189-
if (identifier == null)
190-
{
191-
return;
192-
}
193-
194-
InternalHidePush(identifier, Proxy.git_revwalk_hide);
195-
}
196-
197-
private void Sort(CommitSortStrategies options)
198-
{
199-
Proxy.git_revwalk_sorting(handle, options);
200-
}
201-
202-
private void FirstParentOnly(bool firstParent)
203-
{
204-
if (firstParent)
205-
{
206-
Proxy.git_revwalk_simplify_first_parent(handle);
207-
}
180+
walker.SafeDispose();
208181
}
209182
}
210183
}

LibGit2Sharp/Core/NativeMethods.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,14 @@ internal static extern unsafe int git_revparse_ext(
16861686
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
16871687
internal static extern unsafe int git_revwalk_hide(git_revwalk* walker, ref GitOid commit_id);
16881688

1689+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
1690+
internal static extern unsafe int git_revwalk_hide_glob(git_revwalk* walker,
1691+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string glob);
1692+
1693+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
1694+
internal static extern unsafe int git_revwalk_hide_ref(git_revwalk* walker,
1695+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refname);
1696+
16891697
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
16901698
internal static extern unsafe int git_revwalk_new(out git_revwalk* walker, git_repository* repo);
16911699

@@ -1695,6 +1703,14 @@ internal static extern unsafe int git_revparse_ext(
16951703
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
16961704
internal static extern unsafe int git_revwalk_push(git_revwalk* walker, ref GitOid id);
16971705

1706+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
1707+
internal static extern unsafe int git_revwalk_push_glob(git_revwalk* walker,
1708+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string glob);
1709+
1710+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
1711+
internal static extern unsafe int git_revwalk_push_ref(git_revwalk* walker,
1712+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refname);
1713+
16981714
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
16991715
internal static extern unsafe int git_revwalk_reset(git_revwalk* walker);
17001716

LibGit2Sharp/Core/Proxy.cs

+32-5
Original file line numberDiff line numberDiff line change
@@ -2752,6 +2752,18 @@ public static unsafe void git_revwalk_hide(RevWalkerHandle walker, ObjectId comm
27522752
Ensure.ZeroResult(res);
27532753
}
27542754

2755+
public static unsafe void git_revwalk_hide_glob(RevWalkerHandle walker, string glob)
2756+
{
2757+
int res = NativeMethods.git_revwalk_hide_glob(walker, glob);
2758+
Ensure.ZeroResult(res);
2759+
}
2760+
2761+
public static unsafe void git_revwalk_hide_ref(RevWalkerHandle walker, string refName)
2762+
{
2763+
int res = NativeMethods.git_revwalk_hide_ref(walker, refName);
2764+
Ensure.ZeroResult(res);
2765+
}
2766+
27552767
public static unsafe RevWalkerHandle git_revwalk_new(RepositoryHandle repo)
27562768
{
27572769
git_revwalk* handle;
@@ -2783,19 +2795,34 @@ public static unsafe void git_revwalk_push(RevWalkerHandle walker, ObjectId id)
27832795
Ensure.ZeroResult(res);
27842796
}
27852797

2798+
public static unsafe void git_revwalk_push_glob(RevWalkerHandle walker, string glob)
2799+
{
2800+
int res = NativeMethods.git_revwalk_push_glob(walker, glob);
2801+
Ensure.ZeroResult(res);
2802+
}
2803+
2804+
public static unsafe void git_revwalk_push_ref(RevWalkerHandle walker, string refName)
2805+
{
2806+
int res = NativeMethods.git_revwalk_push_ref(walker, refName);
2807+
Ensure.ZeroResult(res);
2808+
}
2809+
27862810
public static unsafe void git_revwalk_reset(RevWalkerHandle walker)
27872811
{
2788-
NativeMethods.git_revwalk_reset(walker);
2812+
int res = NativeMethods.git_revwalk_reset(walker);
2813+
Ensure.ZeroResult(res);
27892814
}
27902815

2791-
public static unsafe int git_revwalk_sorting(RevWalkerHandle walker, CommitSortStrategies options)
2816+
public static unsafe void git_revwalk_sorting(RevWalkerHandle walker, CommitSortStrategies options)
27922817
{
2793-
return NativeMethods.git_revwalk_sorting(walker, options);
2818+
int res = NativeMethods.git_revwalk_sorting(walker, options);
2819+
Ensure.ZeroResult(res);
27942820
}
27952821

2796-
public static unsafe int git_revwalk_simplify_first_parent(RevWalkerHandle walker)
2822+
public static unsafe void git_revwalk_simplify_first_parent(RevWalkerHandle walker)
27972823
{
2798-
return NativeMethods.git_revwalk_simplify_first_parent(walker);
2824+
int res = NativeMethods.git_revwalk_simplify_first_parent(walker);
2825+
Ensure.ZeroResult(res);
27992826
}
28002827

28012828
#endregion

0 commit comments

Comments
 (0)