Skip to main content

gix_diff/index/
mod.rs

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