#![allow(clippy::empty_docs)]
use gix_error::ExnMessageResult;
use gix_error::{ErrorExt, ResultExt};
use gix_hash::ObjectId;
pub use gix_object::Kind;
use crate::{Blob, Commit, Error, Id, Object, ObjectDetached, Result, Tag, Tree};
mod errors;
pub(crate) mod cache {
pub use gix_pack::cache::object::MemoryCappedHashmap;
}
pub(crate) use errors::existing_error;
pub mod blob;
pub mod commit;
mod impls;
pub mod peel;
mod tag;
pub mod tree;
impl ObjectDetached {
pub fn attach(self, repo: &crate::Repository) -> Object<'_> {
Object {
id: self.id,
kind: self.kind,
data: self.data,
repo,
}
}
}
impl std::fmt::Debug for ObjectDetached {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use gix_object::Kind::*;
let type_name = match self.kind {
Blob => "Blob",
Commit => "Commit",
Tree => "Tree",
Tag => "Tag",
};
write!(f, "{}({})", type_name, self.id)
}
}
impl<'repo> Object<'repo> {
pub(crate) fn from_data(
id: impl Into<ObjectId>,
kind: Kind,
data: Vec<u8>,
repo: &'repo crate::Repository,
) -> Self {
Object {
id: id.into(),
kind,
data,
repo,
}
}
pub fn into_blob(self) -> Blob<'repo> {
match self.try_into() {
Ok(blob) => blob,
Err(this) => panic!("Tried to use {} as blob, but was {}", this.id, this.kind),
}
}
pub fn into_tree(self) -> Tree<'repo> {
match self.try_into() {
Ok(tree) => tree,
Err(this) => panic!("Tried to use {} as tree, but was {}", this.id, this.kind),
}
}
pub fn into_commit(self) -> Commit<'repo> {
match self.try_into() {
Ok(commit) => commit,
Err(this) => panic!("Tried to use {} as commit, but was {}", this.id, this.kind),
}
}
pub fn into_tag(self) -> Tag<'repo> {
match self.try_into() {
Ok(tag) => tag,
Err(this) => panic!("Tried to use {} as tag, but was {}", this.id, this.kind),
}
}
pub fn try_into_commit(self) -> ExnMessageResult<Commit<'repo>> {
self.try_into().map_err(|this: Self| {
gix_error::validation(format!(
"Object named {} was supposed to be of kind {}, but was kind {}.",
this.id,
gix_object::Kind::Commit,
this.kind
))
.raise()
})
}
pub fn try_into_tag(self) -> ExnMessageResult<Tag<'repo>> {
self.try_into().map_err(|this: Self| {
gix_error::validation(format!(
"Object named {} was supposed to be of kind {}, but was kind {}.",
this.id,
gix_object::Kind::Tag,
this.kind
))
.raise()
})
}
pub fn try_into_tree(self) -> ExnMessageResult<Tree<'repo>> {
self.try_into().map_err(|this: Self| {
gix_error::validation(format!(
"Object named {} was supposed to be of kind {}, but was kind {}.",
this.id,
gix_object::Kind::Tree,
this.kind
))
.raise()
})
}
pub fn try_into_blob(self) -> ExnMessageResult<Blob<'repo>> {
self.try_into().map_err(|this: Self| {
gix_error::validation(format!(
"Object named {} was supposed to be of kind {}, but was kind {}.",
this.id,
gix_object::Kind::Blob,
this.kind
))
.raise()
})
}
}
impl Object<'_> {
pub fn detached(&self) -> ObjectDetached {
ObjectDetached {
id: self.id,
kind: self.kind,
data: self.data.clone(),
}
}
pub fn detach(self) -> ObjectDetached {
self.into()
}
}
impl<'repo> Object<'repo> {
pub fn to_commit_ref(&self) -> gix_object::CommitRef<'_> {
self.try_to_commit_ref().expect("BUG: need a commit")
}
pub fn try_to_commit_ref(&self) -> Result<gix_object::CommitRef<'_>> {
gix_object::Data::new(&self.data, self.kind, self.id.kind())
.decode()
.or_erased()?
.into_commit()
.ok_or_else(|| {
Error::from_error(gix_error::validation(format!(
"Expected object type {}, but got {}",
gix_object::Kind::Commit,
self.kind
)))
})
}
pub fn to_commit_ref_iter(&self) -> gix_object::CommitRefIter<'_> {
gix_object::Data::new(&self.data, self.kind, self.id.kind())
.try_into_commit_iter()
.expect("BUG: This object must be a commit")
}
pub fn try_to_commit_ref_iter(&self) -> Option<gix_object::CommitRefIter<'_>> {
gix_object::Data::new(&self.data, self.kind, self.id.kind()).try_into_commit_iter()
}
pub fn to_tag_ref_iter(&self) -> gix_object::TagRefIter<'_> {
gix_object::Data::new(&self.data, self.kind, self.id.kind())
.try_into_tag_iter()
.expect("BUG: this object must be a tag")
}
pub fn try_to_tag_ref_iter(&self) -> Option<gix_object::TagRefIter<'_>> {
gix_object::Data::new(&self.data, self.kind, self.id.kind()).try_into_tag_iter()
}
pub fn to_tag_ref(&self) -> gix_object::TagRef<'_> {
self.try_to_tag_ref().expect("BUG: need tag")
}
pub fn try_to_tag_ref(&self) -> Result<gix_object::TagRef<'_>> {
gix_object::Data::new(&self.data, self.kind, self.id.kind())
.decode()
.or_erased()?
.into_tag()
.ok_or_else(|| {
Error::from_error(gix_error::validation(format!(
"Expected object type {}, but got {}",
gix_object::Kind::Tag,
self.kind
)))
})
}
pub fn id(&self) -> Id<'repo> {
Id::from_id(self.id, self.repo)
}
}