use std::ffi::CString;
use std::marker;
use std::str;
use libc;
use {raw, Error, Reference, Signature, BranchType};
use util::Binding;
pub struct Branch<'repo> {
inner: Reference<'repo>,
}
pub struct Branches<'repo> {
raw: *mut raw::git_branch_iterator,
marker: marker::ContravariantLifetime<'repo>,
}
impl<'repo> Branch<'repo> {
pub fn wrap(reference: Reference) -> Branch { Branch { inner: reference } }
pub fn get(&self) -> &Reference<'repo> { &self.inner }
pub fn into_reference(self) -> Reference<'repo> { self.inner }
pub fn delete(&mut self) -> Result<(), Error> {
unsafe { try_call!(raw::git_branch_delete(self.get().raw())); }
Ok(())
}
pub fn is_head(&self) -> bool {
unsafe { raw::git_branch_is_head(&*self.get().raw()) == 1 }
}
pub fn rename(&mut self, new_branch_name: &str, force: bool,
signature: Option<&Signature>,
log_message: &str) -> Result<Branch<'repo>, Error> {
let mut ret = 0 as *mut raw::git_reference;
let new_branch_name = CString::from_slice(new_branch_name.as_bytes());
let log_message = CString::from_slice(log_message.as_bytes());
unsafe {
try_call!(raw::git_branch_move(&mut ret, self.get().raw(),
new_branch_name, force,
signature.map(|s| s.raw()),
log_message));
Ok(Branch::wrap(Binding::from_raw(ret)))
}
}
pub fn name(&self) -> Result<Option<&str>, Error> {
self.name_bytes().map(|s| str::from_utf8(s).ok())
}
pub fn name_bytes(&self) -> Result<&[u8], Error> {
let mut ret = 0 as *const libc::c_char;
unsafe {
try_call!(raw::git_branch_name(&mut ret, &*self.get().raw()));
Ok(::opt_bytes(self, ret).unwrap())
}
}
pub fn upstream<'a>(&'a self) -> Result<Branch<'a>, Error> {
let mut ret = 0 as *mut raw::git_reference;
unsafe {
try_call!(raw::git_branch_upstream(&mut ret, &*self.get().raw()));
Ok(Branch::wrap(Binding::from_raw(ret)))
}
}
pub fn set_upstream(&mut self,
upstream_name: Option<&str>) -> Result<(), Error> {
let upstream_name = upstream_name.map(|s| CString::from_slice(s.as_bytes()));
unsafe {
try_call!(raw::git_branch_set_upstream(self.get().raw(),
upstream_name));
Ok(())
}
}
}
impl<'repo> Branches<'repo> {
pub unsafe fn from_raw(raw: *mut raw::git_branch_iterator)
-> Branches<'repo> {
Branches {
raw: raw,
marker: marker::ContravariantLifetime,
}
}
}
impl<'repo> Iterator for Branches<'repo> {
type Item = (Branch<'repo>, BranchType);
fn next(&mut self) -> Option<(Branch<'repo>, BranchType)> {
let mut ret = 0 as *mut raw::git_reference;
let mut typ = raw::GIT_BRANCH_LOCAL;
unsafe {
let rc = raw::git_branch_next(&mut ret, &mut typ, self.raw);
if rc == raw::GIT_ITEROVER as libc::c_int {
return None
}
assert_eq!(rc, 0);
let typ = match typ {
raw::GIT_BRANCH_LOCAL => BranchType::Local,
raw::GIT_BRANCH_REMOTE => BranchType::Remote,
raw::GIT_BRANCH_ALL => panic!("unexected branch type"),
};
Some((Branch::wrap(Binding::from_raw(ret)), typ))
}
}
}
#[unsafe_destructor]
impl<'repo> Drop for Branches<'repo> {
fn drop(&mut self) {
unsafe { raw::git_branch_iterator_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
use BranchType;
#[test]
fn smoke() {
let (_td, repo) = ::test::repo_init();
let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
let sig = repo.signature().unwrap();
let mut b1 = repo.branch("foo", &commit, false, None, Some("bar")).unwrap();
assert!(!b1.is_head());
repo.branch("foo2", &commit, false, None, None).unwrap();
assert_eq!(repo.branches(None).unwrap().count(), 3);
repo.find_branch("foo", BranchType::Local).unwrap();
let mut b1 = b1.rename("bar", false, Some(&sig), "bar2").unwrap();
assert_eq!(b1.name().unwrap(), Some("bar"));
assert!(b1.upstream().is_err());
b1.set_upstream(Some("master")).unwrap();
b1.upstream().unwrap();
b1.set_upstream(None).unwrap();
b1.delete().unwrap();
}
}