gix_pack/bundle/
find.rs

1use gix_features::zlib;
2
3impl crate::Bundle {
4    /// Find an object with the given [`ObjectId`](gix_hash::ObjectId) and place its data into `out`.
5    /// `inflate` is used to decompress objects, and will be reset before first use, but not after the last use.
6    ///
7    /// [`cache`](crate::cache::DecodeEntry) is used to accelerate the lookup.
8    ///
9    /// **Note** that ref deltas are automatically resolved within this pack only, which makes this implementation unusable
10    /// for thin packs, which by now are expected to be resolved already.
11    pub fn find<'a>(
12        &self,
13        id: &gix_hash::oid,
14        out: &'a mut Vec<u8>,
15        inflate: &mut zlib::Inflate,
16        cache: &mut dyn crate::cache::DecodeEntry,
17    ) -> Result<Option<(gix_object::Data<'a>, crate::data::entry::Location)>, crate::data::decode::Error> {
18        let idx = match self.index.lookup(id) {
19            Some(idx) => idx,
20            None => return Ok(None),
21        };
22        self.get_object_by_index(idx, out, inflate, cache).map(Some)
23    }
24
25    /// Special-use function to get an object given an index previously returned from
26    /// [index::File::](crate::index::File::lookup()).
27    /// `inflate` is used to decompress objects, and will be reset before first use, but not after the last use.
28    ///
29    /// # Panics
30    ///
31    /// If `index` is out of bounds.
32    pub fn get_object_by_index<'a>(
33        &self,
34        idx: u32,
35        out: &'a mut Vec<u8>,
36        inflate: &mut zlib::Inflate,
37        cache: &mut dyn crate::cache::DecodeEntry,
38    ) -> Result<(gix_object::Data<'a>, crate::data::entry::Location), crate::data::decode::Error> {
39        let ofs = self.index.pack_offset_at_index(idx);
40        let pack_entry = self.pack.entry(ofs)?;
41        let header_size = pack_entry.header_size();
42        self.pack
43            .decode_entry(
44                pack_entry,
45                out,
46                inflate,
47                &|id, _out| {
48                    let idx = self.index.lookup(id)?;
49                    self.pack
50                        .entry(self.index.pack_offset_at_index(idx))
51                        .ok()
52                        .map(crate::data::decode::entry::ResolvedBase::InPack)
53                },
54                cache,
55            )
56            .map(move |r| {
57                (
58                    gix_object::Data {
59                        kind: r.kind,
60                        data: out.as_slice(),
61                    },
62                    crate::data::entry::Location {
63                        pack_id: self.pack.id,
64                        pack_offset: ofs,
65                        entry_size: r.compressed_size + header_size,
66                    },
67                )
68            })
69    }
70}