]> git.proxmox.com Git - cargo.git/blob - vendor/git2-0.6.8/src/refspec.rs
New upstream version 0.23.0
[cargo.git] / vendor / git2-0.6.8 / src / refspec.rs
1 use std::ffi::CString;
2 use std::marker;
3 use std::str;
4
5 use {raw, Direction};
6 use util::Binding;
7
8 /// A structure to represent a git [refspec][1].
9 ///
10 /// Refspecs are currently mainly accessed/created through a `Remote`.
11 ///
12 /// [1]: http://git-scm.com/book/en/Git-Internals-The-Refspec
13 pub struct Refspec<'remote> {
14 raw: *const raw::git_refspec,
15 _marker: marker::PhantomData<&'remote raw::git_remote>,
16 }
17
18 impl<'remote> Refspec<'remote> {
19 /// Get the refspec's direction.
20 pub fn direction(&self) -> Direction {
21 match unsafe { raw::git_refspec_direction(self.raw) } {
22 raw::GIT_DIRECTION_FETCH => Direction::Fetch,
23 raw::GIT_DIRECTION_PUSH => Direction::Push,
24 n => panic!("unknown refspec direction: {}", n),
25 }
26 }
27
28 /// Get the destination specifier.
29 ///
30 /// If the destination is not utf-8, None is returned.
31 pub fn dst(&self) -> Option<&str> {
32 str::from_utf8(self.dst_bytes()).ok()
33 }
34
35 /// Get the destination specifier, in bytes.
36 pub fn dst_bytes(&self) -> &[u8] {
37 unsafe { ::opt_bytes(self, raw::git_refspec_dst(self.raw)).unwrap() }
38 }
39
40 /// Check if a refspec's destination descriptor matches a reference
41 pub fn dst_matches(&self, refname: &str) -> bool {
42 let refname = CString::new(refname).unwrap();
43 unsafe { raw::git_refspec_dst_matches(self.raw, refname.as_ptr()) == 1 }
44 }
45
46 /// Get the source specifier.
47 ///
48 /// If the source is not utf-8, None is returned.
49 pub fn src(&self) -> Option<&str> {
50 str::from_utf8(self.src_bytes()).ok()
51 }
52
53 /// Get the source specifier, in bytes.
54 pub fn src_bytes(&self) -> &[u8] {
55 unsafe { ::opt_bytes(self, raw::git_refspec_src(self.raw)).unwrap() }
56 }
57
58 /// Check if a refspec's source descriptor matches a reference
59 pub fn src_matches(&self, refname: &str) -> bool {
60 let refname = CString::new(refname).unwrap();
61 unsafe { raw::git_refspec_src_matches(self.raw, refname.as_ptr()) == 1 }
62 }
63
64 /// Get the force update setting.
65 pub fn is_force(&self) -> bool {
66 unsafe { raw::git_refspec_force(self.raw) == 1 }
67 }
68
69 /// Get the refspec's string.
70 ///
71 /// Returns None if the string is not valid utf8.
72 pub fn str(&self) -> Option<&str> {
73 str::from_utf8(self.bytes()).ok()
74 }
75
76 /// Get the refspec's string as a byte array
77 pub fn bytes(&self) -> &[u8] {
78 unsafe { ::opt_bytes(self, raw::git_refspec_string(self.raw)).unwrap() }
79 }
80 }
81
82 impl<'remote> Binding for Refspec<'remote> {
83 type Raw = *const raw::git_refspec;
84
85 unsafe fn from_raw(raw: *const raw::git_refspec) -> Refspec<'remote> {
86 Refspec { raw: raw, _marker: marker::PhantomData }
87 }
88 fn raw(&self) -> *const raw::git_refspec { self.raw }
89 }