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
use std::marker;
use std::mem;
use std::ptr;
use {raw, Oid, ObjectType, Error, Buf, Commit, Tag, Blob, Tree, Repository};
use {Describe, DescribeOptions};
use util::Binding;
pub struct Object<'repo> {
raw: *mut raw::git_object,
_marker: marker::PhantomData<&'repo Repository>,
}
impl<'repo> Object<'repo> {
pub fn id(&self) -> Oid {
unsafe {
Binding::from_raw(raw::git_object_id(&*self.raw))
}
}
pub fn kind(&self) -> Option<ObjectType> {
ObjectType::from_raw(unsafe { raw::git_object_type(&*self.raw) })
}
pub fn peel(&self, kind: ObjectType) -> Result<Object<'repo>, Error> {
let mut raw = 0 as *mut raw::git_object;
unsafe {
try_call!(raw::git_object_peel(&mut raw, &*self.raw(), kind));
Ok(Binding::from_raw(raw))
}
}
pub fn short_id(&self) -> Result<Buf, Error> {
unsafe {
let buf = Buf::new();
try_call!(raw::git_object_short_id(buf.raw(), &*self.raw()));
Ok(buf)
}
}
pub fn as_commit(&self) -> Option<&Commit<'repo>> {
self.cast(ObjectType::Commit)
}
pub fn into_commit(self) -> Result<Commit<'repo>, Object<'repo>> {
self.cast_into(ObjectType::Commit)
}
pub fn as_tag(&self) -> Option<&Tag<'repo>> {
self.cast(ObjectType::Tag)
}
pub fn into_tag(self) -> Result<Tag<'repo>, Object<'repo>> {
self.cast_into(ObjectType::Tag)
}
pub fn as_tree(&self) -> Option<&Tree<'repo>> {
self.cast(ObjectType::Tree)
}
pub fn into_tree(self) -> Result<Tree<'repo>, Object<'repo>> {
self.cast_into(ObjectType::Tree)
}
pub fn as_blob(&self) -> Option<&Blob<'repo>> {
self.cast(ObjectType::Blob)
}
pub fn into_blob(self) -> Result<Blob<'repo>, Object<'repo>> {
self.cast_into(ObjectType::Blob)
}
pub fn describe(&self, opts: &DescribeOptions)
-> Result<Describe, Error> {
let mut ret = 0 as *mut _;
unsafe {
try_call!(raw::git_describe_commit(&mut ret, self.raw, opts.raw()));
Ok(Binding::from_raw(ret))
}
}
fn cast<T>(&self, kind: ObjectType) -> Option<&T> {
assert_eq!(mem::size_of::<Object>(), mem::size_of::<T>());
if self.kind() == Some(kind) {
unsafe { Some(&*(self as *const _ as *const T)) }
} else {
None
}
}
fn cast_into<T>(self, kind: ObjectType) -> Result<T, Object<'repo>> {
assert_eq!(mem::size_of_val(&self), mem::size_of::<T>());
if self.kind() == Some(kind) {
Ok(unsafe {
let other = ptr::read(&self as *const _ as *const T);
mem::forget(self);
other
})
} else {
Err(self)
}
}
}
impl<'repo> Clone for Object<'repo> {
fn clone(&self) -> Object<'repo> {
let mut raw = 0 as *mut raw::git_object;
unsafe {
let rc = raw::git_object_dup(&mut raw, self.raw);
assert_eq!(rc, 0);
Binding::from_raw(raw)
}
}
}
impl<'repo> Binding for Object<'repo> {
type Raw = *mut raw::git_object;
unsafe fn from_raw(raw: *mut raw::git_object) -> Object<'repo> {
Object { raw: raw, _marker: marker::PhantomData, }
}
fn raw(&self) -> *mut raw::git_object { self.raw }
}
impl<'repo> Drop for Object<'repo> {
fn drop(&mut self) {
unsafe { raw::git_object_free(self.raw) }
}
}