8 /// A structure to represent a git [refspec][1].
10 /// Refspecs are currently mainly accessed/created through a `Remote`.
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
>,
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
),
28 /// Get the destination specifier.
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()
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() }
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 }
46 /// Get the source specifier.
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()
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() }
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 }
64 /// Get the force update setting.
65 pub fn is_force(&self) -> bool
{
66 unsafe { raw::git_refspec_force(self.raw) == 1 }
69 /// Get the refspec's string.
71 /// Returns None if the string is not valid utf8.
72 pub fn str(&self) -> Option
<&str> {
73 str::from_utf8(self.bytes()).ok()
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() }
82 impl<'remote
> Binding
for Refspec
<'remote
> {
83 type Raw
= *const raw
::git_refspec
;
85 unsafe fn from_raw(raw
: *const raw
::git_refspec
) -> Refspec
<'remote
> {
86 Refspec { raw: raw, _marker: marker::PhantomData }
88 fn raw(&self) -> *const raw
::git_refspec { self.raw }