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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::ffi::{CStr, CString};
use std::mem;
use std::path::Path;
use libc::{c_char, size_t, c_void, c_uint, c_int};
use {raw, panic, Error, Repository, FetchOptions, IntoCString};
use util::{self, Binding};
pub struct RepoBuilder<'cb> {
bare: bool,
branch: Option<CString>,
local: bool,
hardlinks: bool,
checkout: Option<CheckoutBuilder<'cb>>,
fetch_opts: Option<FetchOptions<'cb>>,
}
pub struct CheckoutBuilder<'cb> {
their_label: Option<CString>,
our_label: Option<CString>,
ancestor_label: Option<CString>,
target_dir: Option<CString>,
paths: Vec<CString>,
path_ptrs: Vec<*const c_char>,
file_perm: Option<i32>,
dir_perm: Option<i32>,
disable_filters: bool,
checkout_opts: u32,
progress: Option<Box<Progress<'cb>>>,
}
pub type Progress<'a> = FnMut(Option<&Path>, usize, usize) + 'a;
impl<'cb> RepoBuilder<'cb> {
pub fn new() -> RepoBuilder<'cb> {
::init();
RepoBuilder {
bare: false,
branch: None,
local: true,
hardlinks: true,
checkout: None,
fetch_opts: None,
}
}
pub fn bare(&mut self, bare: bool) -> &mut RepoBuilder<'cb> {
self.bare = bare;
self
}
pub fn branch(&mut self, branch: &str) -> &mut RepoBuilder<'cb> {
self.branch = Some(CString::new(branch).unwrap());
self
}
pub fn local(&mut self, local: bool) -> &mut RepoBuilder<'cb> {
self.local = local;
self
}
pub fn hardlinks(&mut self, links: bool) -> &mut RepoBuilder<'cb> {
self.hardlinks = links;
self
}
pub fn with_checkout(&mut self, checkout: CheckoutBuilder<'cb>)
-> &mut RepoBuilder<'cb> {
self.checkout = Some(checkout);
self
}
pub fn fetch_options(&mut self, fetch_opts: FetchOptions<'cb>)
-> &mut RepoBuilder<'cb> {
self.fetch_opts = Some(fetch_opts);
self
}
pub fn clone(&mut self, url: &str, into: &Path) -> Result<Repository, Error> {
let mut opts: raw::git_clone_options = unsafe { mem::zeroed() };
unsafe {
try_call!(raw::git_clone_init_options(&mut opts,
raw::GIT_CLONE_OPTIONS_VERSION));
}
opts.bare = self.bare as c_int;
opts.checkout_branch = self.branch.as_ref().map(|s| {
s.as_ptr()
}).unwrap_or(0 as *const _);
opts.local = match (self.local, self.hardlinks) {
(true, false) => raw::GIT_CLONE_LOCAL_NO_LINKS,
(false, _) => raw::GIT_CLONE_NO_LOCAL,
(true, _) => raw::GIT_CLONE_LOCAL_AUTO,
};
opts.checkout_opts.checkout_strategy =
raw::GIT_CHECKOUT_SAFE as c_uint;
match self.fetch_opts {
Some(ref mut cbs) => {
opts.fetch_opts = cbs.raw();
},
None => {}
}
match self.checkout {
Some(ref mut c) => unsafe { c.configure(&mut opts.checkout_opts) },
None => {}
}
let url = try!(CString::new(url));
let into = try!(into.into_c_string());
let mut raw = 0 as *mut raw::git_repository;
unsafe {
try_call!(raw::git_clone(&mut raw, url, into, &opts));
Ok(Binding::from_raw(raw))
}
}
}
impl<'cb> CheckoutBuilder<'cb> {
pub fn new() -> CheckoutBuilder<'cb> {
::init();
CheckoutBuilder {
disable_filters: false,
dir_perm: None,
file_perm: None,
path_ptrs: Vec::new(),
paths: Vec::new(),
target_dir: None,
ancestor_label: None,
our_label: None,
their_label: None,
checkout_opts: raw::GIT_CHECKOUT_SAFE as u32,
progress: None,
}
}
pub fn dry_run(&mut self) -> &mut CheckoutBuilder<'cb> {
self.checkout_opts &= !((1 << 4) - 1);
self.checkout_opts |= raw::GIT_CHECKOUT_NONE as u32;
self
}
pub fn force(&mut self) -> &mut CheckoutBuilder<'cb> {
self.checkout_opts &= !((1 << 4) - 1);
self.checkout_opts |= raw::GIT_CHECKOUT_FORCE as u32;
self
}
pub fn safe(&mut self) -> &mut CheckoutBuilder<'cb> {
self.checkout_opts &= !((1 << 4) - 1);
self.checkout_opts |= raw::GIT_CHECKOUT_SAFE as u32;
self
}
fn flag(&mut self, bit: raw::git_checkout_strategy_t,
on: bool) -> &mut CheckoutBuilder<'cb> {
if on {
self.checkout_opts |= bit as u32;
} else {
self.checkout_opts &= !(bit as u32);
}
self
}
pub fn allow_conflicts(&mut self, allow: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_ALLOW_CONFLICTS, allow)
}
pub fn remove_untracked(&mut self, remove: bool)
-> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_REMOVE_UNTRACKED, remove)
}
pub fn remove_ignored(&mut self, remove: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_REMOVE_IGNORED, remove)
}
pub fn update_only(&mut self, update: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_UPDATE_ONLY, update)
}
pub fn update_index(&mut self, update: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_DONT_UPDATE_INDEX, !update)
}
pub fn refresh(&mut self, refresh: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_NO_REFRESH, !refresh)
}
pub fn skip_unmerged(&mut self, skip: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_SKIP_UNMERGED, skip)
}
pub fn use_ours(&mut self, ours: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_USE_OURS, ours)
}
pub fn use_theirs(&mut self, theirs: bool) -> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_USE_THEIRS, theirs)
}
pub fn overwrite_ignored(&mut self, overwrite: bool)
-> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, !overwrite)
}
pub fn conflict_style_merge(&mut self, on: bool)
-> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_CONFLICT_STYLE_MERGE, on)
}
pub fn conflict_style_diff3(&mut self, on: bool)
-> &mut CheckoutBuilder<'cb> {
self.flag(raw::GIT_CHECKOUT_CONFLICT_STYLE_DIFF3, on)
}
pub fn disable_filters(&mut self, disable: bool)
-> &mut CheckoutBuilder<'cb> {
self.disable_filters = disable;
self
}
pub fn dir_perm(&mut self, perm: i32) -> &mut CheckoutBuilder<'cb> {
self.dir_perm = Some(perm);
self
}
pub fn file_perm(&mut self, perm: i32) -> &mut CheckoutBuilder<'cb> {
self.file_perm = Some(perm);
self
}
pub fn path<T: IntoCString>(&mut self, path: T)
-> &mut CheckoutBuilder<'cb> {
let path = path.into_c_string().unwrap();
self.path_ptrs.push(path.as_ptr());
self.paths.push(path);
self
}
pub fn target_dir(&mut self, dst: &Path) -> &mut CheckoutBuilder<'cb> {
self.target_dir = Some(dst.into_c_string().unwrap());
self
}
pub fn ancestor_label(&mut self, label: &str) -> &mut CheckoutBuilder<'cb> {
self.ancestor_label = Some(CString::new(label).unwrap());
self
}
pub fn our_label(&mut self, label: &str) -> &mut CheckoutBuilder<'cb> {
self.our_label = Some(CString::new(label).unwrap());
self
}
pub fn their_label(&mut self, label: &str) -> &mut CheckoutBuilder<'cb> {
self.their_label = Some(CString::new(label).unwrap());
self
}
pub fn progress<F>(&mut self, cb: F) -> &mut CheckoutBuilder<'cb>
where F: FnMut(Option<&Path>, usize, usize) + 'cb {
self.progress = Some(Box::new(cb) as Box<Progress<'cb>>);
self
}
pub unsafe fn configure(&mut self, opts: &mut raw::git_checkout_options) {
opts.version = raw::GIT_CHECKOUT_OPTIONS_VERSION;
opts.disable_filters = self.disable_filters as c_int;
opts.dir_mode = self.dir_perm.unwrap_or(0) as c_uint;
opts.file_mode = self.file_perm.unwrap_or(0) as c_uint;
if self.path_ptrs.len() > 0 {
opts.paths.strings = self.path_ptrs.as_ptr() as *mut _;
opts.paths.count = self.path_ptrs.len() as size_t;
}
match self.target_dir {
Some(ref c) => opts.target_directory = c.as_ptr(),
None => {}
}
match self.ancestor_label {
Some(ref c) => opts.ancestor_label = c.as_ptr(),
None => {}
}
match self.our_label {
Some(ref c) => opts.our_label = c.as_ptr(),
None => {}
}
match self.their_label {
Some(ref c) => opts.their_label = c.as_ptr(),
None => {}
}
if self.progress.is_some() {
let f: raw::git_checkout_progress_cb = progress_cb;
opts.progress_cb = Some(f);
opts.progress_payload = self as *mut _ as *mut _;
}
opts.checkout_strategy = self.checkout_opts as c_uint;
}
}
extern fn progress_cb(path: *const c_char,
completed: size_t,
total: size_t,
data: *mut c_void) {
panic::wrap(|| unsafe {
let payload = &mut *(data as *mut CheckoutBuilder);
let callback = match payload.progress {
Some(ref mut c) => c,
None => return,
};
let path = if path.is_null() {
None
} else {
Some(util::bytes2path(CStr::from_ptr(path).to_bytes()))
};
callback(path, completed as usize, total as usize)
});
}
#[cfg(test)]
mod tests {
use std::fs;
use std::path::Path;
use tempdir::TempDir;
use super::RepoBuilder;
use Repository;
#[test]
fn smoke() {
let r = RepoBuilder::new().clone("/path/to/nowhere", Path::new("foo"));
assert!(r.is_err());
}
#[test]
fn smoke2() {
let td = TempDir::new("test").unwrap();
Repository::init_bare(&td.path().join("bare")).unwrap();
let url = if cfg!(unix) {
format!("file://{}/bare", td.path().display())
} else {
format!("file:///{}/bare", td.path().display().to_string()
.replace("\\", "/"))
};
let dst = td.path().join("foo");
RepoBuilder::new().clone(&url, &dst).unwrap();
fs::remove_dir_all(&dst).unwrap();
RepoBuilder::new().local(false).clone(&url, &dst).unwrap();
fs::remove_dir_all(&dst).unwrap();
RepoBuilder::new().local(false).hardlinks(false).bare(true)
.clone(&url, &dst).unwrap();
fs::remove_dir_all(&dst).unwrap();
assert!(RepoBuilder::new().branch("foo")
.clone(&url, &dst).is_err());
}
}