Skip to content

Commit 5f1d3b1

Browse files
cpickChris Pick
andauthored
Add bindings for git_refspec_transform() and git_refspec_rtransform() (#584)
Co-authored-by: Chris Pick <[email protected]>
1 parent 4015887 commit 5f1d3b1

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

libgit2-sys/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,16 @@ extern "C" {
20592059
pub fn git_refspec_src_matches(spec: *const git_refspec, refname: *const c_char) -> c_int;
20602060
pub fn git_refspec_force(spec: *const git_refspec) -> c_int;
20612061
pub fn git_refspec_string(spec: *const git_refspec) -> *const c_char;
2062+
pub fn git_refspec_transform(
2063+
out: *mut git_buf,
2064+
spec: *const git_refspec,
2065+
name: *const c_char,
2066+
) -> c_int;
2067+
pub fn git_refspec_rtransform(
2068+
out: *mut git_buf,
2069+
spec: *const git_refspec,
2070+
name: *const c_char,
2071+
) -> c_int;
20622072

20632073
// strarray
20642074
pub fn git_strarray_free(array: *mut git_strarray);

src/refspec.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::marker;
33
use std::str;
44

55
use crate::util::Binding;
6-
use crate::{raw, Direction};
6+
use crate::{raw, Buf, Direction, Error};
77

88
/// A structure to represent a git [refspec][1].
99
///
@@ -77,6 +77,34 @@ impl<'remote> Refspec<'remote> {
7777
pub fn bytes(&self) -> &[u8] {
7878
unsafe { crate::opt_bytes(self, raw::git_refspec_string(self.raw)).unwrap() }
7979
}
80+
81+
/// Transform a reference to its target following the refspec's rules
82+
pub fn transform(&self, name: &str) -> Result<Buf, Error> {
83+
let name = CString::new(name).unwrap();
84+
unsafe {
85+
let buf = Buf::new();
86+
try_call!(raw::git_refspec_transform(
87+
buf.raw(),
88+
self.raw,
89+
name.as_ptr()
90+
));
91+
Ok(buf)
92+
}
93+
}
94+
95+
/// Transform a target reference to its source reference following the refspec's rules
96+
pub fn rtransform(&self, name: &str) -> Result<Buf, Error> {
97+
let name = CString::new(name).unwrap();
98+
unsafe {
99+
let buf = Buf::new();
100+
try_call!(raw::git_refspec_rtransform(
101+
buf.raw(),
102+
self.raw,
103+
name.as_ptr()
104+
));
105+
Ok(buf)
106+
}
107+
}
80108
}
81109

82110
impl<'remote> Binding for Refspec<'remote> {

0 commit comments

Comments
 (0)