use crate::{bstr::BStr, Commit, DetachedObject, Tree}; mod error { use crate::object; #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error(transparent)] FindExistingObject(#[from] object::find::existing::OdbError), #[error("The commit could not be decoded fully or partially")] Decode(#[from] git_object::decode::Error), #[error("Expected object of type {}, but got {}", .expected, .actual)] ObjectKind { expected: git_object::Kind, actual: git_object::Kind, }, } } use crate::id::Ancestors; pub use error::Error; impl<'repo> Commit<'repo> { /// Create an owned instance of this object, copying our data in the process. pub fn detached(&self) -> DetachedObject { DetachedObject { id: self.id, kind: git_object::Kind::Commit, data: self.data.clone(), } } /// Sever the connection to the `Repository` and turn this instance into a standalone object. pub fn detach(self) -> DetachedObject { self.into() } } impl<'repo> Commit<'repo> { /// Turn this objects id into a shortened id with a length in hex as configured by `core.abbrev`. pub fn short_id(&self) -> Result { use crate::ext::ObjectIdExt; self.id.attach(self.repo).shorten() } /// Parse the commits message into a [`MessageRef`][git_object::commit::MessageRef] pub fn message(&self) -> Result, git_object::decode::Error> { Ok(git_object::commit::MessageRef::from_bytes(self.message_raw()?)) } /// Decode the entire commit object in full and return the raw message bytes. pub fn message_raw(&self) -> Result<&'_ BStr, git_object::decode::Error> { git_object::CommitRefIter::from_bytes(&self.data).message() } /// Decode the commit and obtain the time at which the commit was created. /// /// For the time at which it was authored, refer to `.decode()?.author.time`. pub fn time(&self) -> Result { Ok(self.committer()?.time) } /// Decode the entire commit object and return it for accessing all commit information. /// /// It will allocate only if there are more than 2 parents. /// /// Note that the returned commit object does make lookup easy and should be /// used for successive calls to string-ish information to avoid decoding the object /// more than once. pub fn decode(&self) -> Result, git_object::decode::Error> { git_object::CommitRef::from_bytes(&self.data) } /// Return an iterator over tokens, representing this commit piece by piece. pub fn iter(&self) -> git_object::CommitRefIter<'_> { git_object::CommitRefIter::from_bytes(&self.data) } /// Return the commits author, with surrounding whitespace trimmed. pub fn author(&self) -> Result, git_object::decode::Error> { git_object::CommitRefIter::from_bytes(&self.data) .author() .map(|s| s.trim()) } /// Return the commits committer. with surrounding whitespace trimmed. pub fn committer(&self) -> Result, git_object::decode::Error> { git_object::CommitRefIter::from_bytes(&self.data) .committer() .map(|s| s.trim()) } /// Decode this commits parent ids on the fly without allocating. // TODO: tests pub fn parent_ids(&self) -> impl Iterator> + '_ { use crate::ext::ObjectIdExt; let repo = self.repo; git_object::CommitRefIter::from_bytes(&self.data) .parent_ids() .map(move |id| id.attach(repo)) } /// Parse the commit and return the the tree object it points to. pub fn tree(&self) -> Result, Error> { let tree_id = self.tree_id()?; match self.repo.find_object(tree_id)?.try_into_tree() { Ok(tree) => Ok(tree), Err(crate::object::try_into::Error { actual, expected, .. }) => Err(Error::ObjectKind { actual, expected }), } } /// Parse the commit and return the the tree id it points to. pub fn tree_id(&self) -> Result { git_object::CommitRefIter::from_bytes(&self.data).tree_id() } /// Return our id own id with connection to this repository. pub fn id(&self) -> crate::Id<'repo> { use crate::ext::ObjectIdExt; self.id.attach(self.repo) } /// Obtain a platform for traversing ancestors of this commit. pub fn ancestors(&self) -> Ancestors<'repo> { self.id().ancestors() } } impl<'r> std::fmt::Debug for Commit<'r> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Commit({})", self.id) } }