8000 Make all DiffResults disposable, move ownership of diff handle · rmunn/libgit2sharp@1062426 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1062426

Browse files
committed
Make all DiffResults disposable, move ownership of diff handle
This will let us perform lazy loading of details in these classes, just as we do with commits and some other classes.
1 parent 68d9be9 commit 1062426

File tree

5 files changed

+111
-25
lines changed

5 files changed

+111
-25
lines changed

LibGit2Sharp/Diff.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,17 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
241241
}
242242
}
243243

244-
using (DiffHandle diff = BuildDiffList(oldTreeId, newTreeId, comparer, diffOptions, paths, explicitPathsOptions, compareOptions))
244+
DiffHandle diff = BuildDiffList(oldTreeId, newTreeId, comparer, diffOptions, paths, explicitPathsOptions, compareOptions);
245+
246+
try
245247
{
246248
return BuildDiffResult<T>(diff);
247249
}
250+
catch
251+
{
252+
diff.SafeDispose();
253+
throw;
254+
}
248255
}
249256

250257
/// <summary>
@@ -343,10 +350,17 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
343350
}
344351
}
345352

346-
using (DiffHandle diff = BuildDiffList(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions))
353+
DiffHandle diff = BuildDiffList(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions);
354+
355+
try
347356
{
348357
return BuildDiffResult<T>(diff);
349358
}
359+
catch
360+
{
361+
diff.SafeDispose();
362+
throw;
363+
}
350364
}
351365

352366
/// <summary>
@@ -462,10 +476,17 @@ internal virtual T Compare<T>(
462476
}
463477
}
464478

465-
using (DiffHandle diff = BuildDiffList(null, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions))
479+
DiffHandle diff = BuildDiffList(null, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions);
480+
481+
try
466482
{
467483
return BuildDiffResult<T>(diff);
468484
}
485+
catch
486+
{
487+
diff.SafeDispose();
488+
throw;
489+
}
469490
}
470491

471492
internal delegate DiffHandle TreeComparisonHandleRetriever(ObjectId oldTreeId, ObjectId newTreeId, GitDiffOptions options);

LibGit2Sharp/IDiffResult.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
namespace LibGit2Sharp
1+
using System;
2+
3+
namespace LibGit2Sharp
24
{
35
/// <summary>
46
/// Marker interface to identify Diff results.
57
/// </summary>
6-
public interface IDiffResult
8+
public interface IDiffResult: IDisposable
79
{ }
810
}

LibGit2Sharp/Patch.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ protected Patch()
3232

3333
internal unsafe Patch(DiffHandle diff)
3434
{
35-
int count = Proxy.git_diff_num_deltas(diff);
36-
for (int i = 0; i < count; i++)
35+
using (diff)
3736
{
38-
using (var patch = Proxy.git_patch_from_diff(diff, i))
37+
int count = Proxy.git_diff_num_deltas(diff);
38+
for (int i = 0; i < count; i++)
3939
{
40-
var delta = Proxy.git_diff_get_delta(diff, i);
41-
AddFileChange(delta);
42-
Proxy.git_patch_print(patch, PrintCallBack);
40+
using (var patch = Proxy.git_patch_from_diff(diff, i))
41+
{
42+
var delta = Proxy.git_diff_get_delta(diff, i);
43+
AddFileChange(delta);
44+
Proxy.git_patch_print(patch, PrintCallBack);
45+
}
4346
}
4447
}
4548
}
@@ -180,5 +183,24 @@ private string DebuggerDisplay
180183
linesDeleted);
181184
}
182185
}
186+
187+
/// <summary>
188+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
189+
/// </summary>
190+
public void Dispose()
191+
{
192+
Dispose(true);
193+
GC.SuppressFinalize(this);
194+
}
195+
196+
/// <summary>
197+
/// Releases unmanaged and - optionally - managed resources.
198+
/// </summary>
199+
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
200+
protected virtual void Dispose(bool disposing)
201+
{
202+
// This doesn't do anything yet because it loads everything
203+
// eagerly and disposes of the diff handle in the constructor.
204+
}
183205
}
184206
}

LibGit2Sharp/PatchStats.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,25 @@ protected PatchStats()
2727

2828
internal unsafe PatchStats(DiffHandle diff)
2929
{
30-
int count = Proxy.git_diff_num_deltas(diff);
31-
for (int i = 0; i < count; i++)
30+
using (diff)
3231
{
33-
using (var patch = Proxy.git_patch_from_diff(diff, i))
32+
int count = Proxy.git_diff_num_deltas(diff);
33+
for (int i = 0; i < count; i++)
3434
{
35-
var delta = Proxy.git_diff_get_delta(diff, i);
36-
var pathPtr = delta->new_file.Path != null ? delta->new_file.Path : delta->old_file.Path;
37-
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);
35+
using (var patch = Proxy.git_patch_from_diff(diff, i))
36+
{
37+
var delta = Proxy.git_diff_get_delta(diff, i);
38+
var pathPtr = delta->new_file.Path != null ? delta->new_file.Path : delta->old_file.Path;
39+
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);
3840

39-
var stats = Proxy.git_patch_line_stats(patch);
40-
int added = stats.Item1;
41-
int deleted = stats.Item2;
42-
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
43-
totalLinesAdded += added;
44-
totalLinesDeleted += deleted;
41+
var stats = Proxy.git_patch_line_stats(patch);
42+
int added = stats.Item1;
43+
int deleted = stats.Item2;
44+
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
45+
totalLinesAdded += added;
46+
totalLinesDeleted += deleted;
47+
}
4548
}
46-
4749
}
4850
}
4951

@@ -117,5 +119,24 @@ private string DebuggerDisplay
117119
TotalLinesDeleted);
118120
}
119121
}
122+
123+
/// <summary>
124+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
125+
/// </summary>
126+
public void Dispose()
127+
{
128+
Dispose(true);
129+
GC.SuppressFinalize(this);
130+
}
131+
132+
/// <summary>
133+
/// Releases unmanaged and - optionally - managed resources.
134+
/// </summary>
135+
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
136+
protected virtual void Dispose(bool disposing)
137+
{
138+
// This doesn't do anything yet because it loads everything
139+
// eagerly and disposes of the diff handle in the constructor.
140+
}
120141
}
121142
}

LibGit2Sharp/TreeChanges.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ protected TreeChanges()
5252

5353
internal unsafe TreeChanges(DiffHandle diff)
5454
{
55-
Proxy.git_diff_foreach(diff, FileCallback, null, null);
55+
using(diff)
56+
Proxy.git_diff_foreach(diff, FileCallback, null, null);
5657
}
5758

5859
private unsafe int FileCallback(git_diff_delta* delta, float progress, IntPtr payload)
@@ -169,5 +170,24 @@ private string DebuggerDisplay
169170
Copied.Count());
170171
}
171172
}
173+
174+
/// <summary>
175+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
176+
/// </summary>
177+
public void Dispose()
178+
{
179+
Dispose(true);
180+
GC.SuppressFinalize(this);
181+
}
182+
183+
/// <summary>
184+
/// Releases unmanaged and - optionally - managed resources.
185+
/// </summary>
186+
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
187+
protected virtual void Dispose(bool disposing)
188+
{
189+
// This doesn't do anything yet because it loads everything
190+
// eagerly and disposes of the diff handle in the constructor.
191+
}
172192
}
173193
}

0 commit comments

Comments
 (0)
0