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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::cmp::Ordering;
use std::ffi::CString;
use std::marker;
use std::mem;
use std::str;
use libc;
use {raw, Error, Oid, Repository, Object, ObjectType};
use util::Binding;
struct Refdb<'repo>(&'repo Repository);
pub struct Reference<'repo> {
raw: *mut raw::git_reference,
_marker: marker::PhantomData<Refdb<'repo>>,
}
pub struct References<'repo> {
raw: *mut raw::git_reference_iterator,
_marker: marker::PhantomData<Refdb<'repo>>,
}
pub struct ReferenceNames<'repo> {
inner: References<'repo>,
}
impl<'repo> Reference<'repo> {
pub fn is_valid_name(refname: &str) -> bool {
::init();
let refname = CString::new(refname).unwrap();
unsafe { raw::git_reference_is_valid_name(refname.as_ptr()) == 1 }
}
pub fn raw(&self) -> *mut raw::git_reference { self.raw }
pub fn delete(&mut self) -> Result<(), Error> {
unsafe { try_call!(raw::git_reference_delete(self.raw)); }
Ok(())
}
pub fn is_branch(&self) -> bool {
unsafe { raw::git_reference_is_branch(&*self.raw) == 1 }
}
pub fn is_note(&self) -> bool {
unsafe { raw::git_reference_is_note(&*self.raw) == 1 }
}
pub fn is_remote(&self) -> bool {
unsafe { raw::git_reference_is_remote(&*self.raw) == 1 }
}
pub fn is_tag(&self) -> bool {
unsafe { raw::git_reference_is_tag(&*self.raw) == 1 }
}
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_reference_name(&*self.raw)).unwrap() }
}
pub fn shorthand(&self) -> Option<&str> {
str::from_utf8(self.shorthand_bytes()).ok()
}
pub fn shorthand_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_reference_shorthand(&*self.raw)).unwrap()
}
}
pub fn target(&self) -> Option<Oid> {
unsafe {
Binding::from_raw_opt(raw::git_reference_target(&*self.raw))
}
}
pub fn target_peel(&self) -> Option<Oid> {
unsafe {
Binding::from_raw_opt(raw::git_reference_target_peel(&*self.raw))
}
}
pub fn symbolic_target(&self) -> Option<&str> {
self.symbolic_target_bytes().and_then(|s| str::from_utf8(s).ok())
}
pub fn symbolic_target_bytes(&self) -> Option<&[u8]> {
unsafe { ::opt_bytes(self, raw::git_reference_symbolic_target(&*self.raw)) }
}
pub fn resolve(&self) -> Result<Reference<'repo>, Error> {
let mut raw = 0 as *mut raw::git_reference;
unsafe {
try_call!(raw::git_reference_resolve(&mut raw, &*self.raw));
Ok(Binding::from_raw(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_reference_peel(&mut raw, self.raw, kind));
Ok(Binding::from_raw(raw))
}
}
pub fn rename(&mut self, new_name: &str, force: bool,
msg: &str) -> Result<Reference<'repo>, Error> {
let mut raw = 0 as *mut raw::git_reference;
let new_name = try!(CString::new(new_name));
let msg = try!(CString::new(msg));
unsafe {
try_call!(raw::git_reference_rename(&mut raw, self.raw, new_name,
force, msg));
Ok(Binding::from_raw(raw))
}
}
pub fn set_target(&mut self, id: Oid, reflog_msg: &str)
-> Result<Reference<'repo>, Error> {
let mut raw = 0 as *mut raw::git_reference;
let msg = try!(CString::new(reflog_msg));
unsafe {
try_call!(raw::git_reference_set_target(&mut raw, self.raw,
id.raw(), msg));
Ok(Binding::from_raw(raw))
}
}
}
impl<'repo> PartialOrd for Reference<'repo> {
fn partial_cmp(&self, other: &Reference<'repo>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<'repo> Ord for Reference<'repo> {
fn cmp(&self, other: &Reference<'repo>) -> Ordering {
match unsafe { raw::git_reference_cmp(&*self.raw, &*other.raw) } {
0 => Ordering::Equal,
n if n < 0 => Ordering::Less,
_ => Ordering::Greater,
}
}
}
impl<'repo> PartialEq for Reference<'repo> {
fn eq(&self, other: &Reference<'repo>) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl<'repo> Eq for Reference<'repo> {}
impl<'repo> Binding for Reference<'repo> {
type Raw = *mut raw::git_reference;
unsafe fn from_raw(raw: *mut raw::git_reference) -> Reference<'repo> {
Reference { raw: raw, _marker: marker::PhantomData }
}
fn raw(&self) -> *mut raw::git_reference { self.raw }
}
impl<'repo> Drop for Reference<'repo> {
fn drop(&mut self) {
unsafe { raw::git_reference_free(self.raw) }
}
}
impl<'repo> References<'repo> {
pub fn names(self) -> ReferenceNames<'repo> {
ReferenceNames { inner: self }
}
}
impl<'repo> Binding for References<'repo> {
type Raw = *mut raw::git_reference_iterator;
unsafe fn from_raw(raw: *mut raw::git_reference_iterator)
-> References<'repo> {
References { raw: raw, _marker: marker::PhantomData }
}
fn raw(&self) -> *mut raw::git_reference_iterator { self.raw }
}
impl<'repo> Iterator for References<'repo> {
type Item = Result<Reference<'repo>, Error>;
fn next(&mut self) -> Option<Result<Reference<'repo>, Error>> {
let mut out = 0 as *mut raw::git_reference;
unsafe {
try_call_iter!(raw::git_reference_next(&mut out, self.raw));
Some(Ok(Binding::from_raw(out)))
}
}
}
impl<'repo> Drop for References<'repo> {
fn drop(&mut self) {
unsafe { raw::git_reference_iterator_free(self.raw) }
}
}
impl<'repo> Iterator for ReferenceNames<'repo> {
type Item = Result<&'repo str, Error>;
fn next(&mut self) -> Option<Result<&'repo str, Error>> {
let mut out = 0 as *const libc::c_char;
unsafe {
try_call_iter!(raw::git_reference_next_name(&mut out,
self.inner.raw));
let bytes = ::opt_bytes(self, out).unwrap();
let s = str::from_utf8(bytes).unwrap();
Some(Ok(mem::transmute::<&str, &'repo str>(s)))
}
}
}
#[cfg(test)]
mod tests {
use {Reference, ObjectType};
#[test]
fn smoke() {
assert!(Reference::is_valid_name("refs/foo"));
assert!(!Reference::is_valid_name("foo"));
}
#[test]
fn smoke2() {
let (_td, repo) = ::test::repo_init();
let mut head = repo.head().unwrap();
assert!(head.is_branch());
assert!(!head.is_remote());
assert!(!head.is_tag());
assert!(!head.is_note());
assert!(head == repo.head().unwrap());
assert_eq!(head.name(), Some("refs/heads/master"));
assert!(head == repo.find_reference("refs/heads/master").unwrap());
assert_eq!(repo.refname_to_id("refs/heads/master").unwrap(),
head.target().unwrap());
assert!(head.symbolic_target().is_none());
assert!(head.target_peel().is_none());
assert_eq!(head.shorthand(), Some("master"));
assert!(head.resolve().unwrap() == head);
let mut tag1 = repo.reference("refs/tags/tag1",
head.target().unwrap(),
false, "test").unwrap();
assert!(tag1.is_tag());
let peeled_commit = tag1.peel(ObjectType::Commit).unwrap();
assert_eq!(ObjectType::Commit, peeled_commit.kind().unwrap());
assert_eq!(tag1.target().unwrap(), peeled_commit.id());
tag1.delete().unwrap();
let mut sym1 = repo.reference_symbolic("refs/tags/tag1",
"refs/heads/master", false,
"test").unwrap();
sym1.delete().unwrap();
{
assert!(repo.references().unwrap().count() == 1);
assert!(repo.references().unwrap().next().unwrap().unwrap() == head);
let mut names = repo.references().unwrap().names();
assert_eq!(names.next().unwrap().unwrap(), "refs/heads/master");
assert!(names.next().is_none());
assert!(repo.references_glob("foo").unwrap().count() == 0);
assert!(repo.references_glob("refs/heads/*").unwrap().count() == 1);
}
let mut head = head.rename("refs/foo", true, "test").unwrap();
head.delete().unwrap();
}
}