Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43521a6

Browse files
committed
Disentangle the reference and config iterators
While these both work roughly in the same manner (as they both representiterators) the actual code duplication between these two is rather smallas each `_next()` method is different and how we return the managed valuesis different. The net reduction in lines of code indicates that this isindeed the case.
1 parent966ec5a commit43521a6

File tree

4 files changed

+57
-96
lines changed

4 files changed

+57
-96
lines changed

‎LibGit2Sharp/Configuration.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ public virtual IEnumerable<ConfigurationEntry<string>> Find(string regexp, Confi
679679
using(ConfigurationSafeHandlesnapshot=Snapshot())
680680
using(ConfigurationSafeHandleh=RetrieveConfigurationHandle(level,true,snapshot))
681681
{
682-
returnProxy.git_config_iterator_glob(h,regexp,BuildConfigEntry).ToList();
682+
returnProxy.git_config_iterator_glob(h,regexp).ToList();
683683
}
684684
}
685685

@@ -732,7 +732,7 @@ private IEnumerable<ConfigurationEntry<string>> BuildConfigEntries()
732732
returnProxy.git_config_foreach(configHandle,BuildConfigEntry);
733733
}
734734

735-
privatestaticunsafeConfigurationEntry<string>BuildConfigEntry(IntPtrentryPtr)
735+
internalstaticunsafeConfigurationEntry<string>BuildConfigEntry(IntPtrentryPtr)
736736
{
737737
varentry=(GitConfigEntry*)entryPtr.ToPointer();
738738
returnnewConfigurationEntry<string>(LaxUtf8Marshaler.FromNative(entry->namePtr),

‎LibGit2Sharp/Core/Handles/GitReferenceHandle.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ internal GitReferenceHandle(git_reference* refPtr)
1111
this.ptr=refPtr;
1212
}
1313

14+
internalGitReferenceHandle(IntPtrrefPtr)
15+
{
16+
this.ptr=(git_reference*)refPtr.ToPointer();
17+
}
18+
1419
~GitReferenceHandle()
1520
{
1621
Dispose();
@@ -31,7 +36,7 @@ internal bool IsNull
3136

3237
publicvoidDispose()
3338
{
34-
NativeMethods.git_reference_free(newIntPtr(ptr));
39+
NativeMethods.git_reference_free(ptr);
3540
ptr=null;
3641
}
3742
}

‎LibGit2Sharp/Core/NativeMethods.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ internal static extern int git_reference_foreach_glob(
10131013
IntPtrpayload);
10141014

10151015
[DllImport(libgit2)]
1016-
internalstaticexternvoidgit_reference_free(IntPtrreference);
1016+
internalstaticexternunsafevoidgit_reference_free(git_reference*reference);
10171017

10181018
[DllImport(libgit2)]
10191019
internalstaticexternintgit_reference_is_valid_name(

‎LibGit2Sharp/Core/Proxy.cs‎

Lines changed: 48 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,33 @@ public static unsafe void git_branch_delete(GitReferenceHandle reference)
209209
Ensure.ZeroResult(res);
210210
}
211211

212-
publicstaticunsafeIEnumerable<Branch>git_branch_iterator(Repositoryrepo,GitBranchTypebranchType)
213-
{
214-
returngit_iterator((outBranchIteratorSafeHandleiter_out)=>
215-
NativeMethods.git_branch_iterator_new(outiter_out,repo.Handle,branchType),
216-
(BranchIteratorSafeHandleiter,outIntPtrrefPtr,outintres)=>
217-
{
218-
GitBranchTypetype_out;
219-
res=NativeMethods.git_branch_next(outrefPtr,outtype_out,iter);
220-
returnnew{BranchType=type_out};
221-
},
222-
(handle,payload)=>
223-
{
224-
git_reference*refPtr=(git_reference*)handle.ToPointer();
225-
varreference=Reference.BuildFromPtr<Reference>(refPtr,repo);
226-
returnnewBranch(repo,reference,reference.CanonicalName);
227-
});
212+
publicstaticIEnumerable<Branch>git_branch_iterator(Repositoryrepo,GitBranchTypebranchType)
213+
{
214+
BranchIteratorSafeHandleiter;
215+
varres=NativeMethods.git_branch_iterator_new(outiter,repo.Handle,branchType);
216+
Ensure.ZeroResult(res);
217+
218+
using(iter)
219+
{
220+
while(true)
221+
{
222+
IntPtrrefPtr=IntPtr.Zero;
223+
GitBranchType_branchType;
224+
res=NativeMethods.git_branch_next(outrefPtr,out_branchType,iter);
225+
if(res==(int)GitErrorCode.IterOver)
226+
{
227+
yieldbreak;
228+
}
229+
Ensure.ZeroResult(res);
230+
231+
Referencereference;
232+
using(varrefHandle=newGitReferenceHandle(refPtr))
233+
{
234+
reference=Reference.BuildFromPtr<Reference>(refHandle,repo);
235+
}
236+
yieldreturnnewBranch(repo,reference,reference.CanonicalName);
237+
}
238+
}
228239
}
229240

230241
publicstaticvoidgit_branch_iterator_free(IntPtriter)
@@ -583,22 +594,28 @@ public static ICollection<TResult> git_config_foreach<TResult>(
583594
returngit_foreach(resultSelector, c=>NativeMethods.git_config_foreach(config,(e,p)=>c(e,p),IntPtr.Zero));
584595
}
585596

586-
publicstaticunsafeIEnumerable<ConfigurationEntry<string>>git_config_iterator_glob(
597+
publicstaticIEnumerable<ConfigurationEntry<string>>git_config_iterator_glob(
587598
ConfigurationSafeHandleconfig,
588-
stringregexp,
589-
Func<IntPtr,ConfigurationEntry<string>>resultSelector)
590-
{
591-
returngit_iterator((outConfigurationIteratorSafeHandleiter)=>
592-
NativeMethods.git_config_iterator_glob_new(outiter,config,regexp),
593-
(ConfigurationIteratorSafeHandleiter,outIntPtrhandle,outintres)=>
594-
{
595-
handle=IntPtr.Zero;
596-
IntPtrentry;
597-
res=NativeMethods.git_config_next(outentry,iter);
598-
returnnew{EntryPtr=entry};
599-
},
600-
(handle,payload)=>
601-
resultSelector(payload.EntryPtr));
599+
stringregexp)
600+
{
601+
ConfigurationIteratorSafeHandleiter;
602+
varres=NativeMethods.git_config_iterator_glob_new(outiter,config,regexp);
603+
Ensure.ZeroResult(res);
604+
using(iter)
605+
{
606+
while(true)
607+
{
608+
IntPtrentry;
609+
res=NativeMethods.git_config_next(outentry,iter);
610+
if(res==(int)GitErrorCode.IterOver)
611+
{
612+
yieldbreak;
613+
}
614+
Ensure.ZeroResult(res);
615+
616+
yieldreturnConfiguration.BuildConfigEntry(entry);
617+
}
618+
}
602619
}
603620

604621
publicstaticvoidgit_config_iterator_free(IntPtriter)
@@ -1809,11 +1826,6 @@ public static ICollection<TResult> git_reference_foreach_glob<TResult>(
18091826
returngit_foreach(resultSelector, c=>NativeMethods.git_reference_foreach_glob(repo,glob,(x,p)=>c(x,p),IntPtr.Zero));
18101827
}
18111828

1812-
publicstaticvoidgit_reference_free(IntPtrreference)
1813-
{
1814-
NativeMethods.git_reference_free(reference);
1815-
}
1816-
18171829
publicstaticboolgit_reference_is_valid_name(stringrefname)
18181830
{
18191831
intres=NativeMethods.git_reference_is_valid_name(refname);
@@ -3380,62 +3392,6 @@ private static ICollection<TResult> git_foreach<T1, T2, T3, T4, TResult>(
33803392
returnresult;
33813393
}
33823394

3383-
privatedelegateintIteratorNew<THandle>(outTHandleiter);
3384-
3385-
privatedelegateTPayloadIteratorNext<inTIterator,THandle,outTPayload>(TIteratoriter,outTHandlenext,outintres);
3386-
3387-
privatestaticTHandlegit_iterator_new<THandle>(IteratorNew<THandle>newFunc)
3388-
whereTHandle:SafeHandleBase
3389-
{
3390-
THandleiter;
3391-
Ensure.ZeroResult(newFunc(outiter));
3392-
returniter;
3393-
}
3394-
3395-
privatestaticIEnumerable<TResult>git_iterator_next<TIterator,TPayload,TResult>(
3396-
TIteratoriter,
3397-
IteratorNext<TIterator,IntPtr,TPayload>nextFunc,
3398-
Func<IntPtr,TPayload,TResult>resultSelector)
3399-
{
3400-
while(true)
3401-
{
3402-
varnext=IntPtr.Zero;
3403-
try
3404-
{
3405-
intres;
3406-
varpayload=nextFunc(iter,outnext,outres);
3407-
3408-
if(res==(int)GitErrorCode.IterOver)
3409-
{
3410-
yieldbreak;
3411-
}
3412-
3413-
Ensure.ZeroResult(res);
3414-
yieldreturnresultSelector(next,payload);
3415-
}
3416-
finally
3417-
{
3418-
NativeMethods.git_reference_free(next);
3419-
}
3420-
}
3421-
}
3422-
3423-
privatestaticIEnumerable<TResult>git_iterator<TIterator,TPayload,TResult>(
3424-
IteratorNew<TIterator>newFunc,
3425-
IteratorNext<TIterator,IntPtr,TPayload>nextFunc,
3426-
Func<IntPtr,TPayload,TResult>resultSelector
3427-
)
3428-
whereTIterator:SafeHandleBase
3429-
{
3430-
using(variter=git_iterator_new(newFunc))
3431-
{
3432-
foreach(varnextingit_iterator_next(iter,nextFunc,resultSelector))
3433-
{
3434-
yieldreturnnext;
3435-
}
3436-
}
3437-
}
3438-
34393395
privatestaticboolRepositoryStateChecker(RepositorySafeHandlerepo,Func<RepositorySafeHandle,int>checker)
34403396
{
34413397
intres=checker(repo);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp