use std::marker;
use std::mem;
use std::ops::Range;
use std::ptr;
use std::str;
use libc;
use {raw, signature, Oid, Error, Signature, Tree, Time, Object};
use util::Binding;
pub struct Commit<'repo> {
raw: *mut raw::git_commit,
_marker: marker::PhantomData<Object<'repo>>,
}
pub struct Parents<'commit, 'repo: 'commit> {
range: Range<usize>,
commit: &'commit Commit<'repo>,
}
pub struct ParentIds<'commit> {
range: Range<usize>,
commit: &'commit Commit<'commit>,
}
impl<'repo> Commit<'repo> {
pub fn id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_commit_id(&*self.raw)) }
}
pub fn tree_id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_commit_tree_id(&*self.raw)) }
}
pub fn tree(&self) -> Result<Tree<'repo>, Error> {
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_commit_tree(&mut ret, &*self.raw));
Ok(Binding::from_raw(ret))
}
}
pub fn raw(&self) -> *mut raw::git_commit { self.raw }
pub fn message(&self) -> Option<&str> {
str::from_utf8(self.message_bytes()).ok()
}
pub fn message_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_commit_message(&*self.raw)).unwrap()
}
}
pub fn message_encoding(&self) -> Option<&str> {
let bytes = unsafe {
::opt_bytes(self, raw::git_commit_message(&*self.raw))
};
bytes.map(|b| str::from_utf8(b).unwrap())
}
pub fn message_raw(&self) -> Option<&str> {
str::from_utf8(self.message_raw_bytes()).ok()
}
pub fn message_raw_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_commit_message_raw(&*self.raw)).unwrap()
}
}
pub fn raw_header(&self) -> Option<&str> {
str::from_utf8(self.raw_header_bytes()).ok()
}
pub fn raw_header_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_commit_raw_header(&*self.raw)).unwrap()
}
}
pub fn summary(&self) -> Option<&str> {
self.summary_bytes().and_then(|s| str::from_utf8(s).ok())
}
pub fn summary_bytes(&self) -> Option<&[u8]> {
unsafe { ::opt_bytes(self, raw::git_commit_summary(self.raw)) }
}
pub fn time(&self) -> Time {
unsafe {
Time::new(raw::git_commit_time(&*self.raw) as i64,
raw::git_commit_time_offset(&*self.raw) as i32)
}
}
pub fn parents<'a>(&'a self) -> Parents<'a, 'repo> {
let max = unsafe { raw::git_commit_parentcount(&*self.raw) as usize };
Parents { range: 0..max, commit: self }
}
pub fn parent_ids(&self) -> ParentIds {
let max = unsafe { raw::git_commit_parentcount(&*self.raw) as usize };
ParentIds { range: 0..max, commit: self }
}
pub fn author(&self) -> Signature {
unsafe {
let ptr = raw::git_commit_author(&*self.raw);
signature::from_raw_const(self, ptr)
}
}
pub fn committer(&self) -> Signature {
unsafe {
let ptr = raw::git_commit_committer(&*self.raw);
signature::from_raw_const(self, ptr)
}
}
pub fn amend(&self,
update_ref: Option<&str>,
author: Option<&Signature>,
committer: Option<&Signature>,
message_encoding: Option<&str>,
message: Option<&str>,
tree: Option<&Tree<'repo>>) -> Result<Oid, Error> {
let mut raw = raw::git_oid { id: [0; raw::GIT_OID_RAWSZ] };
let update_ref = try!(::opt_cstr(update_ref));
let encoding = try!(::opt_cstr(message_encoding));
let message = try!(::opt_cstr(message));
unsafe {
try_call!(raw::git_commit_amend(&mut raw,
self.raw(),
update_ref,
author.map(|s| s.raw()),
committer.map(|s| s.raw()),
encoding,
message,
tree.map(|t| t.raw())));
Ok(Binding::from_raw(&raw as *const _))
}
}
pub fn parent(&self, i: usize) -> Result<Commit<'repo>, Error> {
unsafe {
let mut raw = ptr::null_mut();
try_call!(raw::git_commit_parent(&mut raw, &*self.raw,
i as libc::c_uint));
Ok(Binding::from_raw(raw))
}
}
pub fn parent_id(&self, i: usize) -> Result<Oid, Error> {
unsafe {
let id = raw::git_commit_parent_id(self.raw, i as libc::c_uint);
if id.is_null() {
Err(Error::from_str("parent index out of bounds"))
} else {
Ok(Binding::from_raw(id))
}
}
}
pub fn as_object(&self) -> &Object<'repo> {
unsafe {
&*(self as *const _ as *const Object<'repo>)
}
}
pub fn into_object(self) -> Object<'repo> {
assert_eq!(mem::size_of_val(&self), mem::size_of::<Object>());
unsafe {
mem::transmute(self)
}
}
}
impl<'repo> Binding for Commit<'repo> {
type Raw = *mut raw::git_commit;
unsafe fn from_raw(raw: *mut raw::git_commit) -> Commit<'repo> {
Commit {
raw: raw,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_commit { self.raw }
}
impl<'repo> ::std::fmt::Debug for Commit<'repo> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
let mut ds = f.debug_struct("Commit");
ds.field("id", &self.id());
if let Some(summary) = self.summary() {
ds.field("summary", &summary);
}
ds.finish()
}
}
impl<'repo, 'commit> Iterator for Parents<'commit, 'repo> {
type Item = Commit<'repo>;
fn next(&mut self) -> Option<Commit<'repo>> {
self.range.next().map(|i| self.commit.parent(i).unwrap())
}
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
}
impl<'repo, 'commit> DoubleEndedIterator for Parents<'commit, 'repo> {
fn next_back(&mut self) -> Option<Commit<'repo>> {
self.range.next_back().map(|i| self.commit.parent(i).unwrap())
}
}
impl<'repo, 'commit> ExactSizeIterator for Parents<'commit, 'repo> {}
impl<'commit> Iterator for ParentIds<'commit> {
type Item = Oid;
fn next(&mut self) -> Option<Oid> {
self.range.next().map(|i| self.commit.parent_id(i).unwrap())
}
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
}
impl<'commit> DoubleEndedIterator for ParentIds<'commit> {
fn next_back(&mut self) -> Option<Oid> {
self.range.next_back().map(|i| self.commit.parent_id(i).unwrap())
}
}
impl<'commit> ExactSizeIterator for ParentIds<'commit> {}
impl<'repo> Clone for Commit<'repo> {
fn clone(&self) -> Self {
self.as_object().clone().into_commit().ok().unwrap()
}
}
impl<'repo> Drop for Commit<'repo> {
fn drop(&mut self) {
unsafe { raw::git_commit_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
#[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();
assert_eq!(commit.message(), Some("initial"));
assert_eq!(commit.id(), target);
commit.message_raw().unwrap();
commit.raw_header().unwrap();
commit.message_encoding();
commit.summary().unwrap();
commit.tree_id();
commit.tree().unwrap();
assert_eq!(commit.parents().count(), 0);
assert_eq!(commit.author().name(), Some("name"));
assert_eq!(commit.author().email(), Some("email"));
assert_eq!(commit.committer().name(), Some("name"));
assert_eq!(commit.committer().email(), Some("email"));
let sig = repo.signature().unwrap();
let tree = repo.find_tree(commit.tree_id()).unwrap();
let id = repo.commit(Some("HEAD"), &sig, &sig, "bar", &tree,
&[&commit]).unwrap();
let head = repo.find_commit(id).unwrap();
let new_head = head.amend(Some("HEAD"), None, None, None,
Some("new message"), None).unwrap();
let new_head = repo.find_commit(new_head).unwrap();
assert_eq!(new_head.message(), Some("new message"));
new_head.into_object();
repo.find_object(target, None).unwrap().as_commit().unwrap();
repo.find_object(target, None).unwrap().into_commit().ok().unwrap();
}
}