#![cfg_attr(
feature = "document-features",
cfg_attr(doc, doc = ::document_features::document_features!())
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![deny(missing_docs, rust_2018_idioms)]
#![forbid(unsafe_code)]
use std::borrow::Cow;
pub use bstr;
use bstr::{BStr, BString, ByteSlice};
use smallvec::SmallVec;
pub mod commit;
mod object;
pub mod tag;
pub mod tree;
mod blob;
pub mod data;
mod traits;
pub use traits::WriteTo;
pub mod encode;
pub(crate) mod parse;
pub mod kind;
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[allow(missing_docs)]
pub enum Kind {
Tree,
Blob,
Commit,
Tag,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct BlobRef<'a> {
pub data: &'a [u8],
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Blob {
pub data: Vec<u8>,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct CommitRef<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
pub tree: &'a BStr,
pub parents: SmallVec<[&'a BStr; 1]>,
pub author: git_actor::SignatureRef<'a>,
pub committer: git_actor::SignatureRef<'a>,
pub encoding: Option<&'a BStr>,
pub message: &'a BStr,
pub extra_headers: Vec<(&'a BStr, Cow<'a, BStr>)>,
}
#[derive(Copy, Clone)]
pub struct CommitRefIter<'a> {
data: &'a [u8],
state: commit::ref_iter::State,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Commit {
pub tree: git_hash::ObjectId,
pub parents: SmallVec<[git_hash::ObjectId; 1]>,
pub author: git_actor::Signature,
pub committer: git_actor::Signature,
pub encoding: Option<BString>,
pub message: BString,
pub extra_headers: Vec<(BString, BString)>,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct TagRef<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
pub target: &'a BStr,
pub target_kind: Kind,
pub name: &'a BStr,
pub tagger: Option<git_actor::SignatureRef<'a>>,
pub message: &'a BStr,
pub pgp_signature: Option<&'a BStr>,
}
#[derive(Copy, Clone)]
pub struct TagRefIter<'a> {
data: &'a [u8],
state: tag::ref_iter::State,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Tag {
pub target: git_hash::ObjectId,
pub target_kind: Kind,
pub name: BString,
pub tagger: Option<git_actor::Signature>,
pub message: BString,
pub pgp_signature: Option<BString>,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum ObjectRef<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
Tree(TreeRef<'a>),
Blob(BlobRef<'a>),
Commit(CommitRef<'a>),
Tag(TagRef<'a>),
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(clippy::large_enum_variant, missing_docs)]
pub enum Object {
Tree(Tree),
Blob(Blob),
Commit(Commit),
Tag(Tag),
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct TreeRef<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
pub entries: Vec<tree::EntryRef<'a>>,
}
#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct TreeRefIter<'a> {
data: &'a [u8],
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Tree {
pub entries: Vec<tree::Entry>,
}
impl Tree {
pub fn empty() -> Self {
Tree { entries: Vec::new() }
}
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct Data<'a> {
pub kind: Kind,
pub data: &'a [u8],
}
pub mod decode {
#[cfg(feature = "verbose-object-parsing-errors")]
mod _decode {
use crate::bstr::{BString, ByteSlice};
pub type ParseError<'a> = nom::error::VerboseError<&'a [u8]>;
pub type ParseErrorOwned = nom::error::VerboseError<BString>;
pub(crate) fn empty_error() -> Error {
Error {
inner: nom::error::VerboseError::<BString> { errors: Vec::new() },
}
}
#[derive(Debug, Clone)]
pub struct Error {
pub inner: ParseErrorOwned,
}
impl<'a> From<nom::Err<ParseError<'a>>> for Error {
fn from(v: nom::Err<ParseError<'a>>) -> Self {
Error {
inner: match v {
nom::Err::Error(err) | nom::Err::Failure(err) => nom::error::VerboseError {
errors: err
.errors
.into_iter()
.map(|(i, v)| (i.as_bstr().to_owned(), v))
.collect(),
},
nom::Err::Incomplete(_) => unreachable!("we don't have streaming parsers"),
},
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
}
#[cfg(not(feature = "verbose-object-parsing-errors"))]
mod _decode {
pub type ParseError<'a> = ();
pub type ParseErrorOwned = ();
pub(crate) fn empty_error() -> Error {
Error { inner: () }
}
#[derive(Debug, Clone)]
pub struct Error {
pub inner: ParseErrorOwned,
}
impl<'a> From<nom::Err<ParseError<'a>>> for Error {
fn from(v: nom::Err<ParseError<'a>>) -> Self {
Error {
inner: match v {
nom::Err::Error(err) | nom::Err::Failure(err) => err,
nom::Err::Incomplete(_) => unreachable!("we don't have streaming parsers"),
},
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}
}
pub(crate) use _decode::empty_error;
pub use _decode::{Error, ParseError, ParseErrorOwned};
impl std::error::Error for Error {}
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum LooseHeaderDecodeError {
#[error("{message}: {number:?}")]
ParseIntegerError {
source: btoi::ParseIntegerError,
message: &'static str,
number: bstr::BString,
},
#[error("{message}")]
InvalidHeader { message: &'static str },
#[error("The object header contained an unknown object kind.")]
ObjectHeader(#[from] super::kind::Error),
}
use bstr::ByteSlice;
pub fn loose_header(input: &[u8]) -> Result<(super::Kind, usize, usize), LooseHeaderDecodeError> {
use LooseHeaderDecodeError::*;
let kind_end = input.find_byte(0x20).ok_or(InvalidHeader {
message: "Expected '<type> <size>'",
})?;
let kind = super::Kind::from_bytes(&input[..kind_end])?;
let size_end = input.find_byte(0x0).ok_or(InvalidHeader {
message: "Did not find 0 byte in header",
})?;
let size_bytes = &input[kind_end + 1..size_end];
let size = btoi::btoi(size_bytes).map_err(|source| ParseIntegerError {
source,
message: "Object size in header could not be parsed",
number: size_bytes.into(),
})?;
Ok((kind, size, size_end + 1))
}
}