Skip to content

add bindings for git_branch_name_is_valid #715

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

Merged
merged 1 commit into from
Jun 1, 2021
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
1 change: 1 addition & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,7 @@ extern "C" {
force: c_int,
) -> c_int;
pub fn git_branch_name(out: *mut *const c_char, branch: *const git_reference) -> c_int;
pub fn git_branch_name_is_valid(valid: *mut c_int, name: *const c_char) -> c_int;
pub fn git_branch_remote_name(
out: *mut git_buf,
repo: *mut git_repository,
Expand Down
20 changes: 19 additions & 1 deletion src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ impl<'repo> Branch<'repo> {
Branch { inner: reference }
}

/// Ensure the branch name is well-formed.
pub fn name_is_valid(name: &str) -> Result<bool, Error> {
let name = CString::new(name)?;
let mut valid: libc::c_int = 0;
unsafe {
try_call!(raw::git_branch_name_is_valid(&mut valid, name.as_ptr()));
}
Ok(valid == 1)
}

/// Gain access to the reference that is this branch
pub fn get(&self) -> &Reference<'repo> {
&self.inner
Expand Down Expand Up @@ -151,7 +161,7 @@ impl<'repo> Drop for Branches<'repo> {

#[cfg(test)]
mod tests {
use crate::BranchType;
use crate::{Branch, BranchType};

#[test]
fn smoke() {
Expand All @@ -175,4 +185,12 @@ mod tests {

b1.delete().unwrap();
}

#[test]
fn name_is_valid() {
assert!(Branch::name_is_valid("foo").unwrap());
assert!(!Branch::name_is_valid("").unwrap());
assert!(!Branch::name_is_valid("with spaces").unwrap());
assert!(!Branch::name_is_valid("~tilde").unwrap());
}
}