8000 Introduce Proxy.git_iterator · apfunk/libgit2sharp@123bd64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 123bd64

Browse files
committed
Introduce Proxy.git_iterator
1 parent 94066ec commit 123bd64

File tree

2 files changed

+76
-37
lines changed

2 files changed

+76
-37
lines changed

LibGit2Sharp/BranchCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private Branch BuildFromReferenceName(string canonicalName)
9292
public virtual IEnumerator<Branch> GetEnumerator()
9393
{
9494
return Proxy.git_branch_iterator(repo, GitBranchType.GIT_BRANCH_LOCAL | GitBranchType.GIT_BRANCH_REMOTE)
95-
.GetEnumerator();
95+
.ToList().GetEnumerator();
9696
}
9797

9898
/// <summary>

LibGit2Sharp/Core/Proxy.cs

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,44 +143,21 @@ public static void git_branch_delete(ReferenceSafeHandle reference)
143143

144144
public static IEnumerable<Branch> git_branch_iterator(Repository repo, GitBranchType branchType)
145145
{
146-
using (ThreadAffinity())
147-
{
148-
BranchIteratorSafeHandle iter_out = null;
149-
150-
try
151-
{
152-
Ensure.ZeroResult(NativeMethods.git_branch_iterator_new(out iter_out, repo.Handle, branchType));
153-
154-
while(true)
146+
return git_iterator(
147+
(out BranchIteratorSafeHandle iter_out) =>
148+
NativeMethods.git_branch_iterator_new(out iter_out, repo.Handle, branchType),
149+
(BranchIteratorSafeHandle iter, out ReferenceSafeHandle ref_out, out int res) =>
155150
{
156-
ReferenceSafeHandle ref_out = null;
157-
try
158-
{
159-
GitBranchType type_out;
160-
var res = NativeMethods.git_branch_next(out ref_out, out type_out, iter_out);
161-
162-
if (res == (int) GitErrorCode.IterOver)
163-
{
164-
yield break;
165-
}
166-
167-
Ensure.ZeroResult(res);
168-
169-
var reference = Reference.BuildFromPtr<Reference>(ref_out, repo);
170-
171-
yield return new Branch(repo, reference, reference.CanonicalName);
172-
}
173-
finally
174-
{
175-
ref_out.SafeDispose();
176-
}
151+
GitBranchType type_out;
152+
res = NativeMethods.git_branch_next(out ref_out, out type_out, iter);
153+
return new { BranchType = type_out };
154+
},
155+
(handle, payload) =>
156+
{
157+
var reference = Reference.BuildFromPtr<Reference>(handle, repo);
158+
return new Branch(repo, reference, reference.CanonicalName);
177159
}
178-
}
179-
finally
180-
{
181-
iter_out.SafeDispose();
182-
}
183-
}
160+
);
184161
}
185162

186163
public static void git_branch_iterator_free(IntPtr iter)
@@ -2522,6 +2499,68 @@ private static ICollection<TResult> git_foreach<T1, T2, T3, T4, TResult>(
25222499
}
25232500
}
25242501

2502+
private delegate int IteratorNew<THandle>(out THandle iter);
2503+
2504+
private delegate TPayload IteratorNext<in TIterator, THandle, out TPayload>(TIterator iter, out THandle next, out int res);
2505+
2506+
private static THandle git_iterator_new<THandle>(IteratorNew<THandle> newFunc)
2507+
where THandle : SafeHandleBase
2508+
{
2509+
THandle iter;
2510+
Ensure.ZeroResult(newFunc(out iter));
2511+
return iter;
2512+
}
2513+
2514+
private static IEnumerable<TResult> git_iterator_next<TIterator, THandle, TPayload, TResult>(
2515+
TIterator iter,
2516+
IteratorNext<TIterator, THandle, TPayload> nextFunc,
2517+
Func<THandle, TPayload, TResult> resultSelector)
2518+
where THandle : SafeHandleBase
2519+
{
2520+
while (true)
2521+
{
2522+
var next = default(THandle);
2523+
try
2524+
{
2525+
int res;
2526+
var payload = nextFunc(iter, out next, out res);
2527+
2528+
if (res == (int)GitErrorCode.IterOver)
2529+
{
2530+
yield break;
2531+
}
2532+
2533+
Ensure.ZeroResult(res);
2534+
yield return result 2364 Selector(next, payload);
2535+
}
2536+
finally
2537+
{
2538+
if (next != null)
2539+
next.SafeDispose();
2540+
}
2541+
}
2542+
}
2543+
2544+
private static IEnumerable<TResult> git_iterator<TIterator, THandle, TPayload, TResult>(
2545+
IteratorNew<TIterator> newFunc,
2546+
IteratorNext<TIterator, THandle, TPayload> nextFunc,
2547+
Func<THandle, TPayload, TResult> resultSelector
2548+
)
2549+
where TIterator : SafeHandleBase
2550+
where THandle : SafeHandleBase
2551+
{
2552+
using (ThreadAffinity())
2553+
{
2554+
using (var iter = git_iterator_new(newFunc))
2555+
{
2556+
foreach (var next in git_iterator_next(iter, nextFunc, resultSelector))
2557+
{
2558+
yield return next;
2559+
}
2560+
}
2561+
}
2562+
}
2563+
25252564
private static unsafe class Libgit2UnsafeHelper
25262565
{
25272566
public static IList<string> BuildListOf(UnSafeNativeMethods.git_strarray strArray)

0 commit comments

Comments
 (0)
0