-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathFilterRegistration.cs
86 lines (77 loc) · 2.9 KB
/
FilterRegistration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// An object representing the registration of a Filter type with libgit2
/// </summary>
public sealed class FilterRegistration
{
/// <summary>
/// Maximum priority value a filter can have. A value of 200 will be run last on checkout and first on checkin.
/// </summary>
public const int FilterPriorityMax = 200;
/// <summary>
/// Minimum priority value a filter can have. A value of 0 will be run first on checkout and last on checkin.
/// </summary>
public const int FilterPriorityMin = 0;
/// <summary>
///
/// </summary>
/// <param name="filter"></param>
/// <param name="priority"></param>
internal FilterRegistration(Filter filter, int priority)
{
System.Diagnostics.Debug.Assert(filter != null);
System.Diagnostics.Debug.Assert(priority >= FilterPriorityMin && priority <= FilterPriorityMax);
Filter = filter;
Priority = priority;
// marshal the git_filter strucutre into native memory
FilterPointer = Marshal.AllocHGlobal(Marshal.SizeOf(filter.GitFilter));
Marshal.StructureToPtr(filter.GitFilter, FilterPointer, false);
// register the filter with the native libary
Proxy.git_filter_register(filter.Name, FilterPointer, priority);
}
/// <summary>
/// Finalizer called by the <see cref="GC"/>, deregisters and frees native memory associated with the registered filter in libgit2.
/// </summary>
~FilterRegistration()
{
// deregister the filter
GlobalSettings.DeregisterFilter(this);
// clean up native allocations
Free();
}
/// <summary>
/// Gets if the registration and underlying filter are valid.
/// </summary>
public bool IsValid { get { return !freed; } }
/// <summary>
/// The registerd filters
/// </summary>
public readonly Filter Filter;
/// <summary>
/// The name of the filter in the libgit2 registry
/// </summary>
public string Name { get { return Filter.Name; } }
/// <summary>
/// The priority of the registered filter
/// </summary>
public readonly int Priority;
private readonly IntPtr FilterPointer;
private bool freed;
internal void Free()
{
if (!freed)
{
// unregister the filter with the native libary
Proxy.git_filter_unregister(Filter.Name);
// release native memory
Marshal.FreeHGlobal(FilterPointer);
// remember to not do this twice
freed = true;
}
}
}
}