gix_object/
lib.rs

1//! This crate provides types for [read-only git objects][crate::ObjectRef] backed by bytes provided in git's serialization format
2//! as well as [mutable versions][Object] of these. Both types of objects can be encoded.
3//! ## Feature Flags
4#![cfg_attr(
5    all(doc, feature = "document-features"),
6    doc = ::document_features::document_features!()
7)]
8#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))]
9#![deny(missing_docs, rust_2018_idioms)]
10#![forbid(unsafe_code)]
11
12use std::borrow::Cow;
13
14/// For convenience to allow using `bstr` without adding it to own cargo manifest.
15pub use bstr;
16use bstr::{BStr, BString, ByteSlice};
17/// For convenience to allow using `gix-date` without adding it to own cargo manifest.
18pub use gix_date as date;
19use smallvec::SmallVec;
20
21///
22pub mod commit;
23mod object;
24///
25pub mod tag;
26///
27pub mod tree;
28
29mod blob;
30///
31pub mod data;
32
33///
34pub mod find;
35
36///
37pub mod write {
38    /// The error type returned by the [`Write`](crate::Write) trait.
39    pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
40}
41
42mod traits;
43pub use traits::{Exists, Find, FindExt, FindObjectOrHeader, Header as FindHeader, HeaderExt, Write, WriteTo};
44
45pub mod encode;
46pub(crate) mod parse;
47
48///
49pub mod kind;
50
51/// The four types of objects that git differentiates.
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
54#[allow(missing_docs)]
55pub enum Kind {
56    Tree,
57    Blob,
58    Commit,
59    Tag,
60}
61/// A chunk of any [`data`](BlobRef::data).
62#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
63#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
64pub struct BlobRef<'a> {
65    /// The bytes themselves.
66    pub data: &'a [u8],
67}
68
69/// A mutable chunk of any [`data`](Blob::data).
70#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub struct Blob {
73    /// The data itself.
74    pub data: Vec<u8>,
75}
76
77/// A git commit parsed using [`from_bytes()`](CommitRef::from_bytes()).
78///
79/// A commit encapsulates information about a point in time at which the state of the repository is recorded, usually after a
80/// change which is documented in the commit `message`.
81#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
82#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
83pub struct CommitRef<'a> {
84    /// HEX hash of tree object we point to. Usually 40 bytes long.
85    ///
86    /// Use [`tree()`](CommitRef::tree()) to obtain a decoded version of it.
87    #[cfg_attr(feature = "serde", serde(borrow))]
88    pub tree: &'a BStr,
89    /// HEX hash of each parent commit. Empty for first commit in repository.
90    pub parents: SmallVec<[&'a BStr; 1]>,
91    /// Who wrote this commit. Name and email might contain whitespace and are not trimmed to ensure round-tripping.
92    ///
93    /// Use the [`author()`](CommitRef::author()) method to received a trimmed version of it.
94    pub author: gix_actor::SignatureRef<'a>,
95    /// Who committed this commit. Name and email might contain whitespace and are not trimmed to ensure round-tripping.
96    ///
97    /// Use the [`committer()`](CommitRef::committer()) method to received a trimmed version of it.
98    ///
99    /// This may be different from the `author` in case the author couldn't write to the repository themselves and
100    /// is commonly encountered with contributed commits.
101    pub committer: gix_actor::SignatureRef<'a>,
102    /// The name of the message encoding, otherwise [UTF-8 should be assumed](https://github.com/git/git/blob/e67fbf927dfdf13d0b21dc6ea15dc3c7ef448ea0/commit.c#L1493:L1493).
103    pub encoding: Option<&'a BStr>,
104    /// The commit message documenting the change.
105    pub message: &'a BStr,
106    /// Extra header fields, in order of them being encountered, made accessible with the iterator returned by [`extra_headers()`](CommitRef::extra_headers()).
107    pub extra_headers: Vec<(&'a BStr, Cow<'a, BStr>)>,
108}
109
110/// Like [`CommitRef`], but as `Iterator` to support (up to) entirely allocation free parsing.
111/// It's particularly useful to traverse the commit graph without ever allocating arrays for parents.
112#[derive(Copy, Clone)]
113pub struct CommitRefIter<'a> {
114    data: &'a [u8],
115    state: commit::ref_iter::State,
116}
117
118/// A mutable git commit, representing an annotated state of a working tree along with a reference to its historical commits.
119#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
120#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
121pub struct Commit {
122    /// The hash of recorded working tree state.
123    pub tree: gix_hash::ObjectId,
124    /// Hash of each parent commit. Empty for the first commit in repository.
125    pub parents: SmallVec<[gix_hash::ObjectId; 1]>,
126    /// Who wrote this commit.
127    pub author: gix_actor::Signature,
128    /// Who committed this commit.
129    ///
130    /// This may be different from the `author` in case the author couldn't write to the repository themselves and
131    /// is commonly encountered with contributed commits.
132    pub committer: gix_actor::Signature,
133    /// The name of the message encoding, otherwise [UTF-8 should be assumed](https://github.com/git/git/blob/e67fbf927dfdf13d0b21dc6ea15dc3c7ef448ea0/commit.c#L1493:L1493).
134    pub encoding: Option<BString>,
135    /// The commit message documenting the change.
136    pub message: BString,
137    /// Extra header fields, in order of them being encountered, made accessible with the iterator returned
138    /// by [`extra_headers()`](Commit::extra_headers()).
139    pub extra_headers: Vec<(BString, BString)>,
140}
141
142/// Represents a git tag, commonly indicating a software release.
143#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub struct TagRef<'a> {
146    /// The hash in hexadecimal being the object this tag points to. Use [`target()`](TagRef::target()) to obtain a byte representation.
147    #[cfg_attr(feature = "serde", serde(borrow))]
148    pub target: &'a BStr,
149    /// The kind of object that `target` points to.
150    pub target_kind: Kind,
151    /// The name of the tag, e.g. "v1.0".
152    pub name: &'a BStr,
153    /// The author of the tag.
154    pub tagger: Option<gix_actor::SignatureRef<'a>>,
155    /// The message describing this release.
156    pub message: &'a BStr,
157    /// A cryptographic signature over the entire content of the serialized tag object thus far.
158    pub pgp_signature: Option<&'a BStr>,
159}
160
161/// Like [`TagRef`], but as `Iterator` to support entirely allocation free parsing.
162/// It's particularly useful to dereference only the target chain.
163#[derive(Copy, Clone)]
164pub struct TagRefIter<'a> {
165    data: &'a [u8],
166    state: tag::ref_iter::State,
167}
168
169/// A mutable git tag.
170#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
171#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
172pub struct Tag {
173    /// The hash this tag is pointing to.
174    pub target: gix_hash::ObjectId,
175    /// The kind of object this tag is pointing to.
176    pub target_kind: Kind,
177    /// The name of the tag, e.g. "v1.0".
178    pub name: BString,
179    /// The tags author.
180    pub tagger: Option<gix_actor::Signature>,
181    /// The message describing the tag.
182    pub message: BString,
183    /// A pgp signature over all bytes of the encoded tag, excluding the pgp signature itself.
184    pub pgp_signature: Option<BString>,
185}
186
187/// Immutable objects are read-only structures referencing most data from [a byte slice](ObjectRef::from_bytes()).
188///
189/// Immutable objects are expected to be deserialized from bytes that acts as backing store, and they
190/// cannot be mutated or serialized. Instead, one will [convert](ObjectRef::into_owned()) them into their [`mutable`](Object) counterparts
191/// which support mutation and serialization.
192///
193/// An `ObjectRef` is representing [`Trees`](TreeRef), [`Blobs`](BlobRef), [`Commits`](CommitRef), or [`Tags`](TagRef).
194#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
195#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
196#[allow(missing_docs)]
197pub enum ObjectRef<'a> {
198    #[cfg_attr(feature = "serde", serde(borrow))]
199    Tree(TreeRef<'a>),
200    Blob(BlobRef<'a>),
201    Commit(CommitRef<'a>),
202    Tag(TagRef<'a>),
203}
204
205/// Mutable objects with each field being separately allocated and changeable.
206///
207/// Mutable objects are Commits, Trees, Blobs and Tags that can be changed and serialized.
208///
209/// They either created using object [construction](Object) or by [deserializing existing objects](ObjectRef::from_bytes())
210/// and converting these [into mutable copies](ObjectRef::into_owned()) for adjustments.
211///
212/// An `Object` is representing [`Trees`](Tree), [`Blobs`](Blob), [`Commits`](Commit), or [`Tags`](Tag).
213#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
214#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
215#[allow(clippy::large_enum_variant, missing_docs)]
216pub enum Object {
217    Tree(Tree),
218    Blob(Blob),
219    Commit(Commit),
220    Tag(Tag),
221}
222/// A directory snapshot containing files (blobs), directories (trees) and submodules (commits).
223#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
224#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
225pub struct TreeRef<'a> {
226    /// The directories and files contained in this tree.
227    ///
228    /// Beware that the sort order isn't *quite* by name, so one may bisect only with a [`tree::EntryRef`] to handle ordering correctly.
229    #[cfg_attr(feature = "serde", serde(borrow))]
230    pub entries: Vec<tree::EntryRef<'a>>,
231}
232
233/// A directory snapshot containing files (blobs), directories (trees) and submodules (commits), lazily evaluated.
234#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
235pub struct TreeRefIter<'a> {
236    /// The directories and files contained in this tree.
237    data: &'a [u8],
238}
239
240/// A mutable Tree, containing other trees, blobs or commits.
241#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
242#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
243pub struct Tree {
244    /// The directories and files contained in this tree. They must be and remain sorted by [`filename`][tree::Entry::filename].
245    ///
246    /// Beware that the sort order isn't *quite* by name, so one may bisect only with a [`tree::Entry`] to handle ordering correctly.
247    pub entries: Vec<tree::Entry>,
248}
249
250impl Tree {
251    /// Return an empty tree which serializes to a well-known hash
252    pub fn empty() -> Self {
253        Tree { entries: Vec::new() }
254    }
255}
256
257/// A borrowed object using a slice as backing buffer, or in other words a bytes buffer that knows the kind of object it represents.
258#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
259pub struct Data<'a> {
260    /// kind of object
261    pub kind: Kind,
262    /// decoded, decompressed data, owned by a backing store.
263    pub data: &'a [u8],
264}
265
266/// Information about an object, which includes its kind and the amount of bytes it would have when obtained.
267#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
268pub struct Header {
269    /// The kind of object.
270    pub kind: Kind,
271    /// The object's size in bytes, or the size of the buffer when it's retrieved in full.
272    pub size: u64,
273}
274
275///
276pub mod decode {
277    #[cfg(feature = "verbose-object-parsing-errors")]
278    mod _decode {
279        /// The type to be used for parse errors.
280        pub type ParseError = winnow::error::ContextError<winnow::error::StrContext>;
281
282        pub(crate) fn empty_error() -> Error {
283            Error {
284                inner: winnow::error::ContextError::new(),
285                remaining: Default::default(),
286            }
287        }
288
289        /// A type to indicate errors during parsing and to abstract away details related to `nom`.
290        #[derive(Debug, Clone)]
291        pub struct Error {
292            /// The actual error
293            pub inner: ParseError,
294            /// Where the error occurred
295            pub remaining: Vec<u8>,
296        }
297
298        impl Error {
299            pub(crate) fn with_err(err: winnow::error::ErrMode<ParseError>, remaining: &[u8]) -> Self {
300                Self {
301                    inner: err.into_inner().expect("we don't have streaming parsers"),
302                    remaining: remaining.to_owned(),
303                }
304            }
305        }
306
307        impl std::fmt::Display for Error {
308            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
309                write!(f, "object parsing failed at `{}`", bstr::BStr::new(&self.remaining))?;
310                if self.inner.context().next().is_some() {
311                    writeln!(f)?;
312                    self.inner.fmt(f)?;
313                }
314                Ok(())
315            }
316        }
317
318        impl std::error::Error for Error {
319            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
320                self.inner.cause().map(|v| v as &(dyn std::error::Error + 'static))
321            }
322        }
323    }
324
325    ///
326    #[cfg(not(feature = "verbose-object-parsing-errors"))]
327    mod _decode {
328        /// The type to be used for parse errors, discards everything and is zero size
329        pub type ParseError = ();
330
331        pub(crate) fn empty_error() -> Error {
332            Error { inner: () }
333        }
334
335        /// A type to indicate errors during parsing and to abstract away details related to `nom`.
336        #[derive(Debug, Clone)]
337        pub struct Error {
338            /// The actual error
339            pub inner: ParseError,
340        }
341
342        impl Error {
343            pub(crate) fn with_err(err: winnow::error::ErrMode<ParseError>, _remaining: &[u8]) -> Self {
344                Self {
345                    inner: err.into_inner().expect("we don't have streaming parsers"),
346                }
347            }
348        }
349
350        impl std::fmt::Display for Error {
351            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
352                f.write_str("object parsing failed")
353            }
354        }
355
356        impl std::error::Error for Error {}
357    }
358    pub(crate) use _decode::empty_error;
359    pub use _decode::{Error, ParseError};
360
361    /// Returned by [`loose_header()`]
362    #[derive(Debug, thiserror::Error)]
363    #[allow(missing_docs)]
364    pub enum LooseHeaderDecodeError {
365        #[error("{message}: {number:?}")]
366        ParseIntegerError {
367            source: gix_utils::btoi::ParseIntegerError,
368            message: &'static str,
369            number: bstr::BString,
370        },
371        #[error("{message}")]
372        InvalidHeader { message: &'static str },
373        #[error("The object header contained an unknown object kind.")]
374        ObjectHeader(#[from] super::kind::Error),
375    }
376
377    use bstr::ByteSlice;
378    /// Decode a loose object header, being `<kind> <size>\0`, returns
379    /// ([`kind`](super::Kind), `size`, `consumed bytes`).
380    ///
381    /// `size` is the uncompressed size of the payload in bytes.
382    pub fn loose_header(input: &[u8]) -> Result<(super::Kind, u64, usize), LooseHeaderDecodeError> {
383        use LooseHeaderDecodeError::*;
384        let kind_end = input.find_byte(0x20).ok_or(InvalidHeader {
385            message: "Expected '<type> <size>'",
386        })?;
387        let kind = super::Kind::from_bytes(&input[..kind_end])?;
388        let size_end = input.find_byte(0x0).ok_or(InvalidHeader {
389            message: "Did not find 0 byte in header",
390        })?;
391        let size_bytes = &input[kind_end + 1..size_end];
392        let size = gix_utils::btoi::to_signed(size_bytes).map_err(|source| ParseIntegerError {
393            source,
394            message: "Object size in header could not be parsed",
395            number: size_bytes.into(),
396        })?;
397        Ok((kind, size, size_end + 1))
398    }
399}
400
401fn object_hasher(hash_kind: gix_hash::Kind, object_kind: Kind, object_size: u64) -> gix_hash::Hasher {
402    let mut hasher = gix_hash::hasher(hash_kind);
403    hasher.update(&encode::loose_header(object_kind, object_size));
404    hasher
405}
406
407/// A function to compute a hash of kind `hash_kind` for an object of `object_kind` and its `data`.
408#[doc(alias = "hash_object", alias = "git2")]
409pub fn compute_hash(
410    hash_kind: gix_hash::Kind,
411    object_kind: Kind,
412    data: &[u8],
413) -> Result<gix_hash::ObjectId, gix_hash::hasher::Error> {
414    let mut hasher = object_hasher(hash_kind, object_kind, data.len() as u64);
415    hasher.update(data);
416    hasher.try_finalize()
417}
418
419/// A function to compute a hash of kind `hash_kind` for an object of `object_kind` and its data read from `stream`
420/// which has to yield exactly `stream_len` bytes.
421/// Use `progress` to learn about progress in bytes processed and `should_interrupt` to be able to abort the operation
422/// if set to `true`.
423#[doc(alias = "hash_file", alias = "git2")]
424pub fn compute_stream_hash(
425    hash_kind: gix_hash::Kind,
426    object_kind: Kind,
427    stream: &mut dyn std::io::Read,
428    stream_len: u64,
429    progress: &mut dyn gix_features::progress::Progress,
430    should_interrupt: &std::sync::atomic::AtomicBool,
431) -> Result<gix_hash::ObjectId, gix_hash::io::Error> {
432    let hasher = object_hasher(hash_kind, object_kind, stream_len);
433    gix_hash::bytes_with_hasher(stream, stream_len, hasher, progress, should_interrupt)
434}