gix_diff/tree/
mod.rs

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