forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteCallbacks.cs
More file actions
131 lines (110 loc) · 4.28 KB
/
RemoteCallbacks.cs
File metadata and controls
131 lines (110 loc) · 4.28 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;
namespace LibGit2Sharp
{
/// <summary>
/// Class to translate libgit2 callbacks into delegates exposed by LibGit2Sharp.
/// Handles generating libgit2 git_remote_callbacks datastructure given a set
/// of LibGit2Sharp delegates and handles propagating libgit2 callbacks into
/// corresponding LibGit2Sharp exposed delegates.
/// </summary>
internal class RemoteCallbacks
{
internal RemoteCallbacks(ProgressHandler onProgress = null, CompletionHandler onCompletion = null, UpdateTipsHandler onUpdateTips = null)
{
Progress = onProgress;
Completion = onCompletion;
UpdateTips = onUpdateTips;
}
#region Delegates
/// <summary>
/// Progress callback. Corresponds to libgit2 progress callback.
/// </summary>
private readonly ProgressHandler Progress;
/// <summary>
/// UpdateTips callback. Corresponds to libgit2 update_tips callback.
/// </summary>
private readonly UpdateTipsHandler UpdateTips;
/// <summary>
/// Completion callback. Corresponds to libgit2 Completion callback.
/// </summary>
private readonly CompletionHandler Completion;
#endregion
internal GitRemoteCallbacks GenerateCallbacks()
{
GitRemoteCallbacks callbacks = new GitRemoteCallbacks {version = 1};
if (Progress != null)
{
callbacks.progress = GitProgressHandler;
}
if (UpdateTips != null)
{
callbacks.update_tips = GitUpdateTipsHandler;
}
if (Completion != null)
{
callbacks.completion = GitCompletionHandler;
}
return callbacks;
}
#region Handlers to respond to callbacks raised by libgit2
/// <summary>
/// Handler for libgit2 Progress callback. Converts values
/// received from libgit2 callback to more suitable types
/// and calls delegate provided by LibGit2Sharp consumer.
/// </summary>
/// <param name="str">IntPtr to string from libgit2</param>
/// <param name="len">length of string</param>
/// <param name="data"></param>
private void GitProgressHandler(IntPtr str, int len, IntPtr data)
{
ProgressHandler onProgress = Progress;
if (onProgress != null)
{
string message = Utf8Marshaler.FromNative(str, len);
onProgress(message);
}
}
/// <summary>
/// Handler for libgit2 update_tips callback. Converts values
/// received from libgit2 callback to more suitable types
/// and calls delegate provided by LibGit2Sharp consumer.
/// </summary>
/// <param name="str">IntPtr to string</param>
/// <param name="oldId">Old reference ID</param>
/// <param name="newId">New referene ID</param>
/// <param name="data"></param>
/// <returns></returns>
private int GitUpdateTipsHandler(IntPtr str, ref GitOid oldId, ref GitOid newId, IntPtr data)
{
UpdateTipsHandler onUpdateTips = UpdateTips;
int result = 0;
if (onUpdateTips != null)
{
string refName = Utf8Marshaler.FromNative(str);
result = onUpdateTips(refName, oldId, newId);
}
return result;
}
/// <summary>
/// Handler for libgit2 completion callback. Converts values
/// received from libgit2 callback to more suitable types
/// and calls delegate provided by LibGit2Sharp consumer.
/// </summary>
/// <param name="remoteCompletionType"></param>
/// <param name="data"></param>
/// <returns></returns>
private int GitCompletionHandler(RemoteCompletionType remoteCompletionType, IntPtr data)
{
CompletionHandler completion = Completion;
int result = 0;
if (completion != null)
{
result = completion(remoteCompletionType);
}
return result;
}
#endregion
}
}