From b6c30fc62188554cce9fb352ce226dd59e59233a Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 28 Mar 2015 18:12:53 +0100 Subject: [PATCH] Simplify git_revparse_ext usage --- LibGit2Sharp/Core/DisposableTuple.cs | 33 ++++++++++++++++++++++++++++ LibGit2Sharp/Core/Proxy.cs | 18 ++------------- LibGit2Sharp/LibGit2Sharp.csproj | 1 + LibGit2Sharp/Repository.cs | 33 +++++++++++++--------------- 4 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 LibGit2Sharp/Core/DisposableTuple.cs diff --git a/LibGit2Sharp/Core/DisposableTuple.cs b/LibGit2Sharp/Core/DisposableTuple.cs new file mode 100644 index 000000000..637a8717d --- /dev/null +++ b/LibGit2Sharp/Core/DisposableTuple.cs @@ -0,0 +1,33 @@ +using System; +using LibGit2Sharp.Core.Handles; + +namespace LibGit2Sharp.Core +{ + internal class DisposableTuple : IDisposable + where T1 : IDisposable where T2 : IDisposable + { + private Tuple _tuple; + + public DisposableTuple(T1 item1, T2 item2) + { + _tuple = new Tuple(item1, item2); + } + + public T1 Item1 { get { return _tuple.Item1; } } + public T2 Item2 { get { return _tuple.Item2; } } + + public void Dispose() + { + if (_tuple == null) + { + return; + } + + _tuple.Item1.SafeDispose(); + _tuple.Item2.SafeDispose(); + _tuple = null; + + GC.SuppressFinalize(this); + } + } +} diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 2ba1be499..ebacf0f21 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -2571,7 +2571,7 @@ public static void git_revert( #region git_revparse_ - public static Tuple git_revparse_ext(RepositorySafeHandle repo, string objectish) + public static DisposableTuple git_revparse_ext(RepositorySafeHandle repo, string objectish) { using (ThreadAffinity()) { @@ -2592,24 +2592,10 @@ public static Tuple git_revparse_ext(R break; } - return new Tuple(obj, reference); + return new DisposableTuple(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_ diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 60b352522..2ff3b9a38 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -69,6 +69,7 @@ + diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index f7c3b5379..67f2ff3af 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -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 handles = Proxy.git_revparse_ext(handle, objectish)) { - if (sh == null) + if (handles == null) { if (lookUpOptions.HasFlag(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound)) { @@ -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)) @@ -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 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(refH, this); @@ -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);