gix_diff/tree_with_rewrites/
change.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use crate::blob::{DiffLineStats, ResourceKind};
use crate::tree;
use bstr::BString;
use bstr::{BStr, ByteSlice};

/// Represents any possible change in order to turn one tree into another, which references data owned by its producer.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ChangeRef<'a> {
    /// An entry was added, like the addition of a file or directory.
    Addition {
        /// The location of the file or directory.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        location: &'a BStr,
        /// The mode of the added entry.
        entry_mode: gix_object::tree::EntryMode,
        /// Identifies a relationship between this instance and another one,
        /// making it easy to reconstruct the top-level of directory changes.
        relation: Option<tree::visit::Relation>,
        /// The object id of the added entry.
        id: gix_hash::ObjectId,
    },
    /// An entry was deleted, like the deletion of a file or directory.
    Deletion {
        /// The location of the file or directory.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        /// are tracked.
        location: &'a BStr,
        /// The mode of the deleted entry.
        entry_mode: gix_object::tree::EntryMode,
        /// Identifies a relationship between this instance and another one,
        /// making it easy to reconstruct the top-level of directory changes.
        relation: Option<tree::visit::Relation>,
        /// The object id of the deleted entry.
        id: gix_hash::ObjectId,
    },
    /// An entry was modified, e.g. changing the contents of a file adjusts its object id and turning
    /// a file into a symbolic link adjusts its mode.
    Modification {
        /// The location of the file or directory.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        /// are tracked.
        location: &'a BStr,
        /// The mode of the entry before the modification.
        previous_entry_mode: gix_object::tree::EntryMode,
        /// The object id of the entry before the modification.
        previous_id: gix_hash::ObjectId,

        /// The mode of the entry after the modification.
        entry_mode: gix_object::tree::EntryMode,
        /// The object id after the modification.
        id: gix_hash::ObjectId,
    },
    /// Entries are considered rewritten if they are not trees and they, according to some understanding of identity, were renamed
    /// or copied.
    /// In case of renames, this means they originally appeared as [`Deletion`](ChangeRef::Deletion) signalling their source as well as an
    /// [`Addition`](ChangeRef::Addition) acting as destination.
    ///
    /// In case of copies, the `copy` flag is true and typically represents a perfect copy of a source was made.
    ///
    /// This variant can only be encountered if [rewrite tracking](super::Options::rewrites) is enabled.
    ///
    /// Note that mode changes may have occurred as well, i.e. changes from executable to non-executable or vice-versa.
    Rewrite {
        /// The location of the source of the rename or copy operation.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        /// are tracked.
        source_location: &'a BStr,
        /// The mode of the entry before the rename.
        source_entry_mode: gix_object::tree::EntryMode,
        /// Identifies a relationship between the source and another source,
        /// making it easy to reconstruct the top-level of directory changes.
        source_relation: Option<tree::visit::Relation>,
        /// The object id of the entry before the rename.
        ///
        /// Note that this is the same as `id` if we require the [similarity to be 100%](super::Rewrites::percentage), but may
        /// be different otherwise.
        source_id: gix_hash::ObjectId,
        /// Information about the diff we performed to detect similarity and match the `source_id` with the current state at `id`.
        /// It's `None` if `source_id` is equal to `id`, as identity made an actual diff computation unnecessary.
        diff: Option<DiffLineStats>,
        /// The mode of the entry after the rename.
        /// It could differ but still be considered a rename as we are concerned only about content.
        entry_mode: gix_object::tree::EntryMode,
        /// The object id after the rename.
        id: gix_hash::ObjectId,
        /// The location after the rename or copy operation.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        location: &'a BStr,
        /// Identifies a relationship between this destination and another destination,
        /// making it easy to reconstruct the top-level of directory changes.
        relation: Option<tree::visit::Relation>,
        /// If true, this rewrite is created by copy, and `source_id` is pointing to its source. Otherwise, it's a rename, and `source_id`
        /// points to a deleted object, as renames are tracked as deletions and additions of the same or similar content.
        copy: bool,
    },
}

/// Represents any possible change in order to turn one tree into another, with fully-owned data.
#[derive(Debug, Clone, PartialEq)]
pub enum Change {
    /// An entry was added, like the addition of a file or directory.
    Addition {
        /// The location of the file or directory.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        location: BString,
        /// Identifies a relationship between this instance and another one,
        /// making it easy to reconstruct the top-level of directory changes.
        relation: Option<tree::visit::Relation>,
        /// The mode of the added entry.
        entry_mode: gix_object::tree::EntryMode,
        /// The object id of the added entry.
        id: gix_hash::ObjectId,
    },
    /// An entry was deleted, like the deletion of a file or directory.
    Deletion {
        /// The location of the file or directory.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        location: BString,
        /// Identifies a relationship between this instance and another one,
        /// making it easy to reconstruct the top-level of directory changes.
        relation: Option<tree::visit::Relation>,
        /// The mode of the deleted entry.
        entry_mode: gix_object::tree::EntryMode,
        /// The object id of the deleted entry.
        id: gix_hash::ObjectId,
    },
    /// An entry was modified, e.g. changing the contents of a file adjusts its object id and turning
    /// a file into a symbolic link adjusts its mode.
    Modification {
        /// The location of the file or directory.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        location: BString,
        /// The mode of the entry before the modification.
        previous_entry_mode: gix_object::tree::EntryMode,
        /// The object id of the entry before the modification.
        previous_id: gix_hash::ObjectId,

        /// The mode of the entry after the modification.
        entry_mode: gix_object::tree::EntryMode,
        /// The object id after the modification.
        id: gix_hash::ObjectId,
    },
    /// Entries are considered rewritten if they are not trees and they, according to some understanding of identity, were renamed
    /// or copied.
    /// In case of renames, this means they originally appeared as [`Deletion`](ChangeRef::Deletion) signalling their source as well as an
    /// [`Addition`](ChangeRef::Addition) acting as destination.
    ///
    /// In case of copies, the `copy` flag is true and typically represents a perfect copy of a source was made.
    ///
    /// This variant can only be encountered if [rewrite tracking](super::Options::rewrites) is enabled.
    ///
    /// Note that mode changes may have occurred as well, i.e. changes from executable to non-executable or vice-versa.
    Rewrite {
        /// The location of the source of the rename operation.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        source_location: BString,
        /// The mode of the entry before the rename.
        source_entry_mode: gix_object::tree::EntryMode,
        /// Identifies a relationship between the source and another source,
        /// making it easy to reconstruct the top-level of directory changes.
        source_relation: Option<tree::visit::Relation>,
        /// The object id of the entry before the rename.
        ///
        /// Note that this is the same as `id` if we require the [similarity to be 100%](super::Rewrites::percentage), but may
        /// be different otherwise.
        source_id: gix_hash::ObjectId,
        /// Information about the diff we performed to detect similarity and match the `source_id` with the current state at `id`.
        /// It's `None` if `source_id` is equal to `id`, as identity made an actual diff computation unnecessary.
        diff: Option<DiffLineStats>,
        /// The mode of the entry after the rename.
        /// It could differ but still be considered a rename as we are concerned only about content.
        entry_mode: gix_object::tree::EntryMode,
        /// The object id after the rename.
        id: gix_hash::ObjectId,
        /// The location after the rename or copy operation.
        ///
        /// It may be empty if [file names](super::Options::location) is `None`.
        location: BString,
        /// Identifies a relationship between this destination and another destination,
        /// making it easy to reconstruct the top-level of directory changes.
        relation: Option<tree::visit::Relation>,
        /// If true, this rewrite is created by copy, and `source_id` is pointing to its source. Otherwise, it's a rename, and `source_id`
        /// points to a deleted object, as renames are tracked as deletions and additions of the same or similar content.
        copy: bool,
    },
}

/// Lifecycle
impl ChangeRef<'_> {
    /// Copy this instance into a fully-owned version
    pub fn into_owned(self) -> Change {
        match self {
            ChangeRef::Addition {
                location,
                entry_mode,
                id,
                relation,
            } => Change::Addition {
                location: location.to_owned(),
                entry_mode,
                id,
                relation,
            },
            ChangeRef::Deletion {
                location,
                entry_mode,
                id,
                relation,
            } => Change::Deletion {
                location: location.to_owned(),
                entry_mode,
                id,
                relation,
            },
            ChangeRef::Modification {
                location,
                previous_entry_mode,
                previous_id,
                entry_mode,
                id,
            } => Change::Modification {
                location: location.to_owned(),
                previous_entry_mode,
                previous_id,
                entry_mode,
                id,
            },
            ChangeRef::Rewrite {
                source_location,
                source_relation,
                source_entry_mode,
                source_id,
                diff,
                entry_mode,
                id,
                location,
                relation,
                copy,
            } => Change::Rewrite {
                source_location: source_location.to_owned(),
                source_relation,
                source_entry_mode,
                source_id,
                diff,
                entry_mode,
                id,
                location: location.to_owned(),
                relation,
                copy,
            },
        }
    }
}

/// Lifecycle
impl Change {
    /// Return an attached version of this instance that uses `old_repo` for previous values and `new_repo` for current values.
    pub fn to_ref(&self) -> ChangeRef<'_> {
        match self {
            Change::Addition {
                location,
                relation,
                entry_mode,
                id,
            } => ChangeRef::Addition {
                location: location.as_bstr(),
                entry_mode: *entry_mode,
                id: *id,
                relation: *relation,
            },
            Change::Deletion {
                location,
                relation,
                entry_mode,
                id,
            } => ChangeRef::Deletion {
                location: location.as_bstr(),
                entry_mode: *entry_mode,
                id: *id,
                relation: *relation,
            },
            Change::Modification {
                location,
                previous_entry_mode,
                previous_id,
                entry_mode,
                id,
            } => ChangeRef::Modification {
                location: location.as_bstr(),
                previous_entry_mode: *previous_entry_mode,
                previous_id: *previous_id,
                entry_mode: *entry_mode,
                id: *id,
            },
            Change::Rewrite {
                source_location,
                source_relation,
                source_entry_mode,
                source_id,
                diff,
                entry_mode,
                id,
                location,
                relation,
                copy,
            } => ChangeRef::Rewrite {
                source_location: source_location.as_ref(),
                source_relation: *source_relation,
                source_entry_mode: *source_entry_mode,
                source_id: *source_id,
                diff: *diff,
                entry_mode: *entry_mode,
                id: *id,
                location: location.as_bstr(),
                relation: *relation,
                copy: *copy,
            },
        }
    }
}

impl crate::blob::Platform {
    /// Set ourselves up to produces blob-diffs from `change`, so this platform can be used to produce diffs easily.
    /// `objects` are used to fetch object data as needed.
    ///
    /// ### Warning about Memory Consumption
    ///
    /// This instance only grows, so one should call [`crate::blob::Platform::clear_resource_cache`] occasionally.
    pub fn set_resource_by_change(
        &mut self,
        change: ChangeRef<'_>,
        objects: &impl gix_object::FindObjectOrHeader,
    ) -> Result<&mut Self, crate::blob::platform::set_resource::Error> {
        match change {
            ChangeRef::Addition {
                location,
                relation: _,
                entry_mode,
                id,
            } => {
                self.set_resource(
                    id.kind().null(),
                    entry_mode.kind(),
                    location,
                    ResourceKind::OldOrSource,
                    objects,
                )?;
                self.set_resource(id, entry_mode.kind(), location, ResourceKind::NewOrDestination, objects)?;
            }
            ChangeRef::Deletion {
                location,
                relation: _,
                entry_mode,
                id,
            } => {
                self.set_resource(id, entry_mode.kind(), location, ResourceKind::OldOrSource, objects)?;
                self.set_resource(
                    id.kind().null(),
                    entry_mode.kind(),
                    location,
                    ResourceKind::NewOrDestination,
                    objects,
                )?;
            }
            ChangeRef::Modification {
                location,
                previous_entry_mode,
                previous_id,
                entry_mode,
                id,
            } => {
                self.set_resource(
                    previous_id,
                    previous_entry_mode.kind(),
                    location,
                    ResourceKind::OldOrSource,
                    objects,
                )?;
                self.set_resource(id, entry_mode.kind(), location, ResourceKind::NewOrDestination, objects)?;
            }
            ChangeRef::Rewrite {
                source_location,
                source_relation: _,
                source_entry_mode,
                source_id,
                entry_mode,
                id,
                location,
                relation: _,
                diff: _,
                copy: _,
            } => {
                self.set_resource(
                    source_id,
                    source_entry_mode.kind(),
                    source_location,
                    ResourceKind::OldOrSource,
                    objects,
                )?;
                self.set_resource(id, entry_mode.kind(), location, ResourceKind::NewOrDestination, objects)?;
            }
        }
        Ok(self)
    }
}

impl<'a> ChangeRef<'a> {
    /// Return the relation this instance may have to other changes.
    pub fn relation(&self) -> Option<tree::visit::Relation> {
        match self {
            ChangeRef::Addition { relation, .. }
            | ChangeRef::Deletion { relation, .. }
            | ChangeRef::Rewrite { relation, .. } => *relation,
            ChangeRef::Modification { .. } => None,
        }
    }

    /// Return the current mode of this instance.
    pub fn entry_mode(&self) -> gix_object::tree::EntryMode {
        match self {
            ChangeRef::Addition { entry_mode, .. }
            | ChangeRef::Deletion { entry_mode, .. }
            | ChangeRef::Modification { entry_mode, .. }
            | ChangeRef::Rewrite { entry_mode, .. } => *entry_mode,
        }
    }

    /// Return the current mode of this instance, along with its object id.
    pub fn entry_mode_and_id(&self) -> (gix_object::tree::EntryMode, &gix_hash::oid) {
        match self {
            ChangeRef::Addition { entry_mode, id, .. }
            | ChangeRef::Deletion { entry_mode, id, .. }
            | ChangeRef::Modification { entry_mode, id, .. }
            | ChangeRef::Rewrite { entry_mode, id, .. } => (*entry_mode, id),
        }
    }

    /// Return the *previous* mode and id of the resource where possible, i.e. the source of a rename or copy, or a modification.
    pub fn source_entry_mode_and_id(&self) -> (gix_object::tree::EntryMode, &gix_hash::oid) {
        match self {
            ChangeRef::Addition { entry_mode, id, .. }
            | ChangeRef::Deletion { entry_mode, id, .. }
            | ChangeRef::Modification {
                previous_entry_mode: entry_mode,
                previous_id: id,
                ..
            }
            | ChangeRef::Rewrite {
                source_entry_mode: entry_mode,
                source_id: id,
                ..
            } => (*entry_mode, id),
        }
    }

    /// Return the *current* location of the resource, i.e. the destination of a rename or copy, or the
    /// location at which an addition, deletion or modification took place.
    pub fn location(&self) -> &'a BStr {
        match self {
            ChangeRef::Addition { location, .. }
            | ChangeRef::Deletion { location, .. }
            | ChangeRef::Modification { location, .. }
            | ChangeRef::Rewrite { location, .. } => location,
        }
    }

    /// Return the *previous* location of the resource where possible, i.e. the source of a rename or copy, or the
    /// location at which an addition, deletion or modification took place.
    pub fn source_location(&self) -> &BStr {
        match self {
            ChangeRef::Addition { location, .. }
            | ChangeRef::Deletion { location, .. }
            | ChangeRef::Modification { location, .. } => location,
            ChangeRef::Rewrite { source_location, .. } => source_location,
        }
    }
}

impl Change {
    /// Return the relation this instance may have to other changes.
    pub fn relation(&self) -> Option<tree::visit::Relation> {
        match self {
            Change::Addition { relation, .. }
            | Change::Deletion { relation, .. }
            | Change::Rewrite { relation, .. } => *relation,
            Change::Modification { .. } => None,
        }
    }

    /// Return the current mode of this instance.
    pub fn entry_mode(&self) -> gix_object::tree::EntryMode {
        match self {
            Change::Addition { entry_mode, .. }
            | Change::Deletion { entry_mode, .. }
            | Change::Modification { entry_mode, .. }
            | Change::Rewrite { entry_mode, .. } => *entry_mode,
        }
    }

    /// Return the current mode of this instance, along with its object id.
    pub fn entry_mode_and_id(&self) -> (gix_object::tree::EntryMode, &gix_hash::oid) {
        match self {
            Change::Addition { entry_mode, id, .. }
            | Change::Deletion { entry_mode, id, .. }
            | Change::Modification { entry_mode, id, .. }
            | Change::Rewrite { entry_mode, id, .. } => (*entry_mode, id),
        }
    }

    /// Return the *previous* mode and id of the resource where possible, i.e. the source of a rename or copy, or a modification.
    pub fn source_entry_mode_and_id(&self) -> (gix_object::tree::EntryMode, &gix_hash::oid) {
        match self {
            Change::Addition { entry_mode, id, .. }
            | Change::Deletion { entry_mode, id, .. }
            | Change::Modification {
                previous_entry_mode: entry_mode,
                previous_id: id,
                ..
            }
            | Change::Rewrite {
                source_entry_mode: entry_mode,
                source_id: id,
                ..
            } => (*entry_mode, id),
        }
    }

    /// Return the *current* location of the resource, i.e. the destination of a rename or copy, or the
    /// location at which an addition, deletion or modification took place.
    pub fn location(&self) -> &BStr {
        match self {
            Change::Addition { location, .. }
            | Change::Deletion { location, .. }
            | Change::Modification { location, .. }
            | Change::Rewrite { location, .. } => location.as_bstr(),
        }
    }

    /// Return the *previous* location of the resource where possible, i.e. the source of a rename or copy, or the
    /// location at which an addition, deletion or modification took place.
    pub fn source_location(&self) -> &BStr {
        match self {
            Change::Addition { location, .. }
            | Change::Deletion { location, .. }
            | Change::Modification { location, .. } => location.as_bstr(),
            Change::Rewrite { source_location, .. } => source_location.as_bstr(),
        }
    }
}