Debian Codegraph @

src/tag.rs

raw
1 use std::ffi::CString;
2 use std::marker;
3 use std::mem;
4 use std::ptr;
5 use std::str;
6
7 use crate::util::Binding;
8 use crate::{call, raw, signature, Error, Object, ObjectType, Oid, Signature};
9
10 /// A structure to represent a git [tag][1]
11 ///
12 /// [1]: http://git-scm.com/book/en/Git-Basics-Tagging
13 pub struct Tag<'repo> {
14 raw: *mut raw::git_tag,
15 _marker: marker::PhantomData<Object<'repo>>,
16 }
17
18 impl<'repo> Tag<'repo> {
19 /// Determine whether a tag name is valid, meaning that (when prefixed with refs/tags/) that
20 /// it is a valid reference name, and that any additional tag name restrictions are imposed
21 /// (eg, it cannot start with a -).
22 pub fn is_valid_name(tag_name: &str) -> bool {
23 crate::init();
24 let tag_name = CString::new(tag_name).unwrap();
25 let mut valid: libc::c_int = 0;
26 unsafe {
27 call::c_try(raw::git_tag_name_is_valid(&mut valid, tag_name.as_ptr())).unwrap();
28 }
29 valid == 1
30 }
31
32 /// Get the id (SHA1) of a repository tag
33 pub fn id(&self) -> Oid {
34 unsafe { Binding::from_raw(raw::git_tag_id(&*self.raw)) }
35 }
36
37 /// Get the message of a tag
38 ///
39 /// Returns Ok(None) if there is no message
40 pub fn message(&self) -> Result<Option<&str>, Error> {
41 match self.message_bytes() {
42 Some(mb) => str::from_utf8(mb).map(|s| Some(s)).map_err(|e| e.into()),
43 None => Ok(None),
44 }
45 }
46
47 /// Get the message of a tag
48 ///
49 /// Returns None if there is no message
50 pub fn message_bytes(&self) -> Option<&[u8]> {
51 unsafe { crate::opt_bytes(self, raw::git_tag_message(&*self.raw)) }
52 }
53
54 /// Get the name of a tag
55 pub fn name(&self) -> Result<&str, Error> {
56 str::from_utf8(self.name_bytes()).map_err(|e| e.into())
57 }
58
59 /// Get the name of a tag
60 pub fn name_bytes(&self) -> &[u8] {
61 unsafe { crate::opt_bytes(self, raw::git_tag_name(&*self.raw)).unwrap() }
62 }
63
64 /// Recursively peel a tag until a non tag git_object is found
65 pub fn peel(&self) -> Result<Object<'repo>, Error> {
66 let mut ret = ptr::null_mut();
67 unsafe {
68 try_call!(raw::git_tag_peel(&mut ret, &*self.raw));
69 Ok(Binding::from_raw(ret))
70 }
71 }
72
73 /// Get the tagger (author) of a tag
74 ///
75 /// If the author is unspecified, then `None` is returned.
76 pub fn tagger(&self) -> Option<Signature<'_>> {
77 unsafe {
78 let ptr = raw::git_tag_tagger(&*self.raw);
79 if ptr.is_null() {
80 None
81 } else {
82 Some(signature::from_raw_const(self, ptr))
83 }
84 }
85 }
86
87 /// Get the tagged object of a tag
88 ///
89 /// This method performs a repository lookup for the given object and
90 /// returns it
91 pub fn target(&self) -> Result<Object<'repo>, Error> {
92 let mut ret = ptr::null_mut();
93 unsafe {
94 try_call!(raw::git_tag_target(&mut ret, &*self.raw));
95 Ok(Binding::from_raw(ret))
96 }
97 }
98
99 /// Get the OID of the tagged object of a tag
100 pub fn target_id(&self) -> Oid {
101 unsafe { Binding::from_raw(raw::git_tag_target_id(&*self.raw)) }
102 }
103
104 /// Get the ObjectType of the tagged object of a tag
105 pub fn target_type(&self) -> Option<ObjectType> {
106 unsafe { ObjectType::from_raw(raw::git_tag_target_type(&*self.raw)) }
107 }
108
109 /// Casts this Tag to be usable as an `Object`
110 pub fn as_object(&self) -> &Object<'repo> {
111 unsafe { &*(self as *const _ as *const Object<'repo>) }
112 }
113
114 /// Consumes Tag to be returned as an `Object`
115 pub fn into_object(self) -> Object<'repo> {
116 assert_eq!(mem::size_of_val(&self), mem::size_of::<Object<'_>>());
117 unsafe { mem::transmute(self) }
118 }
119 }
120
121 impl<'repo> std::fmt::Debug for Tag<'repo> {
122 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
123 let mut ds = f.debug_struct("Tag");
124 if let Ok(name) = self.name() {
125 ds.field("name", &name);
126 }
127 ds.field("id", &self.id());
128 ds.finish()
129 }
130 }
131
132 impl<'repo> Binding for Tag<'repo> {
133 type Raw = *mut raw::git_tag;
134 unsafe fn from_raw(raw: *mut raw::git_tag) -> Tag<'repo> {
135 Tag {
136 raw,
137 _marker: marker::PhantomData,
138 }
139 }
140 fn raw(&self) -> *mut raw::git_tag {
141 self.raw
142 }
143 }
144
145 impl<'repo> Clone for Tag<'repo> {
146 fn clone(&self) -> Self {
147 self.as_object().clone().into_tag().ok().unwrap()
148 }
149 }
150
151 impl<'repo> Drop for Tag<'repo> {
152 fn drop(&mut self) {
153 unsafe { raw::git_tag_free(self.raw) }
154 }
155 }
156
157 #[cfg(test)]
158 mod tests {
159 use crate::Tag;
160
161 // Reference -- https://git-scm.com/docs/git-check-ref-format
162 #[test]
163 fn name_is_valid() {
164 assert_eq!(Tag::is_valid_name("blah_blah"), true);
165 assert_eq!(Tag::is_valid_name("v1.2.3"), true);
166 assert_eq!(Tag::is_valid_name("my/tag"), true);
167 assert_eq!(Tag::is_valid_name("@"), true);
168
169 assert_eq!(Tag::is_valid_name("-foo"), false);
170 assert_eq!(Tag::is_valid_name("foo:bar"), false);
171 assert_eq!(Tag::is_valid_name("foo^bar"), false);
172 assert_eq!(Tag::is_valid_name("foo."), false);
173 assert_eq!(Tag::is_valid_name("@{"), false);
174 assert_eq!(Tag::is_valid_name("as\\cd"), false);
175 }
176
177 #[test]
178 #[should_panic]
179 fn is_valid_name_for_invalid_tag() {
180 Tag::is_valid_name("ab\012");
181 }
182
183 #[test]
184 fn smoke() {
185 let (_td, repo) = crate::test::repo_init();
186 let head = repo.head().unwrap();
187 let id = head.target().unwrap();
188 assert!(repo.find_tag(id).is_err());
189
190 let obj = repo.find_object(id, None).unwrap();
191 let sig = repo.signature().unwrap();
192 let tag_id = repo.tag("foo", &obj, &sig, "msg", false).unwrap();
193 let tag = repo.find_tag(tag_id).unwrap();
194 assert_eq!(tag.id(), tag_id);
195
196 let tags = repo.tag_names(None).unwrap();
197 assert_eq!(tags.len(), 1);
198 assert_eq!(tags.get(0), Ok(Some("foo")));
199
200 assert_eq!(tag.name(), Ok("foo"));
201 assert_eq!(tag.message(), Ok(Some("msg")));
202 assert_eq!(tag.peel().unwrap().id(), obj.id());
203 assert_eq!(tag.target_id(), obj.id());
204 assert_eq!(tag.target_type(), Some(crate::ObjectType::Commit));
205
206 assert_eq!(tag.tagger().unwrap().name(), sig.name());
207 tag.target().unwrap();
208 tag.into_object();
209
210 repo.find_object(tag_id, None).unwrap().as_tag().unwrap();
211 repo.find_object(tag_id, None)
212 .unwrap()
213 .into_tag()
214 .ok()
215 .unwrap();
216
217 repo.tag_delete("foo").unwrap();
218 }
219
220 #[test]
221 fn lite() {
222 let (_td, repo) = crate::test::repo_init();
223 let head = t!(repo.head());
224 let id = head.target().unwrap();
225 let obj = t!(repo.find_object(id, None));
226 let tag_id = t!(repo.tag_lightweight("foo", &obj, false));
227 assert!(repo.find_tag(tag_id).is_err());
228 assert_eq!(t!(repo.refname_to_id("refs/tags/foo")), id);
229
230 let tags = t!(repo.tag_names(Some("f*")));
231 assert_eq!(tags.len(), 1);
232 let tags = t!(repo.tag_names(Some("b*")));
233 assert_eq!(tags.len(), 0);
234 }
235 }