-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathRemoteCallbacks.cs
377 lines (314 loc) · 13.3 KB
/
RemoteCallbacks.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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(CredentialsHandler credentialsProvider)
{
CredentialsProvider = credentialsProvider;
}
internal RemoteCallbacks(PushOptions pushOptions)
{
if (pushOptions == null)
{
return;
}
PushTransferProgress = pushOptions.OnPushTransferProgress;
PackBuilderProgress = pushOptions.OnPackBuilderProgress;
CredentialsProvider = pushOptions.CredentialsProvider;
CertificateCheck = pushOptions.CertificateCheck;
PushStatusError = pushOptions.OnPushStatusError;
PrePushCallback = pushOptions.OnNegotiationCompletedBeforePush;
}
internal RemoteCallbacks(FetchOptionsBase fetchOptions)
{
if (fetchOptions == null)
{
return;
}
Progress = fetchOptions.OnProgress;
DownloadTransferProgress = fetchOptions.OnTransferProgress;
UpdateTips = fetchOptions.OnUpdateTips;
CredentialsProvider = fetchOptions.CredentialsProvider;
CertificateCheck = fetchOptions.CertificateCheck;
}
#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>
/// PushStatusError callback. It will be called when the libgit2 push_update_reference returns a non null status message,
/// which means that the update was rejected by the remote server.
/// </summary>
private readonly PushStatusErrorHandler PushStatusError;
/// <summary>
/// Managed delegate to be called in response to a git_transfer_progress_callback callback from libgit2.
/// This will in turn call the user provided delegate.
/// </summary>
private readonly TransferProgressHandler DownloadTransferProgress;
/// <summary>
/// Push transfer progress callback.
/// </summary>
private readonly PushTransferProgressHandler PushTransferProgress;
/// <summary>
/// Pack builder creation progress callback.
/// </summary>
private readonly PackBuilderProgressHandler PackBuilderProgress;
/// <summary>
/// Called during remote push operation after negotiation, before upload
/// </summary>
private readonly PrePushHandler PrePushCallback;
#endregion
/// <summary>
/// The credentials to use for authentication.
/// </summary>
private readonly CredentialsHandler CredentialsProvider;
/// <summary>
/// Callback to perform validation on the certificate
/// </summary>
private readonly CertificateCheckHandler CertificateCheck;
internal unsafe GitRemoteCallbacks GenerateCallbacks()
{
var callbacks = new GitRemoteCallbacks { version = 1 };
if (Progress != null)
{
callbacks.progress = GitProgressHandler;
}
if (UpdateTips != null)
{
callbacks.update_tips = GitUpdateTipsHandler;
}
if (PushStatusError != null)
{
callbacks.push_update_reference = GitPushUpdateReference;
}
if (CredentialsProvider != null)
{
callbacks.acquire_credentials = GitCredentialHandler;
}
if (CertificateCheck != null)
{
callbacks.certificate_check = GitCertificateCheck;
}
if (DownloadTransferProgress != null)
{
callbacks.download_progress = GitDownloadTransferProgressHandler;
}
if (PushTransferProgress != null)
{
callbacks.push_transfer_progress = GitPushTransferProgressHandler;
}
if (PackBuilderProgress != null)
{
callbacks.pack_progress = GitPackbuilderProgressHandler;
}
if (PrePushCallback != null)
{
callbacks.push_negotiation = GitPushNegotiationHandler;
}
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">IntPtr to optional payload passed back to the callback.</param>
/// <returns>0 on success; a negative value to abort the process.</returns>
private int GitProgressHandler(IntPtr str, int len, IntPtr data)
{
ProgressHandler onProgress = Progress;
bool shouldContinue = true;
if (onProgress != null)
{
string message = LaxUtf8Marshaler.FromNative(str, len);
shouldContinue = onProgress(message);
}
return Proxy.ConvertResultToCancelFlag(shouldContinue);
}
/// <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">IntPtr to optional payload passed back to the callback.</param>
/// <returns>0 on success; a negative value to abort the process.</returns>
private int GitUpdateTipsHandler(IntPtr str, ref GitOid oldId, ref GitOid newId, IntPtr data)
{
UpdateTipsHandler onUpdateTips = UpdateTips;
bool shouldContinue = true;
if (onUpdateTips != null)
{
string refName = LaxUtf8Marshaler.FromNative(str);
shouldContinue = onUpdateTips(refName, oldId, newId);
}
return Proxy.ConvertResultToCancelFlag(shouldContinue);
}
/// <summary>
/// The delegate with the signature that matches the native push_update_reference function's signature
/// </summary>
/// <param name="str">IntPtr to string, the name of the reference</param>
/// <param name="status">IntPtr to string, the update status message</param>
/// <param name="data">IntPtr to optional payload passed back to the callback.</param>
/// <returns>0 on success; a negative value to abort the process.</returns>
private int GitPushUpdateReference(IntPtr str, IntPtr status, IntPtr data)
{
PushStatusErrorHandler onPushError = PushStatusError;
if (onPushError != null)
{
string reference = LaxUtf8Marshaler.FromNative(str);
string message = LaxUtf8Marshaler.FromNative(status);
if (message != null)
{
onPushError(new PushStatusError(reference, message));
}
}
return Proxy.ConvertResultToCancelFlag(true);
}
/// <summary>
/// The delegate with the signature that matches the native git_transfer_progress_callback function's signature.
/// </summary>
/// <param name="progress"><see cref="GitTransferProgress"/> structure containing progress information.</param>
/// <param name="payload">Payload data.</param>
/// <returns>the result of the wrapped <see cref="TransferProgressHandler"/></returns>
private int GitDownloadTransferProgressHandler(ref GitTransferProgress progress, IntPtr payload)
{
bool shouldContinue = true;
if (DownloadTransferProgress != null)
{
shouldContinue = DownloadTransferProgress(new TransferProgress(progress));
}
return Proxy.ConvertResultToCancelFlag(shouldContinue);
}
private int GitPushTransferProgressHandler(uint current, uint total, UIntPtr bytes, IntPtr payload)
{
bool shouldContinue = true;
if (PushTransferProgress != null)
{
shouldContinue = PushTransferProgress((int)current, (int)total, (long)bytes);
}
return Proxy.ConvertResultToCancelFlag(shouldContinue);
}
private int GitPackbuilderProgressHandler(int stage, uint current, uint total, IntPtr payload)
{
bool shouldContinue = true;
if (PackBuilderProgress != null)
{
shouldContinue = PackBuilderProgress((PackBuilderStage)stage, (int)current, (int)total);
}
return Proxy.ConvertResultToCancelFlag(shouldContinue);
}
private int GitCredentialHandler(
out IntPtr ptr,
IntPtr cUrl,
IntPtr usernameFromUrl,
GitCredentialType credTypes,
IntPtr payload)
{
string url = LaxUtf8Marshaler.FromNative(cUrl);
string username = LaxUtf8Marshaler.FromNative(usernameFromUrl);
SupportedCredentialTypes types = default(SupportedCredentialTypes);
if (credTypes.HasFlag(GitCredentialType.UserPassPlaintext))
{
types |= SupportedCredentialTypes.UsernamePassword;
}
if (credTypes.HasFlag(GitCredentialType.Default))
{
types |= SupportedCredentialTypes.Default;
}
ptr = IntPtr.Zero;
try
{
var cred = CredentialsProvider(url, username, types);
if (cred == null)
{
return (int)GitErrorCode.PassThrough;
}
return cred.GitCredentialHandler(out ptr);
}
catch (Exception exception)
{
Proxy.git_error_set_str(GitErrorCategory.Callback, exception);
return (int)GitErrorCode.Error;
}
}
private unsafe int GitCertificateCheck(git_certificate* certPtr, int valid, IntPtr cHostname, IntPtr payload)
{
string hostname = LaxUtf8Marshaler.FromNative(cHostname);
Certificate cert = null;
switch (certPtr->type)
{
case GitCertificateType.X509:
cert = new CertificateX509((git_certificate_x509*)certPtr);
break;
case GitCertificateType.Hostkey:
cert = new CertificateSsh((git_certificate_ssh*)certPtr);
break;
}
bool result = false;
try
{
result = CertificateCheck(cert, valid != 0, hostname);
}
catch (Exception exception)
{
Proxy.git_error_set_str(GitErrorCategory.Callback, exception);
}
return Proxy.ConvertResultToCancelFlag(result);
}
private int GitPushNegotiationHandler(IntPtr updates, UIntPtr len, IntPtr payload)
{
if (updates == IntPtr.Zero)
{
return (int)GitErrorCode.Error;
}
bool result = false;
try
{
int length = len.ConvertToInt();
PushUpdate[] pushUpdates = new PushUpdate[length];
unsafe
{
IntPtr* ptr = (IntPtr*)updates.ToPointer();
for (int i = 0; i < length; i++)
{
if (ptr[i] == IntPtr.Zero)
{
throw new NullReferenceException("Unexpected null git_push_update pointer was encountered");
}
PushUpdate pushUpdate = new PushUpdate((git_push_update*)ptr[i].ToPointer());
pushUpdates[i] = pushUpdate;
}
result = PrePushCallback(pushUpdates);
}
}
catch (Exception exception)
{
Log.Write(LogLevel.Error, exception.ToString());
Proxy.git_error_set_str(GitErrorCategory.Callback, exception);
result = false;
}
return Proxy.ConvertResultToCancelFlag(result);
}
#endregion
}
}