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
use std::ffi::CStr;
use std::marker;
use std::mem;
use std::slice;
use std::str;
use libc::{c_void, c_int, c_char, c_uint};
use {raw, Error, Cred, CredentialType, Oid};
use cert::Cert;
use util::Binding;
pub struct RemoteCallbacks<'a> {
progress: Option<Box<TransferProgress<'a>>>,
credentials: Option<Box<Credentials<'a>>>,
sideband_progress: Option<Box<TransportMessage<'a>>>,
update_tips: Option<Box<UpdateTips<'a>>>,
certificate_check: Option<Box<CertificateCheck<'a>>>,
}
pub struct Progress<'a> {
raw: ProgressState,
_marker: marker::PhantomData<&'a raw::git_transfer_progress>,
}
enum ProgressState {
Borrowed(*const raw::git_transfer_progress),
Owned(raw::git_transfer_progress),
}
pub type Credentials<'a> = FnMut(&str, Option<&str>, CredentialType)
-> Result<Cred, Error> + 'a;
pub type TransferProgress<'a> = FnMut(Progress) -> bool + 'a;
pub type TransportMessage<'a> = FnMut(&[u8]) -> bool + 'a;
pub type UpdateTips<'a> = FnMut(&str, Oid, Oid) -> bool + 'a;
pub type CertificateCheck<'a> = FnMut(&Cert, &str) -> bool + 'a;
impl<'a> RemoteCallbacks<'a> {
pub fn new() -> RemoteCallbacks<'a> {
RemoteCallbacks {
credentials: None,
progress: None,
sideband_progress: None,
update_tips: None,
certificate_check: None,
}
}
pub fn credentials<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
where F: FnMut(&str, Option<&str>, CredentialType)
-> Result<Cred, Error> + 'a
{
self.credentials = Some(Box::new(cb) as Box<Credentials<'a>>);
self
}
pub fn transfer_progress<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
where F: FnMut(Progress) -> bool + 'a {
self.progress = Some(Box::new(cb) as Box<TransferProgress<'a>>);
self
}
pub fn sideband_progress<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
where F: FnMut(&[u8]) -> bool + 'a {
self.sideband_progress = Some(Box::new(cb) as Box<TransportMessage<'a>>);
self
}
pub fn update_tips<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
where F: FnMut(&str, Oid, Oid) -> bool + 'a {
self.update_tips = Some(Box::new(cb) as Box<UpdateTips<'a>>);
self
}
pub fn certificate_check<F>(&mut self, cb: F) -> &mut RemoteCallbacks<'a>
where F: FnMut(&Cert, &str) -> bool + 'a
{
self.certificate_check = Some(Box::new(cb) as Box<CertificateCheck<'a>>);
self
}
}
impl<'a> Binding for RemoteCallbacks<'a> {
type Raw = raw::git_remote_callbacks;
unsafe fn from_raw(_raw: raw::git_remote_callbacks) -> RemoteCallbacks<'a> {
panic!("unimplemented");
}
fn raw(&self) -> raw::git_remote_callbacks {
unsafe {
let mut callbacks: raw::git_remote_callbacks = mem::zeroed();
assert_eq!(raw::git_remote_init_callbacks(&mut callbacks,
raw::GIT_REMOTE_CALLBACKS_VERSION), 0);
if self.progress.is_some() {
let f: raw::git_transfer_progress_cb = transfer_progress_cb;
callbacks.transfer_progress = Some(f);
}
if self.credentials.is_some() {
let f: raw::git_cred_acquire_cb = credentials_cb;
callbacks.credentials = Some(f);
}
if self.sideband_progress.is_some() {
let f: raw::git_transport_message_cb = sideband_progress_cb;
callbacks.sideband_progress = Some(f);
}
if self.certificate_check.is_some() {
let f: raw::git_transport_certificate_check_cb =
certificate_check_cb;
callbacks.certificate_check = Some(f);
}
if self.update_tips.is_some() {
let f: extern fn(*const c_char, *const raw::git_oid,
*const raw::git_oid, *mut c_void) -> c_int
= update_tips_cb;
callbacks.update_tips = Some(f);
}
callbacks.payload = self as *const _ as *mut _;
return callbacks;
}
}
}
impl<'a> Progress<'a> {
pub fn total_objects(&self) -> usize {
unsafe { (*self.raw()).total_objects as usize }
}
pub fn indexed_objects(&self) -> usize {
unsafe { (*self.raw()).indexed_objects as usize }
}
pub fn received_objects(&self) -> usize {
unsafe { (*self.raw()).received_objects as usize }
}
pub fn local_objects(&self) -> usize {
unsafe { (*self.raw()).local_objects as usize }
}
pub fn total_deltas(&self) -> usize {
unsafe { (*self.raw()).total_deltas as usize }
}
pub fn indexed_deltas(&self) -> usize {
unsafe { (*self.raw()).indexed_deltas as usize }
}
pub fn received_bytes(&self) -> usize {
unsafe { (*self.raw()).received_bytes as usize }
}
pub fn to_owned(&self) -> Progress<'static> {
Progress {
raw: ProgressState::Owned(unsafe { *self.raw() }),
_marker: marker::PhantomData,
}
}
}
impl<'a> Binding for Progress<'a> {
type Raw = *const raw::git_transfer_progress;
unsafe fn from_raw(raw: *const raw::git_transfer_progress)
-> Progress<'a> {
Progress {
raw: ProgressState::Borrowed(raw),
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *const raw::git_transfer_progress {
match self.raw {
ProgressState::Borrowed(raw) => raw,
ProgressState::Owned(ref raw) => raw as *const _,
}
}
}
wrap_env! {
fn credentials_cb(ret: *mut *mut raw::git_cred,
url: *const c_char,
username_from_url: *const c_char,
allowed_types: c_uint,
payload: *mut c_void) -> c_int {
unsafe {
let payload = &mut *(payload as *mut RemoteCallbacks);
let callback = try!(payload.credentials.as_mut()
.ok_or(raw::GIT_PASSTHROUGH as c_int));
*ret = 0 as *mut raw::git_cred;
let url = try!(str::from_utf8(CStr::from_ptr(url).to_bytes())
.map_err(|_| raw::GIT_PASSTHROUGH as c_int));
let username_from_url = match ::opt_bytes(&url, username_from_url) {
Some(username) => {
Some(try!(str::from_utf8(username)
.map_err(|_| raw::GIT_PASSTHROUGH as c_int)))
}
None => None,
};
let cred_type = CredentialType::from_bits_truncate(allowed_types as u32);
callback(url, username_from_url, cred_type).map_err(|e| {
e.raw_code() as c_int
})
}
}
returning ok as unsafe {
match ok {
Some(Ok(cred)) => {
if allowed_types & (cred.credtype() as c_uint) != 0 {
*ret = cred.unwrap();
0
} else {
raw::GIT_PASSTHROUGH as c_int
}
}
Some(Err(e)) => e,
None => -1,
}
}
}
wrap_env! {
fn transfer_progress_cb(stats: *const raw::git_transfer_progress,
payload: *mut c_void) -> c_int {
unsafe {
let payload = &mut *(payload as *mut RemoteCallbacks);
let callback = match payload.progress {
Some(ref mut c) => c,
None => return true,
};
let progress = Binding::from_raw(stats);
callback(progress)
}
}
returning ok as (if ok == Some(true) {0} else {-1})
}
wrap_env! {
fn sideband_progress_cb(str: *const c_char,
len: c_int,
payload: *mut c_void) -> c_int {
unsafe {
let payload = &mut *(payload as *mut RemoteCallbacks);
let callback = match payload.sideband_progress {
Some(ref mut c) => c,
None => return true,
};
let buf = slice::from_raw_parts(str as *const u8, len as usize);
callback(buf)
}
}
returning ok as (if ok == Some(true) {0} else {-1})
}
wrap_env! {
fn update_tips_cb(refname: *const c_char,
a: *const raw::git_oid,
b: *const raw::git_oid,
data: *mut c_void) -> c_int {
unsafe {
let payload = &mut *(data as *mut RemoteCallbacks);
let callback = match payload.update_tips {
Some(ref mut c) => c,
None => return true,
};
let refname = str::from_utf8(CStr::from_ptr(refname).to_bytes())
.unwrap();
let a = Binding::from_raw(a);
let b = Binding::from_raw(b);
callback(refname, a, b)
}
}
returning ok as (if ok == Some(true) {0} else {-1})
}
wrap_env! {
fn certificate_check_cb(cert: *mut raw::git_cert,
_valid: c_int,
hostname: *const c_char,
data: *mut c_void) -> c_int {
unsafe {
let payload = &mut *(data as *mut RemoteCallbacks);
let callback = match payload.certificate_check {
Some(ref mut c) => c,
None => return true,
};
let cert = Binding::from_raw(cert);
let hostname = str::from_utf8(CStr::from_ptr(hostname).to_bytes())
.unwrap();
callback(&cert, hostname)
}
}
returning ok as (if ok == Some(true) {0} else {-1})
}