git_index/
verify.rs

1use std::cmp::Ordering;
2
3use crate::State;
4
5///
6pub mod entries {
7    use bstr::BString;
8
9    /// The error returned by [State::verify_entries()][crate::State::verify_entries()].
10    #[derive(Debug, thiserror::Error)]
11    #[allow(missing_docs)]
12    pub enum Error {
13        #[error("Entry '{current_path}' (stage = {current_stage}) at index {current_index} should order after prior entry '{previous_path}' (stage = {previous_stage})")]
14        OutOfOrder {
15            current_index: usize,
16            current_path: BString,
17            current_stage: u8,
18            previous_path: BString,
19            previous_stage: u8,
20        },
21    }
22}
23
24///
25pub mod extensions {
26    use crate::extension;
27
28    /// An implementation of a `find` function that never finds or returns any object, a no-op.
29    pub fn no_find<'a>(_: &git_hash::oid, _: &'a mut Vec<u8>) -> Option<git_object::TreeRefIter<'a>> {
30        None
31    }
32
33    /// The error returned by [State::verify_extensions()][crate::State::verify_extensions()].
34    #[derive(Debug, thiserror::Error)]
35    #[allow(missing_docs)]
36    pub enum Error {
37        #[error(transparent)]
38        Tree(#[from] extension::tree::verify::Error),
39    }
40}
41
42impl State {
43    /// Assure our entries are consistent.
44    pub fn verify_entries(&self) -> Result<(), entries::Error> {
45        let mut previous = None::<&crate::Entry>;
46        for (idx, entry) in self.entries.iter().enumerate() {
47            if let Some(prev) = previous {
48                if prev.cmp(entry, self) != Ordering::Less {
49                    return Err(entries::Error::OutOfOrder {
50                        current_index: idx,
51                        current_path: entry.path(self).into(),
52                        current_stage: entry.flags.stage() as u8,
53                        previous_path: prev.path(self).into(),
54                        previous_stage: prev.flags.stage() as u8,
55                    });
56                }
57            }
58            previous = Some(entry);
59        }
60        Ok(())
61    }
62
63    /// Note: `find` cannot be `Option<F>` as we can't call it with a closure then due to the indirection through `Some`.
64    pub fn verify_extensions<F>(&self, use_find: bool, find: F) -> Result<(), extensions::Error>
65    where
66        F: for<'a> FnMut(&git_hash::oid, &'a mut Vec<u8>) -> Option<git_object::TreeRefIter<'a>>,
67    {
68        self.tree().map(|t| t.verify(use_find, find)).transpose()?;
69        // TODO: verify links by running the whole set of tests on the index
70        //       - do that once we load it as well, or maybe that's lazy loaded? Too many questions for now.
71        Ok(())
72    }
73}