Skip to content

Commit 580dee6

Browse files
committed
Simplify disposal of some resources
1 parent 8384e7e commit 580dee6

File tree

6 files changed

+24
-67
lines changed

6 files changed

+24
-67
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace LibGit2Sharp.Core.Handles
4+
{
5+
internal static class DisposableExtensions
6+
{
7+
public static void SafeDispose(this IDisposable disposable)
8+
{
9+
if (disposable == null)
10+
{
11+
return;
12+
}
13+
14+
disposable.Dispose();
15+
}
16+
}
17+
}

LibGit2Sharp/Core/Handles/SafeHandleExtensions.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

LibGit2Sharp/Core/Proxy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,8 +2965,7 @@ private static IEnumerable<TResult> git_iterator_next<TIterator, THandle, TPaylo
29652965
}
29662966
finally
29672967
{
2968-
if (next != null)
2969-
next.SafeDispose();
2968+
next.SafeDispose();
29702969
}
29712970
}
29722971
}

LibGit2Sharp/Diff.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,23 +292,12 @@ private static TreeComparisonHandleRetriever WorkdirAndIndexToTree(Repository re
292292
{
293293
TreeComparisonHandleRetriever comparisonHandleRetriever = (oh, nh, o) =>
294294
{
295-
DiffSafeHandle diff = null, diff2 = null;
295+
DiffSafeHandle diff = Proxy.git_diff_tree_to_index(repo.Handle, repo.Index.Handle, oh, o);
296296

297-
try
297+
using (DiffSafeHandle diff2 = Proxy.git_diff_index_to_workdir(repo.Handle, repo.Index.Handle, o))
298298
{
299-
diff = Proxy.git_diff_tree_to_index(repo.Handle, repo.Index.Handle, oh, o);
300-
diff2 = Proxy.git_diff_index_to_workdir(repo.Handle, repo.Index.Handle, o);
301299
Proxy.git_diff_merge(diff, diff2);
302300
}
303-
catch
304-
{
305-
diff.SafeDispose();
306-
throw;
307-
}
308-
finally
309-
{
310-
diff2.SafeDispose();
311-
}
312301

313302
return diff;
314303
};
@@ -332,17 +321,9 @@ private DiffSafeHandle BuildDiffList(ObjectId oldTreeId, ObjectId newTreeId, Tre
332321
{
333322
var diffList = comparisonHandleRetriever(oldTreeId, newTreeId, options);
334323

335-
try
336-
{
337-
if (explicitPathsOptions != null)
338-
{
339-
DispatchUnmatchedPaths(explicitPathsOptions, filePaths, matchedPaths);
340-
}
341-
}
342-
catch
324+
if (explicitPathsOptions != null)
343325
{
344-
diffList.Dispose();
345-
throw;
326+
DispatchUnmatchedPaths(explicitPathsOptions, filePaths, matchedPaths);
346327
}
347328

348329
DetectRenames(diffList, compareOptions);

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<Compile Include="Core\GitCheckoutOptsWrapper.cs" />
8484
<Compile Include="Core\GitCredentialType.cs" />
8585
<Compile Include="Core\GitRevertOpts.cs" />
86+
<Compile Include="Core\Handles\DisposableExtensions.cs" />
8687
<Compile Include="Core\Handles\PatchSafeHandle.cs" />
8788
<Compile Include="Core\IntPtrExtensions.cs" />
8889
<Compile Include="DefaultCredentials.cs" />
@@ -258,7 +259,6 @@
258259
<Compile Include="FileStatus.cs" />
259260
<Compile Include="Core\GitTime.cs" />
260261
<Compile Include="Core\NativeMethods.cs" />
261-
<Compile Include="Core\Handles\SafeHandleExtensions.cs" />
262262
<Compile Include="Core\ObjectSafeWrapper.cs" />
263263
<Compile Include="Core\Handles\RemoteSafeHandle.cs" />
264264
<Compile Include="Core\Handles\RevWalkerSafeHandle.cs">

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,23 +440,15 @@ internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath know
440440
{
441441
Ensure.ArgumentNotNull(id, "id");
442442

443-
GitObjectSafeHandle obj = null;
444-
445-
try
443+
using (GitObjectSafeHandle obj = Proxy.git_object_lookup(handle, id, type))
446444
{
447-
obj = Proxy.git_object_lookup(handle, id, type);
448-
449445
if (obj == null)
450446
{
451447
return null;
452448
}
453449

454450
return GitObject.BuildFrom(this, id, Proxy.git_object_type(obj), knownPath);
455451
}
456-
finally
457-
{
458-
obj.SafeDispose();
459-
}
460452
}
461453

462454
private static string PathFromRevparseSpec(string spec)

0 commit comments

Comments
 (0)