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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use libc::{self, c_char, c_int, c_void};
use std::cmp::Ordering;
use std::ffi::{CStr, CString};
use std::marker;
use std::mem;
use std::ops::Range;
use std::path::Path;
use std::ptr;
use std::str;
use crate::util::{c_cmp_to_ordering, path_to_repo_path, Binding};
use crate::{panic, raw, Error, Object, ObjectType, Oid, Repository};
pub struct Tree<'repo> {
raw: *mut raw::git_tree,
_marker: marker::PhantomData<Object<'repo>>,
}
pub struct TreeEntry<'tree> {
raw: *mut raw::git_tree_entry,
owned: bool,
_marker: marker::PhantomData<&'tree raw::git_tree_entry>,
}
pub struct TreeIter<'tree> {
range: Range<usize>,
tree: &'tree Tree<'tree>,
}
pub enum TreeWalkMode {
PreOrder = 0,
PostOrder = 1,
}
#[repr(i32)]
pub enum TreeWalkResult {
Ok = 0,
Skip = 1,
Abort = raw::GIT_EUSER,
}
impl Into<i32> for TreeWalkResult {
fn into(self) -> i32 {
self as i32
}
}
impl Into<raw::git_treewalk_mode> for TreeWalkMode {
#[cfg(target_env = "msvc")]
fn into(self) -> raw::git_treewalk_mode {
self as i32
}
#[cfg(not(target_env = "msvc"))]
fn into(self) -> raw::git_treewalk_mode {
self as u32
}
}
impl<'repo> Tree<'repo> {
pub fn id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_tree_id(&*self.raw)) }
}
pub fn len(&self) -> usize {
unsafe { raw::git_tree_entrycount(&*self.raw) as usize }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> TreeIter<'_> {
TreeIter {
range: 0..self.len(),
tree: self,
}
}
pub fn walk<C, T>(&self, mode: TreeWalkMode, mut callback: C) -> Result<(), Error>
where
C: FnMut(&str, &TreeEntry<'_>) -> T,
T: Into<i32>,
{
#[allow(unused)]
struct TreeWalkCbData<'a, T> {
pub callback: &'a mut TreeWalkCb<'a, T>,
}
unsafe {
let mut data = TreeWalkCbData {
callback: &mut callback,
};
raw::git_tree_walk(
self.raw(),
mode.into(),
Some(treewalk_cb::<T>),
&mut data as *mut _ as *mut c_void,
);
Ok(())
}
}
pub fn get_id(&self, id: Oid) -> Option<TreeEntry<'_>> {
unsafe {
let ptr = raw::git_tree_entry_byid(&*self.raw(), &*id.raw());
if ptr.is_null() {
None
} else {
Some(entry_from_raw_const(ptr))
}
}
}
pub fn get(&self, n: usize) -> Option<TreeEntry<'_>> {
unsafe {
let ptr = raw::git_tree_entry_byindex(&*self.raw(), n as libc::size_t);
if ptr.is_null() {
None
} else {
Some(entry_from_raw_const(ptr))
}
}
}
pub fn get_name(&self, filename: &str) -> Option<TreeEntry<'_>> {
let filename = CString::new(filename).unwrap();
unsafe {
let ptr = call!(raw::git_tree_entry_byname(&*self.raw(), filename));
if ptr.is_null() {
None
} else {
Some(entry_from_raw_const(ptr))
}
}
}
pub fn get_path(&self, path: &Path) -> Result<TreeEntry<'static>, Error> {
let path = path_to_repo_path(path)?;
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_tree_entry_bypath(&mut ret, &*self.raw(), path));
Ok(Binding::from_raw(ret))
}
}
pub fn as_object(&self) -> &Object<'repo> {
unsafe { &*(self as *const _ as *const Object<'repo>) }
}
pub fn into_object(self) -> Object<'repo> {
assert_eq!(mem::size_of_val(&self), mem::size_of::<Object<'_>>());
unsafe { mem::transmute(self) }
}
}
type TreeWalkCb<'a, T> = dyn FnMut(&str, &TreeEntry<'_>) -> T + 'a;
extern "C" fn treewalk_cb<T: Into<i32>>(
root: *const c_char,
entry: *const raw::git_tree_entry,
payload: *mut c_void,
) -> c_int {
match panic::wrap(|| unsafe {
let root = match CStr::from_ptr(root).to_str() {
Ok(value) => value,
_ => return -1,
};
let entry = entry_from_raw_const(entry);
let payload = payload as *mut &mut TreeWalkCb<'_, T>;
(*payload)(root, &entry).into()
}) {
Some(value) => value,
None => -1,
}
}
impl<'repo> Binding for Tree<'repo> {
type Raw = *mut raw::git_tree;
unsafe fn from_raw(raw: *mut raw::git_tree) -> Tree<'repo> {
Tree {
raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_tree {
self.raw
}
}
impl<'repo> std::fmt::Debug for Tree<'repo> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("Tree").field("id", &self.id()).finish()
}
}
impl<'repo> Clone for Tree<'repo> {
fn clone(&self) -> Self {
self.as_object().clone().into_tree().ok().unwrap()
}
}
impl<'repo> Drop for Tree<'repo> {
fn drop(&mut self) {
unsafe { raw::git_tree_free(self.raw) }
}
}
impl<'repo, 'iter> IntoIterator for &'iter Tree<'repo> {
type Item = TreeEntry<'iter>;
type IntoIter = TreeIter<'iter>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub unsafe fn entry_from_raw_const<'tree>(raw: *const raw::git_tree_entry) -> TreeEntry<'tree> {
TreeEntry {
raw: raw as *mut raw::git_tree_entry,
owned: false,
_marker: marker::PhantomData,
}
}
impl<'tree> TreeEntry<'tree> {
pub fn id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_tree_entry_id(&*self.raw)) }
}
pub fn name(&self) -> Option<&str> {
str::from_utf8(self.name_bytes()).ok()
}
pub fn name_bytes(&self) -> &[u8] {
unsafe { crate::opt_bytes(self, raw::git_tree_entry_name(&*self.raw())).unwrap() }
}
pub fn to_object<'a>(&self, repo: &'a Repository) -> Result<Object<'a>, Error> {
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_tree_entry_to_object(
&mut ret,
repo.raw(),
&*self.raw()
));
Ok(Binding::from_raw(ret))
}
}
pub fn kind(&self) -> Option<ObjectType> {
ObjectType::from_raw(unsafe { raw::git_tree_entry_type(&*self.raw) })
}
pub fn filemode(&self) -> i32 {
unsafe { raw::git_tree_entry_filemode(&*self.raw) as i32 }
}
pub fn filemode_raw(&self) -> i32 {
unsafe { raw::git_tree_entry_filemode_raw(&*self.raw) as i32 }
}
pub fn to_owned(&self) -> TreeEntry<'static> {
unsafe {
let me = mem::transmute::<&TreeEntry<'tree>, &TreeEntry<'static>>(self);
me.clone()
}
}
}
impl<'a> Binding for TreeEntry<'a> {
type Raw = *mut raw::git_tree_entry;
unsafe fn from_raw(raw: *mut raw::git_tree_entry) -> TreeEntry<'a> {
TreeEntry {
raw,
owned: true,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_tree_entry {
self.raw
}
}
impl<'a> Clone for TreeEntry<'a> {
fn clone(&self) -> TreeEntry<'a> {
let mut ret = ptr::null_mut();
unsafe {
assert_eq!(raw::git_tree_entry_dup(&mut ret, &*self.raw()), 0);
Binding::from_raw(ret)
}
}
}
impl<'a> PartialOrd for TreeEntry<'a> {
fn partial_cmp(&self, other: &TreeEntry<'a>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<'a> Ord for TreeEntry<'a> {
fn cmp(&self, other: &TreeEntry<'a>) -> Ordering {
c_cmp_to_ordering(unsafe { raw::git_tree_entry_cmp(&*self.raw(), &*other.raw()) })
}
}
impl<'a> PartialEq for TreeEntry<'a> {
fn eq(&self, other: &TreeEntry<'a>) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl<'a> Eq for TreeEntry<'a> {}
impl<'a> Drop for TreeEntry<'a> {
fn drop(&mut self) {
if self.owned {
unsafe { raw::git_tree_entry_free(self.raw) }
}
}
}
impl<'tree> Iterator for TreeIter<'tree> {
type Item = TreeEntry<'tree>;
fn next(&mut self) -> Option<TreeEntry<'tree>> {
self.range.next().and_then(|i| self.tree.get(i))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
}
impl<'tree> DoubleEndedIterator for TreeIter<'tree> {
fn next_back(&mut self) -> Option<TreeEntry<'tree>> {
self.range.next_back().and_then(|i| self.tree.get(i))
}
}
impl<'tree> ExactSizeIterator for TreeIter<'tree> {}
#[cfg(test)]
mod tests {
use super::{TreeWalkMode, TreeWalkResult};
use crate::{Object, ObjectType, Repository, Tree, TreeEntry};
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use tempfile::TempDir;
pub struct TestTreeIter<'a> {
entries: Vec<TreeEntry<'a>>,
repo: &'a Repository,
}
impl<'a> Iterator for TestTreeIter<'a> {
type Item = TreeEntry<'a>;
fn next(&mut self) -> Option<TreeEntry<'a>> {
if self.entries.is_empty() {
None
} else {
let entry = self.entries.remove(0);
match entry.kind() {
Some(ObjectType::Tree) => {
let obj: Object<'a> = entry.to_object(self.repo).unwrap();
let tree: &Tree<'a> = obj.as_tree().unwrap();
for entry in tree.iter() {
self.entries.push(entry.to_owned());
}
}
_ => {}
}
Some(entry)
}
}
}
fn tree_iter<'repo>(tree: &Tree<'repo>, repo: &'repo Repository) -> TestTreeIter<'repo> {
let mut initial = vec![];
for entry in tree.iter() {
initial.push(entry.to_owned());
}
TestTreeIter {
entries: initial,
repo: repo,
}
}
#[test]
fn smoke_tree_iter() {
let (td, repo) = crate::test::repo_init();
setup_repo(&td, &repo);
let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
let tree = repo.find_tree(commit.tree_id()).unwrap();
assert_eq!(tree.id(), commit.tree_id());
assert_eq!(tree.len(), 1);
for entry in tree_iter(&tree, &repo) {
println!("iter entry {:?}", entry.name());
}
}
fn setup_repo(td: &TempDir, repo: &Repository) {
let mut index = repo.index().unwrap();
File::create(&td.path().join("foo"))
.unwrap()
.write_all(b"foo")
.unwrap();
index.add_path(Path::new("foo")).unwrap();
let id = index.write_tree().unwrap();
let sig = repo.signature().unwrap();
let tree = repo.find_tree(id).unwrap();
let parent = repo
.find_commit(repo.head().unwrap().target().unwrap())
.unwrap();
repo.commit(
Some("HEAD"),
&sig,
&sig,
"another commit",
&tree,
&[&parent],
)
.unwrap();
}
#[test]
fn smoke() {
let (td, repo) = crate::test::repo_init();
setup_repo(&td, &repo);
let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
let tree = repo.find_tree(commit.tree_id()).unwrap();
assert_eq!(tree.id(), commit.tree_id());
assert_eq!(tree.len(), 1);
{
let e1 = tree.get(0).unwrap();
assert!(e1 == tree.get_id(e1.id()).unwrap());
assert!(e1 == tree.get_name("foo").unwrap());
assert!(e1 == tree.get_path(Path::new("foo")).unwrap());
assert_eq!(e1.name(), Some("foo"));
e1.to_object(&repo).unwrap();
}
tree.into_object();
repo.find_object(commit.tree_id(), None)
.unwrap()
.as_tree()
.unwrap();
repo.find_object(commit.tree_id(), None)
.unwrap()
.into_tree()
.ok()
.unwrap();
}
#[test]
fn tree_walk() {
let (td, repo) = crate::test::repo_init();
setup_repo(&td, &repo);
let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
let tree = repo.find_tree(commit.tree_id()).unwrap();
let mut ct = 0;
tree.walk(TreeWalkMode::PreOrder, |_, entry| {
assert_eq!(entry.name(), Some("foo"));
ct += 1;
0
})
.unwrap();
assert_eq!(ct, 1);
let mut ct = 0;
tree.walk(TreeWalkMode::PreOrder, |_, entry| {
assert_eq!(entry.name(), Some("foo"));
ct += 1;
TreeWalkResult::Ok
})
.unwrap();
assert_eq!(ct, 1);
}
}