| 1 |
use std::ffi::CString; |
| 2 |
use std::marker; |
| 3 |
use std::str; |
| 4 |
|
| 5 |
use crate::util::Binding; |
| 6 |
use crate::{raw, Buf, Direction, Error}; |
| 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 |
pub fn dst(&self) -> Result<&str, Error> { |
| 30 |
str::from_utf8(self.dst_bytes()).map_err(|e| e.into()) |
| 31 |
} |
| 32 |
|
| 33 |
/// Get the destination specifier, in bytes. |
| 34 |
pub fn dst_bytes(&self) -> &[u8] { |
| 35 |
unsafe { crate::opt_bytes(self, raw::git_refspec_dst(self.raw)).unwrap() } |
| 36 |
} |
| 37 |
|
| 38 |
/// Check if a refspec's destination descriptor matches a reference |
| 39 |
pub fn dst_matches(&self, refname: &str) -> bool { |
| 40 |
let refname = CString::new(refname).unwrap(); |
| 41 |
unsafe { raw::git_refspec_dst_matches(self.raw, refname.as_ptr()) == 1 } |
| 42 |
} |
| 43 |
|
| 44 |
/// Get the source specifier. |
| 45 |
pub fn src(&self) -> Result<&str, Error> { |
| 46 |
str::from_utf8(self.src_bytes()).map_err(|e| e.into()) |
| 47 |
} |
| 48 |
|
| 49 |
/// Get the source specifier, in bytes. |
| 50 |
pub fn src_bytes(&self) -> &[u8] { |
| 51 |
unsafe { crate::opt_bytes(self, raw::git_refspec_src(self.raw)).unwrap() } |
| 52 |
} |
| 53 |
|
| 54 |
/// Check if a refspec's source descriptor matches a reference |
| 55 |
pub fn src_matches(&self, refname: &str) -> bool { |
| 56 |
let refname = CString::new(refname).unwrap(); |
| 57 |
unsafe { raw::git_refspec_src_matches(self.raw, refname.as_ptr()) == 1 } |
| 58 |
} |
| 59 |
|
| 60 |
/// Get the force update setting. |
| 61 |
pub fn is_force(&self) -> bool { |
| 62 |
unsafe { raw::git_refspec_force(self.raw) == 1 } |
| 63 |
} |
| 64 |
|
| 65 |
/// Get the refspec's string. |
| 66 |
pub fn str(&self) -> Result<&str, Error> { |
| 67 |
str::from_utf8(self.bytes()).map_err(|e| e.into()) |
| 68 |
} |
| 69 |
|
| 70 |
/// Get the refspec's string as a byte array |
| 71 |
pub fn bytes(&self) -> &[u8] { |
| 72 |
unsafe { crate::opt_bytes(self, raw::git_refspec_string(self.raw)).unwrap() } |
| 73 |
} |
| 74 |
|
| 75 |
/// Transform a reference to its target following the refspec's rules |
| 76 |
pub fn transform(&self, name: &str) -> Result<Buf, Error> { |
| 77 |
let name = CString::new(name).unwrap(); |
| 78 |
unsafe { |
| 79 |
let buf = Buf::new(); |
| 80 |
try_call!(raw::git_refspec_transform( |
| 81 |
buf.raw(), |
| 82 |
self.raw, |
| 83 |
name.as_ptr() |
| 84 |
)); |
| 85 |
Ok(buf) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/// Transform a target reference to its source reference following the refspec's rules |
| 90 |
pub fn rtransform(&self, name: &str) -> Result<Buf, Error> { |
| 91 |
let name = CString::new(name).unwrap(); |
| 92 |
unsafe { |
| 93 |
let buf = Buf::new(); |
| 94 |
try_call!(raw::git_refspec_rtransform( |
| 95 |
buf.raw(), |
| 96 |
self.raw, |
| 97 |
name.as_ptr() |
| 98 |
)); |
| 99 |
Ok(buf) |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl<'remote> Binding for Refspec<'remote> { |
| 105 |
type Raw = *const raw::git_refspec; |
| 106 |
|
| 107 |
unsafe fn from_raw(raw: *const raw::git_refspec) -> Refspec<'remote> { |
| 108 |
Refspec { |
| 109 |
raw, |
| 110 |
_marker: marker::PhantomData, |
| 111 |
} |
| 112 |
} |
| 113 |
fn raw(&self) -> *const raw::git_refspec { |
| 114 |
self.raw |
| 115 |
} |
| 116 |
} |