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