Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for git_refspec_transform() and git_refspec_rtransform() #584

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,16 @@ extern "C" {
pub fn git_refspec_src_matches(spec: *const git_refspec, refname: *const c_char) -> c_int;
pub fn git_refspec_force(spec: *const git_refspec) -> c_int;
pub fn git_refspec_string(spec: *const git_refspec) -> *const c_char;
pub fn git_refspec_transform(
out: *mut git_buf,
spec: *const git_refspec,
name: *const c_char,
) -> c_int;
pub fn git_refspec_rtransform(
out: *mut git_buf,
spec: *const git_refspec,
name: *const c_char,
) -> c_int;

// strarray
pub fn git_strarray_free(array: *mut git_strarray);
Expand Down
30 changes: 29 additions & 1 deletion src/refspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::marker;
use std::str;

use crate::util::Binding;
use crate::{raw, Direction};
use crate::{raw, Buf, Direction, Error};

/// A structure to represent a git [refspec][1].
///
Expand Down Expand Up @@ -77,6 +77,34 @@ impl<'remote> Refspec<'remote> {
pub fn bytes(&self) -> &[u8] {
unsafe { crate::opt_bytes(self, raw::git_refspec_string(self.raw)).unwrap() }
}

/// Transform a reference to its target following the refspec's rules
pub fn transform(&self, name: &str) -> Result<Buf, Error> {
let name = CString::new(name).unwrap();
unsafe {
let buf = Buf::new();
try_call!(raw::git_refspec_transform(
buf.raw(),
self.raw,
name.as_ptr()
));
Ok(buf)
}
}

/// Transform a target reference to its source reference following the refspec's rules
pub fn rtransform(&self, name: &str) -> Result<Buf, Error> {
let name = CString::new(name).unwrap();
unsafe {
let buf = Buf::new();
try_call!(raw::git_refspec_rtransform(
buf.raw(),
self.raw,
name.as_ptr()
));
Ok(buf)
}
}
}

impl<'remote> Binding for Refspec<'remote> {
Expand Down