Skip to content

Commit 3203126

Browse files
committed
Clean up custom callbacks
1 parent 269d2c8 commit 3203126

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

LibGit2Sharp.Tests/FilterFixture.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,31 @@ public override void Dispose()
203203
base.Dispose();
204204
}
205205

206+
[Fact]
207+
public void CleanUpIsCalledAfterStage()
208+
{
209+
bool called = false;
210+
211+
Action cleanUpCallback = () =>
212+
{
213+
called = true;
214+
};
215+
216+
string repoPath = InitNewRepository();
217+
var callbacks = new FilterCallbacks(() => 0, ()=> 0, () => {}, ()=>0, cleanUpCallback);
218+
var test = new Filter("test-filter55", "filter", 1, callbacks);
219+
using (var repo = new Repository(repoPath))
220+
{
221+
test.Register();
222+
223+
StageNewFile(repo, 44);
224+
}
225+
226+
test.Deregister();
227+
Assert.True(called);
228+
}
229+
230+
206231
[Fact]
207232
public void ShutdownCallbackNotMadeWhenFilterNeverUsed()
208233
{

LibGit2Sharp/Core/GitFilter.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ internal class GitFilter
1313

1414
public IntPtr attributes;
1515

16-
//public IntPtr init;
17-
18-
//public IntPtr shutdown;
19-
20-
//public IntPtr check;
21-
22-
//public IntPtr apply;
23-
24-
//public IntPtr cleanup;
25-
2616
[MarshalAs(UnmanagedType.FunctionPtr)]
2717
public git_filter_init_fn init;
2818

LibGit2Sharp/Filter.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Runtime.InteropServices;
32
using System.Text;
43
using LibGit2Sharp.Core;
54
using LibGit2Sharp.Core.Handles;
@@ -190,6 +189,7 @@ private int ApplyCallback(IntPtr gitFilter, IntPtr payload, IntPtr gitBufferTo,
190189
private void CleanUpCallback(IntPtr gitFilter, IntPtr payload)
191190
{
192191
Console.WriteLine("Cleanup");
192+
filterCallbacks.CustomCleanUpCallback();
193193
}
194194

195195
/// <summary>
@@ -270,6 +270,7 @@ public class FilterCallbacks
270270
private readonly Func<int> customApplyCallback;
271271
private readonly Func<int> customInitializeCallback;
272272
private readonly Action customShutdownCallback;
273+
private Action customCleanUpCallback;
273274

274275
private readonly Func<int> passThroughFunc = () => (int) GitErrorCode.PassThrough;
275276
private readonly Func<int> passThroughSuccess = () => 0;
@@ -285,12 +286,14 @@ public FilterCallbacks(
285286
Func<int> customCheckCallback = null,
286287
Func<int> customApplyCallback = null,
287288
Action customShutdownCallback = null,
288-
Func<int> customInitializeCallback = null)
289+
Func<int> customInitializeCallback = null,
290+
Action customCleanupCallback = null)
289291
{
290292
this.customCheckCallback = customCheckCallback ?? passThroughFunc;
291293
this.customApplyCallback = customApplyCallback ?? passThroughFunc;
292294
this.customShutdownCallback = customShutdownCallback ?? (() => { });
293295
this.customInitializeCallback = customInitializeCallback ?? passThroughSuccess;
296+
this.customCleanUpCallback = customCleanupCallback ?? (() => { });
294297
}
295298

296299
/// <summary>
@@ -349,6 +352,18 @@ public Action CustomShutdownCallback
349352
return customShutdownCallback;
350353
}
351354
}
355+
/// <summary>
356+
/// Callback to clean up after filtering has been applied. Specified as `filter.cleanup`, this is an optional callback invoked
357+
/// after the filter has been applied. If the `check` or `apply` callbacks allocated a `payload`
358+
/// to keep per-source filter state, use this callback to free that payload and release resources as required.
359+
/// </summary>
360+
public Action CustomCleanUpCallback
361+
{
362+
get
363+
{
364+
return customCleanUpCallback;
365+
}
366+
}
352367

353368
/// <summary>
354369
/// Initialize callback on filter

0 commit comments

Comments
 (0)