10000 Simplify git_revparse_ext usage · thatfrankdev/libgit2sharp@b6c30fc · GitHub
[go: up one dir, main page]

Skip to content

Commit b6c30fc

Browse files
committed
Simplify git_revparse_ext usage
1 parent 40de694 commit b6c30fc

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

LibGit2Sharp/Core/DisposableTuple.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using LibGit2Sharp.Core.Handles;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
internal class DisposableTuple<T1, T2> : IDisposable
7+
where T1 : IDisposable where T2 : IDisposable
8+
{
9+
private Tuple<T1, T2> _tuple;
10+
11+
public DisposableTuple(T1 item1, T2 item2)
12+
{
13+
_tuple = new Tuple<T1, T2>(item1, item2);
14+
}
15+
16+
public T1 Item1 { get { return _tuple.Item1; } }
17+
public T2 Item2 { get { return _tuple.Item2; } }
18+
19+
public void Dispose()
20+
{
21+
if (_tuple == null)
22+
{
23+
return;
24+
}
25+
26+
_tuple.Item1.SafeDispose();
27+
_tuple.Item2.SafeDispose();
28+
_tuple = null;
29+
30+
GC.SuppressFinalize(this);
31+
}
32+
}
33+
}

LibGit2Sharp/Core/Proxy.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,7 @@ public static void git_revert(
25712571

25722572
#region git_revparse_
25732573

2574-
public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(RepositorySafeHandle repo, string objectish)
2574+
public static DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(RepositorySafeHandle repo, string objectish)
25752575
{
25762576
using (ThreadAffinity())
25772577
{
@@ -2592,24 +2592,10 @@ public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(R
25922592
break;
25932593
}
25942594

2595-
return new Tuple<GitObjectSafeHandle, ReferenceSafeHandle>(obj, reference);
2595+
return new DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle>(obj, reference);
25962596
}
25972597
}
25982598

2599-
public static GitObjectSafeHandle git_revparse_single(RepositorySafeHandle repo, string objectish)
2600-
{
2601-
var handles = git_revparse_ext(repo, objectish);
2602-
2603-
if (handles == null)
2604-
{
2605-
return null;
2606-
}
2607-
2608-
handles.Item2.Dispose();
2609-
2610-
return handles.Item1;
2611-
}
2612-
26132599
#endregion
26142600

26152601
#region git_revwalk_

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="CommitSortStrategies.cs" />
7070
<Compile Include="CompareOptions.cs" />
7171
<Compile Include="Core\FileHistory.cs" />
72+
<Compile Include="Core\DisposableTuple.cs" />
7273
<Compile Include="Core\Platform.cs" />
7374
<Compile Include="Core\Handles\ConflictIteratorSafeHandle.cs" />
7475
<Compile Include="DescribeOptions.cs" />

LibGit2Sharp/Repository.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lo
485485
Ensure.ArgumentNotNullOrEmptyString(objectish, "objectish");
486486

487487
GitObject obj;
488-
using (GitObjectSafeHandle sh = Proxy.git_revparse_single(handle, objectish))
488+
using (DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle> handles = Proxy.git_revparse_ext(handle, objectish))
489489
{
490-
if (sh == null)
490+
if (handles == null)
491491
{
492492
if (lookUpOptions.HasFlag(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
493493
{
@@ -497,14 +497,15 @@ internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lo
497497
return null;
498498
}
499499

500-
GitObjectType objType = Proxy.git_object_type(sh);
500+
var objH = handles.Item1;
501+
GitObjectType objType = Proxy.git_object_type(objH);
501502

502503
if (type != GitObjectType.Any && objType != type)
503504
{
504505
return null;
505506
}
506507

507-
obj = GitObject.BuildFrom(this, Proxy.git_object_id(sh), objType, PathFromRevparseSpec(objectish));
508+
obj = GitObject.BuildFrom(this, Proxy.git_object_id(objH), objType, PathFromRevparseSpec(objectish));
508509
}
509510

510511
if (lookUpOptions.HasFlag(LookUpOptions.DereferenceResultToCommit))
@@ -752,17 +753,18 @@ public Branch Checkout(string committishOrBranchSpec, CheckoutOptions options)
752753
Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec");
753754
Ensure.ArgumentNotNull(options, "options");
754755

755-
var handles = Proxy.git_revparse_ext(Handle, committishOrBranchSpec);
756-
if (handles == null)
757-
{
758-
Ensure.GitObjectIsNotNull(null, committishOrBranchSpec);
759-
}
760-
761-
var objH = handles.Item1;
762-
var refH = handles.Item2;
763756
GitObject obj;
764-
try
757+
758+
using (DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle> handles = Proxy.git_revparse_ext(Handle, committishOrBranchSpec))
765759
{
760+
if (handles == null)
761+
{
762+
Ensure.GitObjectIsNotNull(null, committishOrBranchSpec);
763+
}
764+
765+
var objH = handles.Item1;
766+
var refH = handles.Item2;
767+
766768
if (!refH.IsInvalid)
767769
{
768770
var reference = Reference.BuildFromPtr<Reference>(refH, this);
@@ -776,11 +778,6 @@ public Branch Checkout(string committishOrBranchSpec, CheckoutOptions options)
776778
obj = GitObject.BuildFrom(this, Proxy.git_object_id(objH), Proxy.git_object_type(objH),
777779
PathFromRevparseSpec(committishOrBranchSpec));
778780
}
779-
finally
780-
{
781-
objH.Dispose();
782-
refH.Dispose();
783-
}
784781

785782
Commit commit = obj.DereferenceToCommit(true);
786783
Checkout(commit.Tree, options, committishOrBranchSpec);

0 commit comments

Comments
 (0)
0