Skip to main content

gix_object/tree/
ref_iter.rs

1use gix_error::{ErrorExt, Result};
2use std::ops::ControlFlow;
3
4use bstr::BStr;
5
6use crate::{TreeRef, TreeRefIter, tree, tree::EntryRef};
7
8/// Advance a path lookup by matching the next path component against `tree`.
9///
10/// `components` must yield the remaining path components to resolve, and `tree` must be the
11/// current object to search in.
12///
13/// The return value indicates how the caller should proceed:
14///
15/// - [`ControlFlow::Continue`] contains the object id of the matched entry when there are more
16///   components left to resolve. Callers should load that object and pass it back into a subsequent
17///   invocation.
18/// - [`ControlFlow::Break`]`(Some(entry))` contains the matched entry for the final component, and
19///   signals that lookup completed successfully.
20/// - [`ControlFlow::Break`]`(None)` signals that lookup cannot continue and should stop without a
21///   match. This happens if `tree` is not a tree object, if `components` is already exhausted, or if
22///   the next component is not present in `tree`.
23///
24/// Note that this behaviour is tuned to prefer to exhaust the entire chain of `components`, only the
25/// last component can yield a [`ControlFlow::Break`].
26pub fn next_entry<'a, I, P>(
27    components: &mut core::iter::Peekable<I>,
28    tree: crate::Data<'a>,
29) -> core::ops::ControlFlow<Option<EntryRef<'a>>, gix_hash::ObjectId>
30where
31    I: Iterator<Item = P>,
32    P: PartialEq<BStr>,
33{
34    if !tree.kind.is_tree() {
35        return ControlFlow::Break(None);
36    }
37
38    let Some(component) = components.next() else {
39        return ControlFlow::Break(None);
40    };
41
42    let Some(entry) = TreeRefIter::from_bytes(tree.data, tree.object_hash)
43        .filter_map(std::result::Result::ok)
44        .find(|entry| component.eq(entry.filename))
45    else {
46        return ControlFlow::Break(None);
47    };
48
49    if components.peek().is_none() {
50        ControlFlow::Break(Some(entry))
51    } else {
52        ControlFlow::Continue(entry.oid.to_owned())
53    }
54}
55
56impl<'a> TreeRefIter<'a> {
57    /// Instantiate an iterator from the given tree `data` and `object_hash`.
58    pub fn from_bytes(data: &'a [u8], hash_kind: gix_hash::Kind) -> TreeRefIter<'a> {
59        TreeRefIter { data, hash_kind }
60    }
61
62    /// Follow a sequence of `path` components starting from this instance, and look them up in `odb` one by one using `buffer`
63    /// until the last component is looked up and its tree entry is returned.
64    ///
65    /// # Performance Notes
66    ///
67    /// Searching tree entries is currently done in sequence, which allows the search to be allocation free. It would be possible
68    /// to reuse a vector and use a binary search instead, which might be able to improve performance over all.
69    /// However, a benchmark should be created first to have some data and see which trade-off to choose here.
70    pub fn lookup_entry<I, P>(
71        &self,
72        odb: impl crate::Find,
73        buffer: &'a mut Vec<u8>,
74        path: I,
75    ) -> Result<Option<tree::Entry>>
76    where
77        I: IntoIterator<Item = P>,
78        P: PartialEq<BStr>,
79    {
80        buffer.clear();
81        buffer.extend_from_slice(self.data);
82
83        let mut iter = path.into_iter().peekable();
84        let mut data = crate::Data::new(buffer, crate::Kind::Tree, self.hash_kind);
85
86        loop {
87            data = match next_entry(&mut iter, data) {
88                ControlFlow::Continue(oid) => {
89                    let Some(next_tree) = odb.try_find(&oid, buffer)? else {
90                        break Ok(None);
91                    };
92                    next_tree
93                }
94                ControlFlow::Break(v) => break Ok(v.map(Into::into)),
95            }
96        }
97    }
98
99    /// Like [`Self::lookup_entry()`], but takes any [`AsRef<Path>`](`std::path::Path`) directly via `relative_path`,
100    /// a path relative to this tree.
101    /// `odb` and `buffer` are used to lookup intermediate trees.
102    ///
103    /// # Note
104    ///
105    /// If any path component contains illformed UTF-8 and thus can't be converted to bytes on platforms which can't do so natively,
106    /// the returned component will be empty which makes the lookup fail.
107    pub fn lookup_entry_by_path(
108        &self,
109        odb: impl crate::Find,
110        buffer: &'a mut Vec<u8>,
111        relative_path: impl AsRef<std::path::Path>,
112    ) -> Result<Option<tree::Entry>> {
113        self.lookup_entry(
114            odb,
115            buffer,
116            relative_path
117                .as_ref()
118                .components()
119                .map(|c| c.as_os_str().as_encoded_bytes()),
120        )
121    }
122}
123
124impl<'a> TreeRef<'a> {
125    /// Deserialize a Tree from `data`, assuming `object_hash` to determine how the object ids are encoded in this particular tree.
126    pub fn from_bytes(data: &'a [u8], hash_kind: gix_hash::Kind) -> Result<TreeRef<'a>> {
127        decode::tree(data, hash_kind.len_in_bytes())
128    }
129
130    /// Find an entry named `name` knowing if the entry is a directory or not, using a binary search.
131    ///
132    /// Note that it's impossible to binary search by name alone as the sort order is special.
133    pub fn bisect_entry(&self, name: &BStr, is_dir: bool) -> Option<EntryRef<'a>> {
134        self.entries
135            .binary_search_by(|entry| tree::name_order(entry.filename, entry.mode.is_tree(), name, is_dir))
136            .ok()
137            .map(|idx| self.entries[idx])
138    }
139
140    /// Create an instance of the empty tree.
141    ///
142    /// It's particularly useful as static part of a program.
143    pub const fn empty() -> TreeRef<'static> {
144        TreeRef { entries: Vec::new() }
145    }
146}
147
148impl<'a> TreeRefIter<'a> {
149    /// Consume self and return all parsed entries.
150    pub fn entries(self) -> Result<Vec<EntryRef<'a>>> {
151        self.collect()
152    }
153
154    /// Return the offset in bytes that our data advanced from `buf`, the original buffer
155    /// to the beginning of the data of the tree.
156    ///
157    /// Then the tree-iteration can be resumed at the entry that would otherwise be returned next.
158    pub fn offset_to_next_entry(&self, buf: &[u8]) -> usize {
159        let before = (*buf).as_ptr();
160        let after = (*self.data).as_ptr();
161
162        debug_assert!(
163            before <= after,
164            "`TreeRefIter::offset_to_next_entry(): {after:?} <= {before:?}) violated"
165        );
166        (after as usize - before as usize) / std::mem::size_of::<u8>()
167    }
168}
169
170impl<'a> Iterator for TreeRefIter<'a> {
171    type Item = Result<EntryRef<'a>>;
172
173    fn next(&mut self) -> Option<Self::Item> {
174        if self.data.is_empty() {
175            return None;
176        }
177        match decode::fast_entry(self.data, self.hash_kind.len_in_bytes()) {
178            Some((data_left, entry)) => {
179                self.data = data_left;
180                Some(Ok(entry))
181            }
182            None => {
183                self.data = &[];
184                Some(Err(crate::decode::empty_error().raise()))
185            }
186        }
187    }
188}
189
190impl<'a> TryFrom<&'a [u8]> for tree::EntryMode {
191    type Error = &'a [u8];
192
193    fn try_from(mode: &'a [u8]) -> std::result::Result<Self, Self::Error> {
194        tree::EntryMode::from_bytes(mode).ok_or(mode)
195    }
196}
197
198mod decode {
199    use bstr::ByteSlice;
200    use gix_error::{Result, bail};
201
202    use crate::{TreeRef, tree, tree::EntryRef};
203
204    pub fn fast_entry(i: &[u8], hash_len: usize) -> Option<(&[u8], EntryRef<'_>)> {
205        let (mode, i) = tree::EntryMode::extract_from_bytes(i)?;
206        let (filename, i) = i.split_at(i.find_byte(0)?);
207        let i = &i[1..];
208        let (oid, i) = match i.len() {
209            len if len < hash_len => return None,
210            _ => i.split_at(hash_len),
211        };
212        Some((
213            i,
214            EntryRef {
215                mode,
216                filename: filename.as_bstr(),
217                oid: gix_hash::oid::try_from_bytes(oid)
218                    .unwrap_or_else(|_| panic!("we counted exactly {hash_len} bytes")),
219            },
220        ))
221    }
222
223    pub fn tree(data: &[u8], hash_len: usize) -> Result<TreeRef<'_>> {
224        let mut i = data;
225
226        // Calculate an estimate of the amount of entries to reduce
227        // the amount of allocations necessary.
228        // Note that this assumes that we want speed over fitting Vecs, this is a trade-off.
229        const AVERAGE_FILENAME_LEN: usize = 24;
230        const AVERAGE_MODE_LEN: usize = 6;
231        const ENTRY_DELIMITER_LEN: usize = 2; // space + trailing zero
232        const AVERAGE_TREE_ENTRIES: usize = 16 * 2; // prevent overallocation beyond what's meaningful or what could be dangerous
233        let average_entry_len = ENTRY_DELIMITER_LEN + hash_len + AVERAGE_MODE_LEN + AVERAGE_FILENAME_LEN;
234        let upper_bound = i.len() / average_entry_len;
235        let mut out = Vec::with_capacity(upper_bound.min(AVERAGE_TREE_ENTRIES));
236
237        while !i.is_empty() {
238            let Some((rest, entry)) = fast_entry(i, hash_len) else {
239                bail!(crate::decode::empty_error());
240            };
241            i = rest;
242            out.push(entry);
243        }
244        Ok(TreeRef { entries: out })
245    }
246}