Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions LibGit2Sharp/Core/DisposableTuple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp.Core
{
internal class DisposableTuple<T1, T2> : IDisposable
where T1 : IDisposable where T2 : IDisposable
{
private Tuple<T1, T2> _tuple;

public DisposableTuple(T1 item1, T2 item2)
{
_tuple = new Tuple<T1, T2>(item1, item2);
}

public T1 Item1 { get { return _tuple.Item1; } }
public T2 Item2 { get { return _tuple.Item2; } }

public void Dispose()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call GC.SuppressFinalize() at the end of this method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Indeed. Thanks for this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think I was mistaken here - I don't think this is needed.

From Object.Finalize documentation:

The Object class provides no implementation for the Finalize method, and the garbage collector does not mark types derived from Object for finalization unless they override the Finalize method.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
if (_tuple == null)
{
return;
}

_tuple.Item1.SafeDispose();
_tuple.Item2.SafeDispose();
_tuple = null;

GC.SuppressFinalize(this);
}
}
}
18 changes: 2 additions & 16 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ public static void git_revert(

#region git_revparse_

public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(RepositorySafeHandle repo, string objectish)
public static DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(RepositorySafeHandle repo, string objectish)
{
using (ThreadAffinity())
{
Expand All @@ -2592,24 +2592,10 @@ public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(R
break;
}

return new Tuple<GitObjectSafeHandle, ReferenceSafeHandle>(obj, reference);
return new DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle>(obj, reference);
}
}

public static GitObjectSafeHandle git_revparse_single(RepositorySafeHandle repo, string objectish)
{
var handles = git_revparse_ext(repo, objectish);

if (handles == null)
{
return null;
}

handles.Item2.Dispose();

return handles.Item1;
}

#endregion

#region git_revwalk_
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="CommitSortStrategies.cs" />
<Compile Include="CompareOptions.cs" />
<Compile Include="Core\FileHistory.cs" />
<Compile Include="Core\DisposableTuple.cs" />
<Compile Include="Core\Platform.cs" />
<Compile Include="Core\Handles\ConflictIteratorSafeHandle.cs" />
<Compile Include="DescribeOptions.cs" />
Expand Down
33 changes: 15 additions & 18 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lo
Ensure.ArgumentNotNullOrEmptyString(objectish, "objectish");

GitObject obj;
using (GitObjectSafeHandle sh = Proxy.git_revparse_single(handle, objectish))
using (DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle> handles = Proxy.git_revparse_ext(handle, objectish))
{
if (sh == null)
if (handles == null)
{
if (lookUpOptions.HasFlag(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
{
Expand All @@ -497,14 +497,15 @@ internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lo
return null;
}

GitObjectType objType = Proxy.git_object_type(sh);
var objH = handles.Item1;
GitObjectType objType = Proxy.git_object_type(objH);

if (type != GitObjectType.Any && objType != type)
{
return null;
}

obj = GitObject.BuildFrom(this, Proxy.git_object_id(sh), objType, PathFromRevparseSpec(objectish));
obj = GitObject.BuildFrom(this, Proxy.git_object_id(objH), objType, PathFromRevparseSpec(objectish));
}

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

var handles = Proxy.git_revparse_ext(Handle, committishOrBranchSpec);
if (handles == null)
{
Ensure.GitObjectIsNotNull(null, committishOrBranchSpec);
}

var objH = handles.Item1;
var refH = handles.Item2;
GitObject obj;
try

using (DisposableTuple<GitObjectSafeHandle, ReferenceSafeHandle> handles = Proxy.git_revparse_ext(Handle, committishOrBranchSpec))
{
if (handles == null)
{
Ensure.GitObjectIsNotNull(null, committishOrBranchSpec);
}

var objH = handles.Item1;
var refH = handles.Item2;

if (!refH.IsInvalid)
{
var reference = Reference.BuildFromPtr<Reference>(refH, this);
Expand All @@ -776,11 +778,6 @@ public Branch Checkout(string committishOrBranchSpec, CheckoutOptions options)
obj = GitObject.BuildFrom(this, Proxy.git_object_id(objH), Proxy.git_object_type(objH),
PathFromRevparseSpec(committishOrBranchSpec));
}
finally
{
objH.Dispose();
refH.Dispose();
}

Commit commit = obj.DereferenceToCommit(true);
Checkout(commit.Tree, options, committishOrBranchSpec);
Expand Down