gix_pack/data/output/entry/
mod.rs

1use std::io::Write;
2
3use gix_hash::ObjectId;
4
5use crate::{data, data::output, find};
6
7///
8pub mod iter_from_counts;
9pub use iter_from_counts::function::iter_from_counts;
10
11/// The kind of pack entry to be written
12#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub enum Kind {
15    /// A complete base object, including its kind
16    Base(gix_object::Kind),
17    /// A delta against the object with the given index. It's always an index that was already encountered to refer only
18    /// to object we have written already.
19    DeltaRef {
20        /// The absolute index to the object to serve as base. It's up to the writer to maintain enough state to allow producing
21        /// a packed delta object from it.
22        object_index: usize,
23    },
24    /// A delta against the given object as identified by its `ObjectId`.
25    /// This is the case for thin packs only, i.e. those that are sent over the wire.
26    /// Note that there is the option of the `ObjectId` being used to refer to an object within
27    /// the same pack, but it's a discontinued practice which won't be encountered here.
28    DeltaOid {
29        /// The object serving as base for this delta
30        id: ObjectId,
31    },
32}
33
34/// The error returned by [`output::Entry::from_data()`].
35#[allow(missing_docs)]
36#[derive(Debug, thiserror::Error)]
37pub enum Error {
38    #[error("{0}")]
39    ZlibDeflate(#[from] std::io::Error),
40    #[error(transparent)]
41    EntryType(#[from] crate::data::entry::decode::Error),
42}
43
44impl output::Entry {
45    /// An object which can be identified as invalid easily which happens if objects didn't exist even if they were referred to.
46    pub fn invalid() -> output::Entry {
47        output::Entry {
48            id: gix_hash::Kind::Sha1.null(), // NOTE: the actual object hash used in the repo doesn't matter here, this is a sentinel value.
49            kind: Kind::Base(gix_object::Kind::Blob),
50            decompressed_size: 0,
51            compressed_data: vec![],
52        }
53    }
54
55    /// Returns true if this object doesn't really exist but still has to be handled responsibly
56    ///
57    /// Note that this is true for tree entries that are commits/git submodules, or for objects which aren't present in our local clone
58    /// due to shallow clones.
59    pub fn is_invalid(&self) -> bool {
60        self.id.is_null()
61    }
62
63    /// Create an Entry from a previously counted object which is located in a pack. It's `entry` is provided here.
64    /// The `version` specifies what kind of target `Entry` version the caller desires.
65    pub fn from_pack_entry(
66        mut entry: find::Entry,
67        count: &output::Count,
68        potential_bases: &[output::Count],
69        bases_index_offset: usize,
70        pack_offset_to_oid: Option<impl FnMut(u32, u64) -> Option<ObjectId>>,
71        target_version: data::Version,
72    ) -> Option<Result<Self, Error>> {
73        if entry.version != target_version {
74            return None;
75        };
76
77        let pack_offset_must_be_zero = 0;
78        let pack_entry = match data::Entry::from_bytes(&entry.data, pack_offset_must_be_zero, count.id.as_slice().len())
79        {
80            Ok(e) => e,
81            Err(err) => return Some(Err(err.into())),
82        };
83
84        use crate::data::entry::Header::*;
85        match pack_entry.header {
86            Commit => Some(output::entry::Kind::Base(gix_object::Kind::Commit)),
87            Tree => Some(output::entry::Kind::Base(gix_object::Kind::Tree)),
88            Blob => Some(output::entry::Kind::Base(gix_object::Kind::Blob)),
89            Tag => Some(output::entry::Kind::Base(gix_object::Kind::Tag)),
90            OfsDelta { base_distance } => {
91                let pack_location = count.entry_pack_location.as_ref().expect("packed");
92                let base_offset = pack_location
93                    .pack_offset
94                    .checked_sub(base_distance)
95                    .expect("pack-offset - distance is firmly within the pack");
96                potential_bases
97                    .binary_search_by(|e| {
98                        e.entry_pack_location
99                            .as_ref()
100                            .expect("packed")
101                            .pack_offset
102                            .cmp(&base_offset)
103                    })
104                    .ok()
105                    .map(|idx| output::entry::Kind::DeltaRef {
106                        object_index: idx + bases_index_offset,
107                    })
108                    .or_else(|| {
109                        pack_offset_to_oid
110                            .and_then(|mut f| f(pack_location.pack_id, base_offset))
111                            .map(|id| output::entry::Kind::DeltaOid { id })
112                    })
113            }
114            RefDelta { base_id: _ } => None, // ref deltas are for thin packs or legacy, repack them as base objects
115        }
116        .map(|kind| {
117            Ok(output::Entry {
118                id: count.id.to_owned(),
119                kind,
120                decompressed_size: pack_entry.decompressed_size as usize,
121                compressed_data: {
122                    entry.data.copy_within(pack_entry.data_offset as usize.., 0);
123                    entry.data.resize(
124                        entry.data.len()
125                            - usize::try_from(pack_entry.data_offset).expect("offset representable as usize"),
126                        0,
127                    );
128                    entry.data
129                },
130            })
131        })
132    }
133
134    /// Create a new instance from the given `oid` and its corresponding git object data `obj`.
135    pub fn from_data(count: &output::Count, obj: &gix_object::Data<'_>) -> Result<Self, Error> {
136        Ok(output::Entry {
137            id: count.id.to_owned(),
138            kind: Kind::Base(obj.kind),
139            decompressed_size: obj.data.len(),
140            compressed_data: {
141                let mut out = gix_features::zlib::stream::deflate::Write::new(Vec::new());
142                if let Err(err) = std::io::copy(&mut &*obj.data, &mut out) {
143                    match err.kind() {
144                        std::io::ErrorKind::Other => return Err(Error::ZlibDeflate(err)),
145                        err => unreachable!("Should never see other errors than zlib, but got {:?}", err,),
146                    }
147                };
148                out.flush()?;
149                out.into_inner()
150            },
151        })
152    }
153
154    /// Transform ourselves into pack entry header of `version` which can be written into a pack.
155    ///
156    /// `index_to_pack(object_index) -> pack_offset` is a function to convert the base object's index into
157    /// the input object array (if each object is numbered) to an offset into the pack.
158    /// This information is known to the one calling the method.
159    pub fn to_entry_header(
160        &self,
161        version: data::Version,
162        index_to_base_distance: impl FnOnce(usize) -> u64,
163    ) -> data::entry::Header {
164        assert!(
165            matches!(version, data::Version::V2),
166            "we can only write V2 pack entries for now"
167        );
168
169        use Kind::*;
170        match self.kind {
171            Base(kind) => {
172                use gix_object::Kind::*;
173                match kind {
174                    Tree => data::entry::Header::Tree,
175                    Blob => data::entry::Header::Blob,
176                    Commit => data::entry::Header::Commit,
177                    Tag => data::entry::Header::Tag,
178                }
179            }
180            DeltaOid { id } => data::entry::Header::RefDelta { base_id: id.to_owned() },
181            DeltaRef { object_index } => data::entry::Header::OfsDelta {
182                base_distance: index_to_base_distance(object_index),
183            },
184        }
185    }
186}