gix_diff/index/
function.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use super::{Action, ChangeRef, Error, RewriteOptions};
use crate::rewrites;
use bstr::BStr;
use gix_filter::attributes::glob::pattern::Case;
use std::borrow::Cow;
use std::cell::RefCell;
use std::cmp::Ordering;

/// Produce an entry-by-entry diff between `lhs` and `rhs`, sending changes to `cb(change) -> Action` for consumption,
/// which would turn `lhs` into `rhs` if applied.
/// Use `pathspec` to reduce the set of entries to look at, and `pathspec_attributes` may be used by pathspecs that perform
/// attribute lookups.
///
/// If `cb` indicated that the operation should be cancelled, no error is triggered as this isn't supposed to
/// occur through user-interaction - this diff is typically too fast.
///
/// Note that rewrites will be emitted at the end, so no ordering can be assumed. They will only be tracked if
/// `rewrite_options` is `Some`. Note that the set of entries participating in rename tracking is affected by `pathspec`.
///
/// Return the outcome of the rewrite tracker if it was enabled.
///
/// Note that only `rhs` may contain unmerged entries, as `rhs` is expected to be the index read from `.git/index`.
/// Unmerged entries are skipped entirely.
///
/// Conceptually, `rhs` is *ours*, and `lhs` is *theirs*.
/// The entries in `lhs` and `rhs` are both expected to be sorted like index entries are typically sorted.
///
/// Note that sparse indices aren't supported, they must be "unsparsed" before.
pub fn diff<'rhs, 'lhs: 'rhs, E, Find>(
    lhs: &'lhs gix_index::State,
    rhs: &'rhs gix_index::State,
    mut cb: impl FnMut(ChangeRef<'lhs, 'rhs>) -> Result<Action, E>,
    rewrite_options: Option<RewriteOptions<'_, Find>>,
    pathspec: &mut gix_pathspec::Search,
    pathspec_attributes: &mut dyn FnMut(&BStr, Case, bool, &mut gix_attributes::search::Outcome) -> bool,
) -> Result<Option<rewrites::Outcome>, Error>
where
    E: Into<Box<dyn std::error::Error + Send + Sync>>,
    Find: gix_object::FindObjectOrHeader,
{
    if lhs.is_sparse() || rhs.is_sparse() {
        return Err(Error::IsSparse);
    }
    if lhs
        .entries()
        .iter()
        .any(|e| e.stage() != gix_index::entry::Stage::Unconflicted)
    {
        return Err(Error::LhsHasUnmerged);
    }

    let lhs_range = lhs
        .prefixed_entries_range(pathspec.common_prefix())
        .unwrap_or_else(|| 0..lhs.entries().len());
    let rhs_range = rhs
        .prefixed_entries_range(pathspec.common_prefix())
        .unwrap_or_else(|| 0..rhs.entries().len());

    let pattern_matches = RefCell::new(|relative_path, entry: &gix_index::Entry| {
        pathspec
            .pattern_matching_relative_path(relative_path, Some(entry.mode.is_submodule()), pathspec_attributes)
            .is_some_and(|m| !m.is_excluded())
    });

    let (mut lhs_iter, mut rhs_iter) = (
        lhs.entries()[lhs_range.clone()]
            .iter()
            .enumerate()
            .map(|(idx, e)| (idx + lhs_range.start, e.path(lhs), e))
            .filter(|(_, path, e)| pattern_matches.borrow_mut()(path, e)),
        rhs.entries()[rhs_range.clone()]
            .iter()
            .enumerate()
            .map(|(idx, e)| (idx + rhs_range.start, e.path(rhs), e))
            .filter(|(_, path, e)| pattern_matches.borrow_mut()(path, e)),
    );

    let mut resource_cache_storage = None;
    let mut tracker = rewrite_options.map(
        |RewriteOptions {
             resource_cache,
             rewrites,
             find,
         }| {
            resource_cache_storage = Some((resource_cache, find));
            rewrites::Tracker::<ChangeRef<'lhs, 'rhs>>::new(rewrites)
        },
    );

    let (mut lhs_storage, mut rhs_storage) = (lhs_iter.next(), rhs_iter.next());
    loop {
        match (lhs_storage, rhs_storage) {
            (Some(lhs), Some(rhs)) => {
                let (lhs_idx, lhs_path, lhs_entry) = lhs;
                let (rhs_idx, rhs_path, rhs_entry) = rhs;
                match lhs_path.cmp(rhs_path) {
                    Ordering::Less => match emit_deletion(lhs, &mut cb, tracker.as_mut())? {
                        Action::Continue => {
                            lhs_storage = lhs_iter.next();
                        }
                        Action::Cancel => return Ok(None),
                    },
                    Ordering::Equal => {
                        if ignore_unmerged_and_intent_to_add(rhs) {
                            rhs_storage = rhs_iter.next();
                            lhs_storage = lhs_iter.next();
                            continue;
                        }
                        if lhs_entry.id != rhs_entry.id || lhs_entry.mode != rhs_entry.mode {
                            let change = ChangeRef::Modification {
                                location: Cow::Borrowed(rhs_path),
                                previous_index: lhs_idx,
                                previous_entry_mode: lhs_entry.mode,
                                previous_id: Cow::Borrowed(lhs_entry.id.as_ref()),
                                index: rhs_idx,
                                entry_mode: rhs_entry.mode,
                                id: Cow::Borrowed(rhs_entry.id.as_ref()),
                            };

                            let change = match tracker.as_mut() {
                                None => Some(change),
                                Some(tracker) => tracker.try_push_change(change, rhs_path),
                            };
                            if let Some(change) = change {
                                match cb(change).map_err(|err| Error::Callback(err.into()))? {
                                    Action::Continue => {}
                                    Action::Cancel => return Ok(None),
                                }
                            }
                        }
                        lhs_storage = lhs_iter.next();
                        rhs_storage = rhs_iter.next();
                    }
                    Ordering::Greater => match emit_addition(rhs, &mut cb, tracker.as_mut())? {
                        Action::Continue => {
                            rhs_storage = rhs_iter.next();
                        }
                        Action::Cancel => return Ok(None),
                    },
                }
            }
            (Some(lhs), None) => match emit_deletion(lhs, &mut cb, tracker.as_mut())? {
                Action::Cancel => return Ok(None),
                Action::Continue => {
                    lhs_storage = lhs_iter.next();
                }
            },
            (None, Some(rhs)) => match emit_addition(rhs, &mut cb, tracker.as_mut())? {
                Action::Cancel => return Ok(None),
                Action::Continue => {
                    rhs_storage = rhs_iter.next();
                }
            },
            (None, None) => break,
        }
    }

    if let Some((mut tracker, (resource_cache, find))) = tracker.zip(resource_cache_storage) {
        let mut cb_err = None;
        let out = tracker.emit(
            |dst, src| {
                let change = if let Some(src) = src {
                    let (lhs_path, lhs_index, lhs_mode, lhs_id) = src.change.fields();
                    let (rhs_path, rhs_index, rhs_mode, rhs_id) = dst.change.fields();
                    ChangeRef::Rewrite {
                        source_location: Cow::Owned(lhs_path.into()),
                        source_index: lhs_index,
                        source_entry_mode: lhs_mode,
                        source_id: Cow::Owned(lhs_id.into()),
                        location: Cow::Owned(rhs_path.into()),
                        index: rhs_index,
                        entry_mode: rhs_mode,
                        id: Cow::Owned(rhs_id.into()),
                        copy: match src.kind {
                            rewrites::tracker::visit::SourceKind::Rename => false,
                            rewrites::tracker::visit::SourceKind::Copy => true,
                        },
                    }
                } else {
                    dst.change
                };
                match cb(change) {
                    Ok(Action::Continue) => crate::tree::visit::Action::Continue,
                    Ok(Action::Cancel) => crate::tree::visit::Action::Cancel,
                    Err(err) => {
                        cb_err = Some(Error::Callback(err.into()));
                        crate::tree::visit::Action::Cancel
                    }
                }
            },
            resource_cache,
            find,
            |push| {
                for (index, entry) in lhs.entries().iter().enumerate() {
                    let path = entry.path(rhs);
                    push(
                        ChangeRef::Modification {
                            location: Cow::Borrowed(path),
                            previous_index: 0, /* does not matter */
                            previous_entry_mode: entry.mode,
                            previous_id: Cow::Owned(entry.id.kind().null()),
                            index,
                            entry_mode: entry.mode,
                            id: Cow::Borrowed(entry.id.as_ref()),
                        },
                        path,
                    );
                }
                Ok::<_, std::convert::Infallible>(())
            },
        )?;

        if let Some(err) = cb_err {
            Err(err)
        } else {
            Ok(Some(out))
        }
    } else {
        Ok(None)
    }
}

fn emit_deletion<'rhs, 'lhs: 'rhs, E>(
    (idx, path, entry): (usize, &'lhs BStr, &'lhs gix_index::Entry),
    mut cb: impl FnMut(ChangeRef<'lhs, 'rhs>) -> Result<Action, E>,
    tracker: Option<&mut rewrites::Tracker<ChangeRef<'lhs, 'rhs>>>,
) -> Result<Action, Error>
where
    E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
    let change = ChangeRef::Deletion {
        location: Cow::Borrowed(path),
        index: idx,
        entry_mode: entry.mode,
        id: Cow::Borrowed(entry.id.as_ref()),
    };

    let change = match tracker {
        None => change,
        Some(tracker) => match tracker.try_push_change(change, path) {
            Some(change) => change,
            None => return Ok(Action::Continue),
        },
    };

    cb(change).map_err(|err| Error::Callback(err.into()))
}

fn emit_addition<'rhs, 'lhs: 'rhs, E>(
    (idx, path, entry): (usize, &'rhs BStr, &'rhs gix_index::Entry),
    mut cb: impl FnMut(ChangeRef<'lhs, 'rhs>) -> Result<Action, E>,
    tracker: Option<&mut rewrites::Tracker<ChangeRef<'lhs, 'rhs>>>,
) -> Result<Action, Error>
where
    E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
    if ignore_unmerged_and_intent_to_add((idx, path, entry)) {
        return Ok(Action::Continue);
    }

    let change = ChangeRef::Addition {
        location: Cow::Borrowed(path),
        index: idx,
        entry_mode: entry.mode,
        id: Cow::Borrowed(entry.id.as_ref()),
    };

    let change = match tracker {
        None => change,
        Some(tracker) => match tracker.try_push_change(change, path) {
            Some(change) => change,
            None => return Ok(Action::Continue),
        },
    };

    cb(change).map_err(|err| Error::Callback(err.into()))
}

fn ignore_unmerged_and_intent_to_add<'rhs, 'lhs: 'rhs>(
    (_idx, _path, entry): (usize, &'rhs BStr, &'rhs gix_index::Entry),
) -> bool {
    let stage = entry.stage();
    entry.flags.contains(gix_index::entry::Flags::INTENT_TO_ADD) || stage != gix_index::entry::Stage::Unconflicted
}