gix_diff/tree/
mod.rs

1use crate::tree::visit::Relation;
2use bstr::BStr;
3use gix_hash::ObjectId;
4use gix_object::bstr::BString;
5use std::collections::VecDeque;
6
7/// The error returned by [`tree()`](super::tree()).
8#[derive(Debug, thiserror::Error)]
9#[allow(missing_docs)]
10pub enum Error {
11    #[error(transparent)]
12    Find(#[from] gix_object::find::existing_iter::Error),
13    #[error("The delegate cancelled the operation")]
14    Cancelled,
15    #[error(transparent)]
16    EntriesDecode(#[from] gix_object::decode::Error),
17}
18
19/// A trait to allow responding to a traversal designed to figure out the [changes](visit::Change)
20/// to turn tree A into tree B.
21pub trait Visit {
22    /// Sets the full path in front of the queue so future calls to push and pop components affect it instead.
23    fn pop_front_tracked_path_and_set_current(&mut self);
24    /// Append a `component` to the end of a path, which may be empty.
25    fn push_back_tracked_path_component(&mut self, component: &BStr);
26    /// Append a `component` to the end of a path, which may be empty.
27    fn push_path_component(&mut self, component: &BStr);
28    /// Removes the last component from the path, which may leave it empty.
29    fn pop_path_component(&mut self);
30    /// Record a `change` and return an instruction whether to continue or not.
31    ///
32    /// The implementation may use the current path to lean where in the tree the change is located.
33    fn visit(&mut self, change: visit::Change) -> visit::Action;
34}
35
36/// The state required to run [tree-diffs](super::tree()).
37#[derive(Default, Clone)]
38pub struct State {
39    /// A buffer for object data.
40    pub buf1: Vec<u8>,
41    /// Another buffer for object data.
42    pub buf2: Vec<u8>,
43    trees: VecDeque<TreeInfoTuple>,
44    change_id: visit::ChangeId,
45}
46
47type TreeInfoTuple = (Option<ObjectId>, Option<ObjectId>, Option<Relation>);
48
49impl State {
50    fn clear(&mut self) {
51        self.trees.clear();
52        self.buf1.clear();
53        self.buf2.clear();
54        self.change_id = 0;
55    }
56}
57
58pub(super) mod function;
59
60///
61pub mod visit;
62
63/// A [Visit] implementation to record every observed change and keep track of the changed paths.
64#[derive(Clone, Debug)]
65pub struct Recorder {
66    path_deque: VecDeque<BString>,
67    path: BString,
68    location: Option<recorder::Location>,
69    /// The observed changes.
70    pub records: Vec<recorder::Change>,
71}
72
73/// Useful for use as delegate implementing [`Visit`] to keep track of all seen changes. Useful for debugging or printing primarily.
74pub mod recorder;