gix_diff/index/
mod.rs

1use bstr::BStr;
2use std::borrow::Cow;
3
4/// The error returned by [`index()`](crate::index()).
5#[derive(Debug, thiserror::Error)]
6#[allow(missing_docs)]
7pub enum Error {
8    #[error("Cannot diff indices that contain sparse entries")]
9    IsSparse,
10    #[error("Unmerged entries aren't allowed in the left-hand index, only in the right-hand index")]
11    LhsHasUnmerged,
12    #[error("The callback indicated failure")]
13    Callback(#[source] Box<dyn std::error::Error + Send + Sync>),
14    #[error("Failure during rename tracking")]
15    RenameTracking(#[from] crate::rewrites::tracker::emit::Error),
16}
17
18/// What to do after a [ChangeRef] was passed ot the callback of [`index()`](crate::index()).
19#[derive(Default, Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
20pub enum Action {
21    /// Continue the operation.
22    #[default]
23    Continue,
24    /// Stop the operation immediately.
25    ///
26    /// This is useful if one just wants to determine if something changed or not.
27    Cancel,
28}
29
30/// Options to configure how rewrites are tracked as part of the [`index()`](crate::index()) call.
31pub struct RewriteOptions<'a, Find>
32where
33    Find: gix_object::FindObjectOrHeader,
34{
35    /// The cache to be used when rename-tracking by similarity is enabled, typically the default.
36    /// Note that it's recommended to call [`clear_resource_cache()`](`crate::blob::Platform::clear_resource_cache()`)
37    /// between the calls to avoid runaway memory usage, as the cache isn't limited.
38    pub resource_cache: &'a mut crate::blob::Platform,
39    /// A way to lookup objects from the object database, for use in similarity checks.
40    pub find: &'a Find,
41    /// Configure how rewrites are tracked.
42    pub rewrites: crate::Rewrites,
43}
44
45/// Identify a change that would have to be applied to `lhs` to obtain `rhs`, as provided in [`index()`](crate::index()).
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub enum ChangeRef<'lhs, 'rhs> {
48    /// An entry was added to `rhs`.
49    Addition {
50        /// The location of the newly added entry in `rhs`.
51        location: Cow<'rhs, BStr>,
52        /// The index into the entries array of `rhs` for full access.
53        index: usize,
54        /// The mode of the entry in `rhs`.
55        entry_mode: gix_index::entry::Mode,
56        /// The object id of the entry in `rhs`.
57        id: Cow<'rhs, gix_hash::oid>,
58    },
59    /// An entry was removed from `rhs`.
60    Deletion {
61        /// The location the entry that doesn't exist in `rhs`.
62        location: Cow<'lhs, BStr>,
63        /// The index into the entries array of `lhs` for full access.
64        index: usize,
65        /// The mode of the entry in `lhs`.
66        entry_mode: gix_index::entry::Mode,
67        /// The object id of the entry in `lhs`.
68        id: Cow<'rhs, gix_hash::oid>,
69    },
70    /// An entry was modified, i.e. has changed its content or its mode.
71    Modification {
72        /// The location of the modified entry both in `lhs` and `rhs`.
73        location: Cow<'rhs, BStr>,
74        /// The index into the entries array of `lhs` for full access.
75        previous_index: usize,
76        /// The previous mode of the entry, in `lhs`.
77        previous_entry_mode: gix_index::entry::Mode,
78        /// The previous object id of the entry, in `lhs`.
79        previous_id: Cow<'lhs, gix_hash::oid>,
80        /// The index into the entries array of `rhs` for full access.
81        index: usize,
82        /// The mode of the entry in `rhs`.
83        entry_mode: gix_index::entry::Mode,
84        /// The object id of the entry in `rhs`.
85        id: Cow<'rhs, gix_hash::oid>,
86    },
87    /// An entry was renamed or copied from `lhs` to `rhs`.
88    ///
89    /// A rename is effectively fusing together the `Deletion` of the source and the `Addition` of the destination.
90    Rewrite {
91        /// The location of the source of the rename or copy operation, in `lhs`.
92        source_location: Cow<'lhs, BStr>,
93        /// The index of the entry before the rename, into the entries array of `rhs` for full access.
94        source_index: usize,
95        /// The mode of the entry before the rewrite, in `lhs`.
96        source_entry_mode: gix_index::entry::Mode,
97        /// The object id of the entry before the rewrite.
98        ///
99        /// Note that this is the same as `id` if we require the [similarity to be 100%](super::Rewrites::percentage), but may
100        /// be different otherwise.
101        source_id: Cow<'lhs, gix_hash::oid>,
102
103        /// The current location of the entry in `rhs`.
104        location: Cow<'rhs, BStr>,
105        /// The index of the entry after the rename, into the entries array of `rhs` for full access.
106        index: usize,
107        /// The mode of the entry after the rename in `rhs`.
108        entry_mode: gix_index::entry::Mode,
109        /// The object id of the entry after the rename in `rhs`.
110        id: Cow<'rhs, gix_hash::oid>,
111
112        /// If true, this rewrite is created by copy, and `source_id` is pointing to its source. Otherwise, it's a rename,
113        /// and `source_id` points to a deleted object, as renames are tracked as deletions and additions of the same
114        /// or similar content.
115        copy: bool,
116    },
117}
118
119/// The fully-owned version of [`ChangeRef`].
120pub type Change = ChangeRef<'static, 'static>;
121
122mod change;
123pub(super) mod function;