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
use std::marker;
use std::mem;
use std::str;
use {raw, signature, Error, Oid, Object, Signature, ObjectType};
use util::Binding;
pub struct Tag<'repo> {
raw: *mut raw::git_tag,
_marker: marker::PhantomData<Object<'repo>>,
}
impl<'repo> Tag<'repo> {
pub fn id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_tag_id(&*self.raw)) }
}
pub fn message(&self) -> Option<&str> {
self.message_bytes().and_then(|s| str::from_utf8(s).ok())
}
pub fn message_bytes(&self) -> Option<&[u8]> {
unsafe { ::opt_bytes(self, raw::git_tag_message(&*self.raw)) }
}
pub fn name(&self) -> Option<&str> {
str::from_utf8(self.name_bytes()).ok()
}
pub fn name_bytes(&self) -> &[u8] {
unsafe { ::opt_bytes(self, raw::git_tag_name(&*self.raw)).unwrap() }
}
pub fn peel(&self) -> Result<Object<'repo>, Error> {
let mut ret = 0 as *mut raw::git_object;
unsafe {
try_call!(raw::git_tag_peel(&mut ret, &*self.raw));
Ok(Binding::from_raw(ret))
}
}
pub fn tagger(&self) -> Option<Signature> {
unsafe {
let ptr = raw::git_tag_tagger(&*self.raw);
if ptr.is_null() {
None
} else {
Some(signature::from_raw_const(self, ptr))
}
}
}
pub fn target(&self) -> Result<Object<'repo>, Error> {
let mut ret = 0 as *mut raw::git_object;
unsafe {
try_call!(raw::git_tag_target(&mut ret, &*self.raw));
Ok(Binding::from_raw(ret))
}
}
pub fn target_id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_tag_target_id(&*self.raw)) }
}
pub fn target_type(&self) -> Option<ObjectType> {
unsafe { ObjectType::from_raw(raw::git_tag_target_type(&*self.raw)) }
}
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)
}
}
}
impl<'repo> Binding for Tag<'repo> {
type Raw = *mut raw::git_tag;
unsafe fn from_raw(raw: *mut raw::git_tag) -> Tag<'repo> {
Tag { raw: raw, _marker: marker::PhantomData }
}
fn raw(&self) -> *mut raw::git_tag { self.raw }
}
impl<'repo> Drop for Tag<'repo> {
fn drop(&mut self) {
unsafe { raw::git_tag_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
#[test]
fn smoke() {
let (_td, repo) = ::test::repo_init();
let head = repo.head().unwrap();
let id = head.target().unwrap();
assert!(repo.find_tag(id).is_err());
let obj = repo.find_object(id, None).unwrap();
let sig = repo.signature().unwrap();
let tag_id = repo.tag("foo", &obj, &sig, "msg", false).unwrap();
let tag = repo.find_tag(tag_id).unwrap();
assert_eq!(tag.id(), tag_id);
let tags = repo.tag_names(None).unwrap();
assert_eq!(tags.len(), 1);
assert_eq!(tags.get(0), Some("foo"));
assert_eq!(tag.name(), Some("foo"));
assert_eq!(tag.message(), Some("msg"));
assert_eq!(tag.peel().unwrap().id(), obj.id());
assert_eq!(tag.target_id(), obj.id());
assert_eq!(tag.target_type(), Some(::ObjectType::Commit));
assert_eq!(tag.tagger().unwrap().name(), sig.name());
tag.target().unwrap();
tag.into_object();
repo.find_object(tag_id, None).unwrap().as_tag().unwrap();
repo.find_object(tag_id, None).unwrap().into_tag().ok().unwrap();
repo.tag_delete("foo").unwrap();
}
#[test]
fn lite() {
let (_td, repo) = ::test::repo_init();
let head = t!(repo.head());
let id = head.target().unwrap();
let obj = t!(repo.find_object(id, None));
let tag_id = t!(repo.tag_lightweight("foo", &obj, false));
assert!(repo.find_tag(tag_id).is_err());
assert_eq!(t!(repo.refname_to_id("refs/tags/foo")), id);
let tags = t!(repo.tag_names(Some("f*")));
assert_eq!(tags.len(), 1);
let tags = t!(repo.tag_names(Some("b*")));
assert_eq!(tags.len(), 0);
}
}