gix_diff/index/
mod.rs

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