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
use std::marker;
use std::mem;
use {raw, Oid, ObjectType, Error, Buf, Commit, Tag, Blob, Tree};
use util::Binding;
/// 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,
marker: marker::ContravariantLifetime<'repo>,
}
impl<'repo> Object<'repo> {
/// Get the id (SHA1) of a repository object
pub fn id(&self) -> Oid {
unsafe {
Binding::from_raw(raw::git_object_id(&*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<'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))
}
}
/// 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 buf = Buf::new();
try_call!(raw::git_object_short_id(buf.raw(), &*self.raw()));
Ok(buf)
}
}
/// Attempt to view this object as a commit.
///
/// Returns `None` if the object is not actually a commit.
pub fn as_commit(&self) -> Option<&Commit<'repo>> {
self.cast(ObjectType::Commit)
}
/// Attempt to view this object as a tag.
///
/// Returns `None` if the object is not actually a tag.
pub fn as_tag(&self) -> Option<&Tag<'repo>> {
self.cast(ObjectType::Tag)
}
/// Attempt to view this object as a tree.
///
/// Returns `None` if the object is not actually a tree.
pub fn as_tree(&self) -> Option<&Tree<'repo>> {
self.cast(ObjectType::Tree)
}
/// Attempt to view this object as a blob.
///
/// Returns `None` if the object is not actually a blob.
pub fn as_blob(&self) -> Option<&Blob<'repo>> {
self.cast(ObjectType::Blob)
}
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
}
}
}
impl<'repo> Clone for Object<'repo> {
fn clone(&self) -> Object<'repo> {
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,
marker: marker::ContravariantLifetime,
}
}
}
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::ContravariantLifetime,
}
}
fn raw(&self) -> *mut raw::git_object { self.raw }
}
#[unsafe_destructor]
impl<'repo> Drop for Object<'repo> {
fn drop(&mut self) {
unsafe { raw::git_object_free(self.raw) }
}
}