-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.rs
334 lines (302 loc) · 8.72 KB
/
tree.rs
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
use std::libc::c_int;
use std::{ptr, cast};
use std::str::raw::from_c_str;
use super::{OID, OType, FileMode, GitError};
use super::{git_error, last_error};
use super::Repository;
use ext;
pub struct Tree<'r> {
// TODO: make this field priv
tree: *ext::git_tree,
priv owner: &'r Repository,
}
impl<'r> Tree<'r> {
pub fn new(tree: *ext::git_tree, owner: &'r Repository) -> Tree<'r>
{
Tree {
tree: tree,
owner: owner
}
}
/// Get the id of a tree.
pub fn id<'r>(& self) -> &'r OID
{
unsafe {
cast::transmute(ext::git_tree_id(self.tree))
}
}
/// Lookup a tree entry by its filename
pub fn entry_byname(&self, filename: &str) -> Option<~TreeEntry>
{
filename.with_c_str(|c_filename| {
unsafe {
let entry_ptr = ext::git_tree_entry_byname(self.tree, c_filename);
if entry_ptr == ptr::null() {
None
} else {
Some( ~TreeEntry::new(entry_ptr, false) )
}
}
})
}
/// Lookup a tree entry by SHA value.
/// Warning: this must examine every entry in the tree, so it is not fast.
pub fn entry_byoid(&self, oid: &OID) -> Option<~TreeEntry>
{
unsafe {
let entry_ptr = ext::git_tree_entry_byoid(self.tree, oid);
if entry_ptr == ptr::null() {
None
} else {
Some( ~TreeEntry::new(entry_ptr, false) )
}
}
}
/// Retrieve a tree entry contained in a tree or in any of its subtrees,
/// given its relative path.
pub fn entry_bypath(&self, path: &str) -> Option<~TreeEntry>
{
path.with_c_str(|c_path| {
unsafe {
let mut entry_ptr:*ext::git_tree_entry = ptr::null();
if ext::git_tree_entry_bypath(&mut entry_ptr, self.tree, c_path) == 0 {
Some( ~TreeEntry::new(entry_ptr, true) )
} else {
None
}
}
})
}
}
#[unsafe_destructor]
impl<'r> Drop for Tree<'r> {
fn drop(&mut self) {
unsafe {
ext::git_tree_free(self.tree);
}
}
}
pub struct TreeEntry {
priv tree_entry: *ext::git_tree_entry,
priv owned: bool,
}
impl TreeEntry {
fn new(tree_entry: *ext::git_tree_entry, owned: bool) -> TreeEntry {
TreeEntry {
tree_entry: tree_entry,
owned: owned
}
}
/// Get the filename of a tree entry
pub fn name(&self) -> ~str
{
unsafe {
from_c_str(ext::git_tree_entry_name(self.tree_entry))
}
}
/// Get the id of the object pointed by the entry
pub fn id<'r>(&self) -> &'r OID
{
unsafe {
cast::transmute(ext::git_tree_entry_id(self.tree_entry))
}
}
pub fn otype(&self) -> OType
{
unsafe {
ext::git_tree_entry_type(self.tree_entry)
}
}
pub fn filemode(&self) -> FileMode
{
unsafe {
ext::git_tree_entry_filemode(self.tree_entry)
}
}
}
#[unsafe_destructor]
impl Drop for TreeEntry {
fn drop(&mut self) {
unsafe {
if self.owned {
ext::git_tree_entry_free(self.tree_entry);
}
}
}
}
impl Clone for TreeEntry {
fn clone(&self) -> TreeEntry {
unsafe {
TreeEntry::new(ext::git_tree_entry_dup(self.tree_entry), self.owned)
}
}
}
#[inline]
fn tree_entry_cmp(a: &TreeEntry, b: &TreeEntry) -> c_int
{
unsafe {
ext::git_tree_entry_cmp(a.tree_entry, b.tree_entry)
}
}
impl Eq for TreeEntry {
fn eq(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) == 0
}
fn ne(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) != 0
}
}
impl Ord for TreeEntry {
fn lt(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) < 0
}
fn le(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) <= 0
}
fn gt(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) > 0
}
fn ge(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) >= 0
}
}
impl TotalEq for TreeEntry {
fn equals(&self, other: &TreeEntry) -> bool {
tree_entry_cmp(self, other) == 0
}
}
impl TotalOrd for TreeEntry {
fn cmp(&self, other: &TreeEntry) -> Ordering {
let comp = tree_entry_cmp(self, other);
if comp < 0 {
Less
} else if comp == 0 {
Equal
} else {
Greater
}
}
}
impl TreeBuilder {
/// Clear all the entires in the builder
pub fn clear(&self)
{
unsafe {
ext::git_treebuilder_clear(self.bld);
}
}
/// Get an entry from the builder from its filename
pub fn get(&self, filename: &str) -> ~TreeEntry
{
filename.with_c_str(|c_filename| {
unsafe {
let entry_ptr = ext::git_treebuilder_get(self.bld, c_filename);
~TreeEntry::new(entry_ptr, false)
}
})
}
/// Add or update an entry to the builder
///
/// Insert a new entry for `filename` in the builder with the
/// given attributes.
///
/// If an entry named `filename` already exists, its attributes
/// will be updated with the given ones.
///
/// No attempt is being made to ensure that the provided oid points
/// to an existing git object in the object database, nor that the
/// attributes make sense regarding the type of the pointed at object.
///
/// filename: Filename of the entry
/// id: SHA1 OID of the entry
/// filemode: Folder attributes of the entry. This parameter must not be GIT_FILEMODE_NEW
pub fn insert(&self, filename: &str, id: &OID, filemode: FileMode) ->
Result<~TreeEntry, (~str, GitError)>
{
filename.with_c_str(|c_filename| {
unsafe {
let mut entry_ptr:*ext::git_tree_entry = ptr::null();
if(ext::git_treebuilder_insert(&mut entry_ptr, self.bld, c_filename, id,
filemode) == 0) {
Ok( ~TreeEntry::new(entry_ptr, false) )
} else {
Err( last_error() )
}
}
})
}
/// Remove an entry from the builder by its filename
/// return true if successful, false if the entry does not exist
pub fn remove(&self, filename: &str) -> bool
{
filename.with_c_str(|c_filename| {
unsafe {
ext::git_treebuilder_remove(self.bld, c_filename) == 0
}
})
}
/// Write the contents of the tree builder as a tree object
///
/// The tree builder will be written to the given `repo`, and its
/// identifying SHA1 hash will be returned
///
/// repo: Repository in which to store the object
pub fn write(&self, repo: &Repository) -> OID
{
let mut oid = OID { id: [0, ..20] };
unsafe {
if ext::git_treebuilder_write(&mut oid, repo.repo, self.bld) != 0 {
git_error::cond.raise(last_error())
}
}
return oid;
}
/// Get the number of entries listed in a treebuilder
pub fn entrycount(&self) -> uint
{
unsafe {
ext::git_treebuilder_entrycount(self.bld) as uint
}
}
}
pub struct TreeBuilder {
priv bld: *ext::git_treebuilder,
}
impl TreeBuilder {
/// Create a new tree builder.
/// The tree builder can be used to create or modify trees in memory and
/// write them as tree objects to the database.
/// The tree builder will start with no entries and will have to be filled manually.
pub fn new() -> TreeBuilder
{
let mut bld:*ext::git_treebuilder = ptr::null();
unsafe {
if ext::git_treebuilder_create(&mut bld, ptr::null()) == 0 {
TreeBuilder { bld: bld }
} else {
fail!(~"failed to create treebuilder")
}
}
}
/// Create a new tree builder.
/// The tree builder will be initialized with the entries of the given tree.
pub fn from_tree(tree: &Tree) -> TreeBuilder
{
let mut bld:*ext::git_treebuilder = ptr::null();
unsafe {
if ext::git_treebuilder_create(&mut bld, tree.tree) == 0 {
TreeBuilder { bld: bld }
} else {
fail!(~"failed to create treebuilder")
}
}
}
}
#[unsafe_destructor]
impl Drop for TreeBuilder {
fn drop(&mut self) {
unsafe {
ext::git_treebuilder_free(self.bld);
}
}
}