Skip to main content

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)]
11#[expect(missing_docs)]
12pub enum Error {
13    Cancelled,
14    Failure(gix_error::Error),
15}
16
17impl std::fmt::Display for Error {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Error::Cancelled => f.write_str("The delegate cancelled the operation"),
21            Error::Failure(err) => std::fmt::Display::fmt(err, f),
22        }
23    }
24}
25
26impl std::error::Error for Error {
27    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28        match self {
29            Error::Cancelled => None,
30            Error::Failure(err) => Some(err),
31        }
32    }
33}
34
35impl<E> From<gix_error::Exn<E>> for Error
36where
37    E: std::error::Error + Send + Sync + 'static,
38{
39    fn from(err: gix_error::Exn<E>) -> Self {
40        Error::Failure(err.into_error())
41    }
42}
43
44impl From<gix_error::Message> for Error {
45    fn from(err: gix_error::Message) -> Self {
46        Error::Failure(gix_error::Error::from_error(err))
47    }
48}
49
50/// A trait to allow responding to a traversal designed to figure out the [changes](visit::Change)
51/// to turn tree A into tree B.
52pub trait Visit {
53    /// Sets the full path in front of the queue so future calls to push and pop components affect it instead.
54    fn pop_front_tracked_path_and_set_current(&mut self);
55    /// Append a `component` to the end of a path, which may be empty.
56    fn push_back_tracked_path_component(&mut self, component: &BStr);
57    /// Append a `component` to the end of a path, which may be empty.
58    fn push_path_component(&mut self, component: &BStr);
59    /// Removes the last component from the path, which may leave it empty.
60    fn pop_path_component(&mut self);
61    /// Record a `change` and return an instruction whether to continue or not.
62    ///
63    /// The implementation may use the current path to lean where in the tree the change is located.
64    fn visit(&mut self, change: visit::Change) -> visit::Action;
65}
66
67/// The state required to run [tree-diffs](super::tree()).
68#[derive(Default, Clone)]
69pub struct State {
70    /// A buffer for object data.
71    pub buf1: Vec<u8>,
72    /// Another buffer for object data.
73    pub buf2: Vec<u8>,
74    trees: VecDeque<TreeInfoTuple>,
75    change_id: visit::ChangeId,
76}
77
78type TreeInfoTuple = (Option<ObjectId>, Option<ObjectId>, Option<Relation>);
79
80impl State {
81    fn clear(&mut self) {
82        self.trees.clear();
83        self.buf1.clear();
84        self.buf2.clear();
85        self.change_id = 0;
86    }
87}
88
89pub(super) mod function;
90
91///
92pub mod visit;
93
94/// A [Visit] implementation to record every observed change and keep track of the changed paths.
95#[derive(Clone, Debug)]
96pub struct Recorder {
97    path_deque: VecDeque<BString>,
98    path: BString,
99    location: Option<recorder::Location>,
100    /// The observed changes.
101    pub records: Vec<recorder::Change>,
102}
103
104/// Useful for use as delegate implementing [`Visit`] to keep track of all seen changes. Useful for debugging or printing primarily.
105pub mod recorder;