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
use std::kinds::marker;
use std::mem;
use {raw, Oid, Repository, ObjectType, Error, Buf};
/// A structure to represent a git [object][1]
///
/// [1]: http://git-scm.com/book/en/Git-Internals-Git-Objects
pub struct Object<'repo> {
raw: *mut raw::git_object,
marker1: marker::ContravariantLifetime<'repo>,
marker2: marker::NoSend,
marker3: marker::NoSync,
}
impl<'repo> Object<'repo> {
/// Create a new object from its raw component.
///
/// This method is unsafe as there is no guarantee that `raw` is a valid
/// pointer.
pub unsafe fn from_raw(_repo: &Repository,
raw: *mut raw::git_object) -> Object {
Object::from_raw_ptr(raw)
}
/// Even more unsafe than `from_raw`, the output lifetime is not attached to
/// any input.
pub unsafe fn from_raw_ptr<'a>(raw: *mut raw::git_object) -> Object<'a> {
Object {
raw: raw,
marker1: marker::ContravariantLifetime,
marker2: marker::NoSend,
marker3: marker::NoSync,
}
}
/// Get the id (SHA1) of a repository object
pub fn id(&self) -> Oid {
unsafe {
Oid::from_raw(raw::git_object_id(&*self.raw))
}
}
/// Get access to the underlying raw pointer.
pub fn raw(&self) -> *mut raw::git_object { self.raw }
/// Get the object type of an object.
///
/// If the type is unknown, then `None` is returned.
pub fn kind(&self) -> Option<ObjectType> {
ObjectType::from_raw(unsafe { raw::git_object_type(&*self.raw) })
}
/// Recursively peel an object until an object of the specified type is met.
///
/// If you pass `Any` as the target type, then the object will be
/// peeled until the type changes (e.g. a tag will be chased until the
/// referenced object is no longer a tag).
pub fn peel(&self, kind: ObjectType) -> Result<Object, Error> {
let mut raw = 0 as *mut raw::git_object;
unsafe {
try_call!(raw::git_object_peel(&mut raw, &*self.raw(), kind));
}
Ok(Object {
raw: raw,
marker1: marker::ContravariantLifetime,
marker2: marker::NoSend,
marker3: marker::NoSync,
})
}
/// Get a short abbreviated OID string for the object
///
/// This starts at the "core.abbrev" length (default 7 characters) and
/// iteratively extends to a longer string if that length is ambiguous. The
/// result will be unambiguous (at least until new objects are added to the
/// repository).
pub fn short_id(&self) -> Result<Buf, Error> {
unsafe {
let mut raw: raw::git_buf = mem::zeroed();
try_call!(raw::git_object_short_id(&mut raw, &*self.raw()));
Ok(Buf::from_raw(raw))
}
}
}
impl<'a> Clone for Object<'a> {
fn clone(&self) -> Object<'a> {
let mut raw = 0 as *mut raw::git_object;
let rc = unsafe { raw::git_object_dup(&mut raw, self.raw) };
assert_eq!(rc, 0);
Object {
raw: raw,
marker1: marker::ContravariantLifetime,
marker2: marker::NoSend,
marker3: marker::NoSync,
}
}
}
#[unsafe_destructor]
impl<'a> Drop for Object<'a> {
fn drop(&mut self) {
unsafe { raw::git_object_free(self.raw) }
}
}