OXYGENHUB
# 1966f5db15ee oxygen / crates / oxy-core / src / repo / snapshot.rs
95.5 KiB · Rust · last changed Manifest as a flat binary file, hashes and graph keys as bytes (refs #41) 1 month ago source blame history raw
1
//! Working-copy snapshots, commits, and the change builder (spec §12.3).
2
3
use super::*;
4
use crate::objects::AtomRef;
5
6
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7
pub enum ChangeStatus {
8
    Added,
9
    Modified,
10
    Removed,
11
    /// Moved from another path (detected by similarity, tracked as a placement change).
12
    Renamed,
13
    ModeChanged,
14
    /// The file is conflicted in the base state and still contains conflict markers.
15
    ConflictEdited,
16
    /// The file was conflicted in the base state and the working copy resolves it.
17
    Resolved,
18
}
19
20
/// Lines that mark a conflict region in a materialized file (spec §9.5).
21
pub fn has_conflict_markers(content: &[u8]) -> bool {
22
    split_lines(content).iter().any(|l| {
23
        l.starts_with(b"<<<<<<< oxy conflict")
24
            || l.starts_with(b"======= oxy conflict")
25
            || l.starts_with(b">>>>>>> oxy conflict")
26
    })
27
}
28
29
#[derive(Clone, Debug, PartialEq, Eq)]
30
pub struct PathStatus {
31
    pub path: String,
32
    pub status: ChangeStatus,
33
}
34
35
/// Operations of a change built from the working directory against a base.
36
pub(crate) struct BuiltChange {
37
    pub tree_ops: Vec<TreeOp>,
38
    pub edits: Vec<Edit>,
39
    pub contributors: Vec<ChangeHash>,
40
    pub resolves: bool,
41
    pub statuses: Vec<PathStatus>,
42
    /// The stored blob of every opaque path, for the manifest's stat cache.
43
    pub blobs: HashMap<String, ObjectRef>,
44
}
45
46
/// Result of snapshotting the working copy.
47
pub struct SnapshotResult {
48
    pub wc_hash: ChangeHash,
49
    pub wc: Change,
50
    /// The base state (closure of the working-copy change's dependencies).
51
    pub base: State,
52
    pub base_snapshot: Snapshot,
53
    pub statuses: Vec<PathStatus>,
54
    /// Whether the snapshot rewrote the working-copy change.
55
    pub changed: bool,
56
    /// Whether the working copy resolves conflicts of its base (spec §9.6).
57
    pub resolves: bool,
58
    /// Changes that contributed to the conflicts being resolved.
59
    pub contributors: Vec<ChangeHash>,
60
}
61
62
pub fn is_binary(content: &[u8]) -> bool {
63
    content[..content.len().min(BINARY_SNIFF)].contains(&0)
64
}
65
66
impl Repo {
67
    /// Diff the files on disk against `base` and build the operations of a
68
    /// change from `base` to the working directory. `reuse` maps paths to the
69
    /// NodeIds an earlier version of the same change assigned to new paths.
70
    pub(crate) fn build_against(
71
        &self,
72
        base: &State,
73
        base_snapshot: &Snapshot,
74
        reuse: HashMap<String, (NodeId, NodeKind)>,
75
    ) -> Result<BuiltChange> {
76
        let mut fs_entries =
77
            self.walk_working_copy_with(None, &Self::tracked_kinds(base_snapshot), None)?;
78
        self.platform_normalize(&mut fs_entries, base_snapshot);
79
        self.build_from_entries(base, base_snapshot, reuse, &fs_entries)
80
    }
81
82
    /// Read the working copy as the platform left it (spec §7.7): without
83
    /// executable bits a file keeps the bit it has in the base, and without
84
    /// symbolic links a plain file where the base has a symlink is that
85
    /// symlink, written out as its target, not a new file.
86
    fn platform_normalize(&self, entries: &mut BTreeMap<String, FsEntry>, base: &Snapshot) {
87
        let p = self.platform;
88
        if p.exec_bits && p.symlinks {
89
            return;
90
        }
91
        for (path, e) in entries.iter_mut() {
92
            let Some(f) = base.files.get(path) else {
93
                continue;
94
            };
95
            if !p.symlinks && f.kind == NodeKind::Symlink && e.kind == NodeKind::File {
96
                e.kind = NodeKind::Symlink;
97
                e.exec = false;
98
            }
99
            if !p.exec_bits && f.kind == NodeKind::File && e.kind == NodeKind::File {
100
                e.exec = f.mode.exec;
101
            }
102
        }
103
    }
104
105
    /// Like `build_against`, for files given in memory (imports).
106
    pub(crate) fn build_from_entries(
107
        &self,
108
        base: &State,
109
        base_snapshot: &Snapshot,
110
        reuse: HashMap<String, (NodeId, NodeKind)>,
111
        fs_entries: &BTreeMap<String, FsEntry>,
112
    ) -> Result<BuiltChange> {
113
        self.build_from_entries_resolving(base, base_snapshot, reuse, fs_entries, &HashSet::new())
114
    }
115
116
    /// Like `build_from_entries`, settling the replace conflicts of the
117
    /// opaque files in `resolve` to their content (spec §9.3, §9.6): a
118
    /// caller that knows the user chose it, such as the hub's resolver. A
119
    /// snapshot never settles one on its own, since a checkout writes one
120
    /// candidate at the path.
121
    pub(crate) fn build_from_entries_resolving(
122
        &self,
123
        base: &State,
124
        base_snapshot: &Snapshot,
125
        reuse: HashMap<String, (NodeId, NodeKind)>,
126
        fs_entries: &BTreeMap<String, FsEntry>,
127
        resolve: &HashSet<String>,
128
    ) -> Result<BuiltChange> {
129
        self.build_from_entries_with(base, base_snapshot, reuse, fs_entries, resolve, None)
130
    }
131
132
    /// `build_from_entries` whose new nodes take identifiers derived from
133
    /// `seed` and their paths instead of random ones, so the same input
134
    /// always builds the same change (a Git import, spec §23).
135
    ///
136
    /// With `dir_needed`, the snapshot and entries hold only the paths the
137
    /// import touches (and the directories a removal may leave empty); a
138
    /// directory stays while `dir_needed` says the new tree has something
139
    /// under it.
140
    pub(crate) fn build_from_entries_seeded(
141
        &self,
142
        base: &State,
143
        base_snapshot: &Snapshot,
144
        fs_entries: &BTreeMap<String, FsEntry>,
145
        seed: [u8; 32],
146
        dir_needed: Option<&dyn Fn(&str) -> bool>,
147
    ) -> Result<BuiltChange> {
148
        self.build_from_entries_with(
149
            base,
150
            base_snapshot,
151
            HashMap::new(),
152
            fs_entries,
153
            &HashSet::new(),
154
            Some(Import { seed, dir_needed }),
155
        )
156
    }
157
158
    fn build_from_entries_with(
159
        &self,
160
        base: &State,
161
        base_snapshot: &Snapshot,
162
        reuse: HashMap<String, (NodeId, NodeKind)>,
163
        fs_entries: &BTreeMap<String, FsEntry>,
164
        resolve: &HashSet<String>,
165
        import: Option<Import>,
166
    ) -> Result<BuiltChange> {
167
        let seed = import.as_ref().map(|i| i.seed);
168
        let dir_needed = import.and_then(|i| i.dir_needed);
169
        let mut laps = crate::timing::Laps::start();
170
        // Attributes come from the files being committed, so a new
171
        // `.oxyattributes` applies to the same change that adds it.
172
        let attrs = crate::attributes::Attributes::from_files(
173
            fs_entries
174
                .iter()
175
                .filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes"))
176
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
177
        );
178
        let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs);
179
        b.resolve = resolve.clone();
180
        b.node_seed = seed;
181
        laps.lap("build: builder");
182
        // Rename detection (spec §7.1, §23): a removed file and an added file
183
        // of the same kind whose contents are at least half alike become one
184
        // move of the same node, so identity survives across the rename.
185
        let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p));
186
        let renamed_from: HashMap<&str, &str> = renames
187
            .iter()
188
            .map(|(old, new)| (new.as_str(), old.as_str()))
189
            .collect();
190
        let renamed_to: HashMap<&str, &str> = renames
191
            .iter()
192
            .map(|(old, new)| (old.as_str(), new.as_str()))
193
            .collect();
194
        laps.lap("build: renames");
195
        for (path, fs_entry) in fs_entries {
196
            match base_snapshot.files.get(path) {
197
                None => match renamed_from.get(path.as_str()) {
198
                    Some(old) => {
199
                        b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])?
200
                    }
201
                    None => b.add_path(self, path, fs_entry)?,
202
                },
203
                Some(expected) => b.update_path(self, path, fs_entry, expected)?,
204
            }
205
        }
206
        laps.lap("build: paths");
207
        // A directory stays while anything under it exists or is out of
208
        // scope: the directories above each such path, gathered once.
209
        let mut needed_dirs: HashSet<&str> = HashSet::new();
210
        fn parents_of<'a>(p: &'a str, set: &mut HashSet<&'a str>) {
211
            let mut end = p.len();
212
            while let Some(i) = p[..end].rfind('/') {
213
                if !set.insert(&p[..i]) {
214
                    break;
215
                }
216
                end = i;
217
            }
218
        }
219
        for p in fs_entries.keys() {
220
            parents_of(p, &mut needed_dirs);
221
        }
222
        for p in base_snapshot.files.keys() {
223
            if !self.in_scope(p) {
224
                parents_of(p, &mut needed_dirs);
225
            }
226
        }
227
        for (path, expected) in &base_snapshot.files {
228
            if fs_entries.contains_key(path)
229
                || !self.in_scope(path)
230
                || renamed_to.contains_key(path.as_str())
231
            {
232
                continue;
233
            }
234
            let still_needed = expected.kind == NodeKind::Dir
235
                && match dir_needed {
236
                    Some(f) => f(path),
237
                    None => needed_dirs.contains(path.as_str()),
238
                };
239
            if !still_needed {
240
                b.remove_path(path, expected);
241
            }
242
        }
243
        laps.lap("build: removals");
244
        if seed.is_some() {
245
            b.retire_stray_placements();
246
        }
247
        laps.lap("build: stray placements");
248
        let mut statuses = b.statuses;
249
        statuses.sort_by(|a, b| a.path.cmp(&b.path));
250
        Ok(BuiltChange {
251
            tree_ops: b.tree_ops,
252
            edits: b.edits,
253
            contributors: b.contributors.into_iter().collect(),
254
            resolves: b.resolves,
255
            statuses,
256
            blobs: b.blobs,
257
        })
258
    }
259
260
    /// Store a change and, when `core.sign = change` and we are its author,
261
    /// sign it. A rewritten change by another author is stored unsigned: only
262
    /// its author (or a delegate) can vouch for it.
263
    pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> {
264
        let ours = change.author == self.author();
265
        if self.config.sign_changes && ours && self.secret_key()?.is_none() {
266
            return fail("core.sign is \"change\" but no secret key is available for author.key");
267
        }
268
        let hash = self.store.put_change(change)?;
269
        if self.config.sign_changes && ours {
270
            self.sign_change(&hash)?;
271
        }
272
        Ok(hash)
273
    }
274
275
    /// Snapshot the working copy into the working-copy change (spec §12.3).
276
    /// The result carries the full base state and its whole rendering.
277
    pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> {
278
        self.snapshot_as(command, false)
279
    }
280
281
    /// Snapshot the working copy as [`Repo::snapshot`] does, for commands
282
    /// that need its statuses, conflicts and base history, and the graphs
283
    /// of the files it changes: status, log, show, commit, diff, hunks and
284
    /// discard. Text files known unchanged since the last manifest are
285
    /// neither read nor rendered: the result's `base` is a history-only
286
    /// state without their graphs, and its `base_snapshot` lists them in
287
    /// `unrendered` with empty bytes. They are never conflicted, and no
288
    /// change the working copy makes touches them.
289
    pub fn snapshot_status(&self, command: &str) -> Result<SnapshotResult> {
290
        self.snapshot_as(command, true)
291
    }
292
293
    fn snapshot_as(&self, command: &str, partial: bool) -> Result<SnapshotResult> {
294
        let _lock = self.lock_snapshot()?;
295
        let batch = self.store.begin_batch()?;
296
        let result = self.snapshot_locked(command, partial);
297
        batch.finish()?;
298
        result
299
    }
300
301
    fn snapshot_locked(&self, command: &str, partial: bool) -> Result<SnapshotResult> {
302
        let _all = crate::timing::phase("snapshot");
303
        let (op_ref, op) = self.head_op()?;
304
        let (old_hash, old_wc) = self.working_copy()?;
305
        // The base's history first: its graph keys say which files the
306
        // manifest already knows equal to it.
307
        let mut base = {
308
            let _t = crate::timing::phase("snapshot: load base history");
309
            self.load_state_history(&old_wc.deps)?
310
        };
311
        let view = {
312
            let _t = crate::timing::phase("snapshot: tree view");
313
            base.tree().view()
314
        };
315
        let manifest = {
316
            let _t = crate::timing::phase("snapshot: read manifest");
317
            self.read_manifest()?.map(|(_, _, m)| m)
318
        };
319
        let tracked: BTreeMap<String, (NodeKind, Option<ObjectRef>)> = {
320
            let _t = crate::timing::phase("snapshot: tracked paths");
321
            let mut t = BTreeMap::new();
322
            for (p, e) in view.entries.iter() {
323
                if e.kind == NodeKind::Dir {
324
                    continue;
325
                }
326
                let blob = if e.kind != NodeKind::File || e.mode.opaque {
327
                    base.live_blobs(&e.node).first().copied()
328
                } else {
329
                    None
330
                };
331
                t.insert(p.clone(), (e.kind, blob));
332
            }
333
            t
334
        };
335
        // A text file's graph key, worked out when the walk asks for it.
336
        let key_of = |p: &str| -> Option<[u8; 32]> {
337
            let e = view.entries.get(p)?;
338
            if e.kind != NodeKind::File || e.mode.opaque {
339
                return None;
340
            }
341
            base.graph_key(&e.node)
342
        };
343
        let mut fs_entries = {
344
            let _t = crate::timing::phase("snapshot: read working copy");
345
            self.walk_working_copy_with(manifest.as_deref(), &tracked, Some(&key_of))?
346
        };
347
        // The keys of the files read, before building graphs drops them: a
348
        // file found equal to its base rendering keeps its key.
349
        let keys: HashMap<String, [u8; 32]> = fs_entries
350
            .iter()
351
            .filter(|(_, e)| !e.unchanged && e.kind == NodeKind::File)
352
            .filter_map(|(p, _)| Some((p.clone(), key_of(p)?)))
353
            .collect();
354
        let base_snapshot = {
355
            let _t = crate::timing::phase("snapshot: load and render base");
356
            if partial {
357
                let skip: HashSet<NodeId> = fs_entries
358
                    .iter()
359
                    .filter(|(_, e)| e.unchanged)
360
                    .filter_map(|(p, _)| view.entries.get(p).map(|e| e.node))
361
                    .collect();
362
                let need: Vec<NodeId> = view
363
                    .entries
364
                    .values()
365
                    .filter(|e| e.kind == NodeKind::File && !skip.contains(&e.node))
366
                    .map(|e| e.node)
367
                    .collect();
368
                {
369
                    let _t = crate::timing::phase("snapshot:   of which building graphs");
370
                    self.load_graphs_of(&mut base, &need)?;
371
                }
372
                let _t = crate::timing::phase("snapshot:   of which rendering");
373
                base.materialize_except(&skip)
374
            } else {
375
                self.load_all_graphs(&mut base)?;
376
                base.materialize()
377
            }
378
        };
379
380
        // Reuse the NodeIds the previous working-copy version assigned to new
381
        // paths, so an unchanged working copy hashes identically (spec §12.3).
382
        let reuse = added_node_paths(&old_wc, &base_snapshot);
383
        self.platform_normalize(&mut fs_entries, &base_snapshot);
384
        let built = {
385
            let _t = crate::timing::phase("snapshot: diff against base");
386
            self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)?
387
        };
388
        // Text files found equal to an unconflicted base rendering keep its
389
        // graph key, so the next snapshot need not read or render them.
390
        {
391
            let changed: HashSet<&str> = built.statuses.iter().map(|s| s.path.as_str()).collect();
392
            for (p, e) in fs_entries.iter_mut() {
393
                if e.unchanged || e.kind != NodeKind::File || changed.contains(p.as_str()) {
394
                    continue;
395
                }
396
                e.graph_key = match base_snapshot.files.get(p).map(|f| &f.content) {
397
                    Some(FileContent::Text {
398
                        bytes,
399
                        materialized,
400
                    }) if !materialized.is_conflicted() && *bytes == *e.content => {
401
                        keys.get(p).copied()
402
                    }
403
                    _ => None,
404
                };
405
            }
406
        }
407
408
        let mut wc = Change {
409
            version: crate::objects::CHANGE_VERSION,
410
            change_id: old_wc.change_id,
411
            deps: vec![],
412
            author: self.author(),
413
            hlc: old_wc.hlc,
414
            message: old_wc.message.clone(),
415
            tree_ops: built.tree_ops,
416
            edits: built.edits,
417
            kind: ChangeKind::WorkingCopy,
418
        };
419
        let base_heads = base.maximal(&old_wc.deps);
420
        let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect();
421
        deps.extend(referenced_changes(&wc, &base));
422
        deps.extend(built.contributors.iter().copied());
423
        wc.deps = deps.into_iter().collect();
424
        wc.normalize();
425
        let resolves = built.resolves;
426
        let contributors = built.contributors;
427
        let statuses = built.statuses;
428
        // Empty snapshot: hash the candidate before storing; only record an op on change.
429
        let record = crate::timing::phase("snapshot: hash and record");
430
        // Encoded once, its texts stored a batch at a time as they are
431
        // made (a stored blob is not stored again); the change itself only
432
        // when it changed.
433
        let encoded = wc.encode_streaming(|batch| self.store.put_texts(&batch))?;
434
        let new_hash = crate::hash::hash_object(crate::hash::ObjectType::Change, &encoded);
435
        let changed = new_hash != old_hash;
436
        if changed {
437
            self.store.put(crate::hash::ObjectType::Change, &encoded)?;
438
            let mut view = op.view.clone();
439
            view.workspaces.insert(self.workspace, new_hash);
440
            self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?;
441
        }
442
        drop(record);
443
        // The prompt compares against this without recording anything.
444
        let _t = crate::timing::phase("snapshot: write manifest");
445
        let conflicted = base_snapshot.is_conflicted();
446
        let uncommitted = !wc.tree_ops.is_empty() || !wc.edits.is_empty();
447
        // Rewriting the manifest pays only when it would teach the next
448
        // snapshot something: other flags, a file found equal to the base
449
        // (it need not be read again), a new blob, a path added or gone.
450
        // An edited file is read next time whatever it says.
451
        let worth = match self.read_manifest()? {
452
            None => true,
453
            Some((c, u, m)) => {
454
                c != conflicted
455
                    || u != uncommitted
456
                    || m.entries.len() != fs_entries.len()
457
                    || fs_entries.iter().any(|(p, e)| {
458
                        !e.unchanged && (e.graph_key.is_some() || !m.entries.contains_key(p))
459
                    })
460
                    || built
461
                        .blobs
462
                        .iter()
463
                        .any(|(p, b)| m.entries.get(p).and_then(|e| e.blob) != Some(*b))
464
            }
465
        };
466
        if worth {
467
            self.write_manifest_entries(conflicted, uncommitted, &fs_entries, &built.blobs)?;
468
        }
469
        Ok(SnapshotResult {
470
            wc_hash: new_hash,
471
            wc,
472
            base,
473
            base_snapshot,
474
            statuses,
475
            changed,
476
            resolves,
477
            contributors,
478
        })
479
    }
480
481
    /// `oxy status --json` (spec §9.5): the changed paths and every conflict
482
    /// of the working copy's base with its kind, path, and contributing
483
    /// ChangeIds.
484
    pub fn status_json(&self, snap: &SnapshotResult) -> Result<String> {
485
        let heads = self.bookmark_heads()?;
486
        let base_snapshot = &snap.base_snapshot;
487
        let by_node = base_snapshot.tree.by_node();
488
        let path_of = |n: &NodeId| by_node.get(n).map(|e| e.path.clone());
489
        let id_of = |h: &ChangeHash| {
490
            snap.base
491
                .get(h)
492
                .map(|m| m.change_id.to_hex())
493
                .unwrap_or_else(|| h.to_hex())
494
        };
495
        let paths = json_array(snap.statuses.iter().map(|s| {
496
            format!(
497
                "{{\"path\":{},\"status\":{}}}",
498
                json_str(&s.path),
499
                json_str(&format!("{:?}", s.status))
500
            )
501
        }));
502
        let mut conflicts: Vec<String> = Vec::new();
503
        for (p, f) in &base_snapshot.files {
504
            if !f.is_conflicted() {
505
                continue;
506
            }
507
            let (kind, changes) = match &f.content {
508
                FileContent::Text { .. } => {
509
                    ("text", snap.base.conflict_regions(&f.node).contributors)
510
                }
511
                FileContent::Opaque { blobs } => {
512
                    ("replace", snap.base.blob_contributors(&f.node, blobs))
513
                }
514
                FileContent::Dir => continue,
515
            };
516
            let resolved = match snap
517
                .statuses
518
                .iter()
519
                .find(|s| s.path == *p)
520
                .map(|s| s.status)
521
            {
522
                Some(ChangeStatus::Resolved) => "true",
523
                Some(ChangeStatus::ConflictEdited) => "\"markers\"",
524
                _ => "false",
525
            };
526
            conflicts.push(format!(
527
                "{{\"kind\":{},\"path\":{},\"node\":{},\"changes\":{},\"resolved\":{resolved}}}",
528
                json_str(kind),
529
                json_str(p),
530
                json_str(&f.node.to_hex()),
531
                json_array(changes.iter().map(|h| json_str(&id_of(h))))
532
            ));
533
        }
534
        for c in &base_snapshot.tree.conflicts {
535
            let (kind, nodes): (&str, Vec<NodeId>) = match c {
536
                crate::tree::TreeConflict::Name { nodes, .. } => ("name", nodes.clone()),
537
                crate::tree::TreeConflict::MultiPlacement { node, .. } => {
538
                    ("multi-placement", vec![*node])
539
                }
540
                crate::tree::TreeConflict::Orphan { node, .. } => ("orphan", vec![*node]),
541
                crate::tree::TreeConflict::Cycle { node } => ("cycle", vec![*node]),
542
                crate::tree::TreeConflict::Mode { node, .. } => ("mode", vec![*node]),
543
            };
544
            let detail = match c {
545
                crate::tree::TreeConflict::Name { name, parent, .. } => {
546
                    let dir = if *parent == NodeId::ROOT {
547
                        String::new()
548
                    } else {
549
                        path_of(parent).unwrap_or_default()
550
                    };
551
                    format!(
552
                        ",\"path\":{}",
553
                        json_str(&if dir.is_empty() {
554
                            name.clone()
555
                        } else {
556
                            format!("{dir}/{name}")
557
                        })
558
                    )
559
                }
560
                _ => nodes
561
                    .first()
562
                    .and_then(path_of)
563
                    .map(|p| format!(",\"path\":{}", json_str(&p)))
564
                    .unwrap_or_default(),
565
            };
566
            conflicts.push(format!(
567
                "{{\"kind\":{},\"nodes\":{}{detail}}}",
568
                json_str(kind),
569
                json_array(nodes.iter().map(|n| json_str(&n.to_hex())))
570
            ));
571
        }
572
        Ok(format!(
573
            "{{\"bookmark\":{},\"working_copy\":{},\"heads\":{},\"clean\":{},\"conflicted\":{},\"paths\":{paths},\"conflicts\":{}}}",
574
            json_str(&self.config.bookmark),
575
            json_str(&snap.wc.change_id.to_hex()),
576
            heads.len(),
577
            snap.statuses.is_empty(),
578
            base_snapshot.is_conflicted(),
579
            json_array(conflicts)
580
        ))
581
    }
582
583
    /// Turn the working-copy change into a normal change on the bookmark (spec §12.3).
584
    pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> {
585
        self.commit_selection(message, extra_deps, &Selection::All)
586
    }
587
588
    /// The hunks of the working copy, for `oxy commit --interactive`: each
589
    /// tree operation and each edit, in the order `select_hunks` numbers them.
590
    pub fn hunks(&self) -> Result<(SnapshotResult, Vec<Hunk>)> {
591
        let snap = self.snapshot_status("oxy commit")?;
592
        let hunks = render_hunks(&snap.wc, &snap.base, &snap.base_snapshot);
593
        Ok((snap, hunks))
594
    }
595
596
    /// Commit part of the working copy (spec §12.3). What is not selected
597
    /// stays on disk as uncommitted work: the next snapshot rebuilds it
598
    /// against the new base, so the working copy always describes the
599
    /// directory.
600
    /// The graph key of each text file of the state at `heads`, by path,
601
    /// from a history-only load that builds no graph. Files with an `eol`
602
    /// attribute are left out: their bytes on disk are not the rendering.
603
    fn committed_graph_keys(&self, st: &State) -> HashMap<String, [u8; 32]> {
604
        let _t = crate::timing::phase("commit: graph keys");
605
        let attrs = self.disk_attributes();
606
        st.tree()
607
            .view()
608
            .entries
609
            .iter()
610
            .filter(|(p, e)| {
611
                e.kind == NodeKind::File && !e.mode.opaque && attrs.lookup(p).eol.is_none()
612
            })
613
            .filter_map(|(p, e)| Some((p.clone(), st.graph_key(&e.node)?)))
614
            .collect()
615
    }
616
617
    pub fn commit_selection(
618
        &self,
619
        message: &str,
620
        extra_deps: &[ChangeHash],
621
        selection: &Selection,
622
    ) -> Result<ChangeHash> {
623
        let _wc = self.lock_working_copy()?;
624
        let mut snap = self.snapshot_status("oxy commit")?;
625
        if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() {
626
            return fail("nothing to commit: the working copy matches its base");
627
        }
628
        if let Some(s) = snap
629
            .statuses
630
            .iter()
631
            .find(|s| s.status == ChangeStatus::ConflictEdited)
632
        {
633
            return fail(format!("{} still contains conflict markers", s.path));
634
        }
635
        if message.trim().is_empty() {
636
            return fail("a commit message is required");
637
        }
638
        // What the whole working copy references, before a whole commit
639
        // takes its operations over (they are moved, not copied: for a
640
        // large tree they are most of the memory).
641
        let wc_refs = referenced_changes(&snap.wc, &snap.base);
642
        let (tree_ops, edits, partial) = match selection {
643
            Selection::All => (
644
                std::mem::take(&mut snap.wc.tree_ops),
645
                std::mem::take(&mut snap.wc.edits),
646
                false,
647
            ),
648
            Selection::Paths(paths) => {
649
                let (o, e) = select_paths(&snap.wc, &snap.base_snapshot, paths);
650
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
651
                (o, e, partial)
652
            }
653
            Selection::Hunks(indices) => {
654
                let (o, e) = select_hunks(&snap.wc, indices);
655
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
656
                (o, e, partial)
657
            }
658
        };
659
        if tree_ops.is_empty() && edits.is_empty() {
660
            return fail("nothing selected to commit");
661
        }
662
        if partial && snap.resolves {
663
            return fail("a conflict resolution must be committed whole");
664
        }
665
        // pre-commit hook: may veto.
666
        self.run_hook(
667
            "pre-commit",
668
            &format!(
669
                "{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}",
670
                json_str(message),
671
                json_array(snap.statuses.iter().map(|s| format!(
672
                    "{{\"path\":{},\"status\":{}}}",
673
                    json_str(&s.path),
674
                    json_str(&format!("{:?}", s.status))
675
                )))
676
            ),
677
        )?;
678
        let (op_ref, op) = self.head_op()?;
679
        let mut deps: BTreeSet<ChangeHash> = wc_refs;
680
        // semantic-deps hook: may add explicit dependencies (spec §8.1).
681
        if self.config.semantic_deps {
682
            let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
683
            let by_node = snap.base_snapshot.tree.by_node();
684
            let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new();
685
            let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
686
            for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) {
687
                if !seen.insert(*h) {
688
                    continue;
689
                }
690
                let c = self.store.get_change(h)?;
691
                let mut paths: BTreeSet<String> = BTreeSet::new();
692
                for n in c
693
                    .tree_ops
694
                    .iter()
695
                    .map(|o| o.node())
696
                    .chain(c.edits.iter().map(|e| e.node()))
697
                {
698
                    if let Some(e) = by_node.get(&n) {
699
                        paths.insert(e.path.clone());
700
                    }
701
                }
702
                candidates.push((c.change_id, *h, paths.into_iter().collect()));
703
            }
704
            for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? {
705
                if snap.base.contains(&d) {
706
                    deps.insert(d);
707
                }
708
            }
709
        }
710
        // A resolution depends on every change that contributed to the
711
        // conflict (spec §8.1). Tree and opaque conflicts are resolved through
712
        // ordinary operations whose references already cover the contributors.
713
        deps.extend(snap.contributors.iter().copied());
714
        for d in extra_deps {
715
            if !snap.base.contains(d) {
716
                return fail(format!(
717
                    "--depends-on {d}: not in the working copy's base state"
718
                ));
719
            }
720
            deps.insert(*d);
721
        }
722
        let mut change = Change {
723
            version: crate::objects::CHANGE_VERSION,
724
            change_id: if partial {
725
                ChangeId::random()
726
            } else {
727
                snap.wc.change_id
728
            },
729
            deps: vec![],
730
            author: self.author(),
731
            hlc: Hlc::now(),
732
            message: message.trim_end().to_string(),
733
            tree_ops,
734
            edits,
735
            kind: if snap.resolves {
736
                ChangeKind::Resolution
737
            } else {
738
                ChangeKind::Normal
739
            },
740
        };
741
        if partial {
742
            // Dependencies come from what the selected part references, not
743
            // from the whole working copy.
744
            deps = referenced_changes(&change, &snap.base);
745
            for d in extra_deps {
746
                deps.insert(*d);
747
            }
748
        }
749
        change.deps = deps.into_iter().collect();
750
        change.normalize();
751
        let hash = self.store_signed(&change)?;
752
753
        // New bookmark heads: maximal elements of old heads plus the new change.
754
        // The working copy continues on its own base plus the new change, which
755
        // is exactly what the files on disk contain; the bookmark may hold
756
        // other heads (and conflicts) the working copy has not checked out.
757
        let old_heads = self.heads_of(&op.view, &self.config.bookmark)?;
758
        let old_base = snap.base.maximal(&snap.wc.deps);
759
        let mut all: Vec<ChangeHash> = old_heads.clone();
760
        all.extend(old_base.iter().copied());
761
        all.push(hash);
762
        // The snapshot already loaded the working copy's base; when that also
763
        // holds every bookmark head, it is the state to extend.
764
        let mut st = if old_heads.iter().all(|h| snap.base.contains(h)) {
765
            snap.base
766
        } else {
767
            self.load_state(&all)?
768
        };
769
        st.add_hashed(hash, change)?;
770
        let mut with_new = old_heads.clone();
771
        with_new.push(hash);
772
        let new_heads = st.maximal(&with_new);
773
        // Automatic checkpoint once enough changes accumulated (spec §11).
774
        let since = self
775
            .latest_checkpoint(&self.config.bookmark)?
776
            .map(|(_, cp)| cp.count)
777
            .unwrap_or(0);
778
        if self.config.checkpoint_interval > 0
779
            && (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval
780
        {
781
            let cp_state = self.load_state_full(&new_heads)?;
782
            self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?;
783
        }
784
        let mut wc_base = old_base.clone();
785
        wc_base.push(hash);
786
        let wc_base = st.maximal(&wc_base);
787
788
        // The graph keys of the files as they now are, from the state the
789
        // commit just extended: a file whose graph it built has none, and
790
        // is read by the next snapshot; a key the next base does not share
791
        // (the bookmark held other heads) is simply not trusted then.
792
        let committed_keys = (!partial).then(|| self.committed_graph_keys(&st));
793
        let wc = self.new_working_copy(wc_base, "");
794
        let wc_hash = self.store.put_change(&wc)?;
795
        let mut view = op.view.clone();
796
        view.bookmarks.insert(
797
            self.config.bookmark.clone(),
798
            self.store.put_state(&StateObject::new(new_heads))?,
799
        );
800
        view.workspaces.insert(self.workspace, wc_hash);
801
        self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?;
802
        if partial {
803
            // The rest is rebuilt from disk by the next snapshot.
804
            self.drop_manifest();
805
        } else {
806
            let conflicted = snap.base_snapshot.is_conflicted() && !snap.resolves;
807
            // The files on disk are now exactly the new base's renderings:
808
            // record their graph keys, so the next snapshot neither reads nor
809
            // renders them (oxygen#35).
810
            let keys = if conflicted {
811
                HashMap::new()
812
            } else {
813
                committed_keys.unwrap_or_default()
814
            };
815
            self.rewrite_manifest(conflicted, false, &keys)?;
816
        }
817
        self.maybe_autopack()?;
818
        let _ = self.run_hook(
819
            "post-commit",
820
            &format!(
821
                "{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}",
822
                json_str(&snap.wc.change_id.to_hex()),
823
                json_str(&hash.to_hex()),
824
                json_str(message)
825
            ),
826
        );
827
        Ok(hash)
828
    }
829
}
830
831
/// What part of the working copy a commit takes (spec §12.3).
832
#[derive(Clone, Debug)]
833
pub enum Selection {
834
    All,
835
    /// Every operation and edit on nodes under these paths.
836
    Paths(Vec<String>),
837
    /// Hunk indices as `Repo::hunks` numbers them; dependent hunks are added.
838
    Hunks(Vec<usize>),
839
}
840
841
/// One selectable piece of the working copy: a tree operation or an edit.
842
#[derive(Clone, Debug)]
843
pub struct Hunk {
844
    pub index: usize,
845
    pub path: String,
846
    /// Rendered like `oxy diff`.
847
    pub text: String,
848
}
849
850
/// The tree operations and edits on nodes under `paths`, plus the
851
/// additions of any new parent directories they need.
852
fn select_paths(
853
    wc: &Change,
854
    base_snapshot: &Snapshot,
855
    paths: &[String],
856
) -> (Vec<TreeOp>, Vec<Edit>) {
857
    let selected = |p: &str| {
858
        paths
859
            .iter()
860
            .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
861
    };
862
    let by_node = base_snapshot.tree.by_node();
863
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
864
    for op in &wc.tree_ops {
865
        if let TreeOp::AddNode {
866
            node,
867
            parent,
868
            name,
869
            kind,
870
            ..
871
        } = op
872
        {
873
            new_names.insert(*node, (*parent, name.clone(), *kind));
874
        }
875
    }
876
    let mut nodes: BTreeSet<NodeId> = BTreeSet::new();
877
    for n in wc
878
        .tree_ops
879
        .iter()
880
        .map(|o| o.node())
881
        .chain(wc.edits.iter().map(|e| e.node()))
882
    {
883
        if path_of_node(&n, &by_node, &new_names).is_some_and(|p| selected(&p)) {
884
            nodes.insert(n);
885
        }
886
    }
887
    // New parents of selected new nodes come along.
888
    let mut changed = true;
889
    while changed {
890
        changed = false;
891
        for (n, (parent, _, _)) in &new_names {
892
            if nodes.contains(n) && new_names.contains_key(parent) && nodes.insert(*parent) {
893
                changed = true;
894
            }
895
        }
896
    }
897
    let ops = wc
898
        .tree_ops
899
        .iter()
900
        .filter(|o| nodes.contains(&o.node()))
901
        .cloned()
902
        .collect();
903
    let edits = wc
904
        .edits
905
        .iter()
906
        .filter(|e| nodes.contains(&e.node()))
907
        .cloned()
908
        .collect();
909
    (ops, edits)
910
}
911
912
/// The hunks `indices` name plus whatever they refer to within the working
913
/// copy (the parent directory of an added node, the insert that introduced
914
/// an anchor line), renumbered so the result is a well-formed change.
915
fn select_hunks(wc: &Change, indices: &[usize]) -> (Vec<TreeOp>, Vec<Edit>) {
916
    let n_ops = wc.tree_ops.len();
917
    let mut ops_sel = vec![false; n_ops];
918
    let mut edits_sel = vec![false; wc.edits.len()];
919
    for &i in indices {
920
        if i < n_ops {
921
            ops_sel[i] = true;
922
        } else if i - n_ops < wc.edits.len() {
923
            edits_sel[i - n_ops] = true;
924
        }
925
    }
926
    // Atom ranges each insert introduces, per node, in the working copy's numbering.
927
    let mut per_node: HashMap<NodeId, u32> = HashMap::new();
928
    let mut insert_ranges: Vec<Option<(NodeId, u32, u32)>> = Vec::new();
929
    for e in &wc.edits {
930
        insert_ranges.push(match e {
931
            Edit::Insert { node, lines, .. } => {
932
                let start = per_node.entry(*node).or_default();
933
                let r = (*node, *start, *start + lines.len() as u32);
934
                *start += lines.len() as u32;
935
                Some(r)
936
            }
937
            _ => None,
938
        });
939
    }
940
    let add_index: HashMap<NodeId, usize> = wc
941
        .tree_ops
942
        .iter()
943
        .enumerate()
944
        .filter_map(|(i, o)| {
945
            if let TreeOp::AddNode { node, .. } = o {
946
                Some((*node, i))
947
            } else {
948
                None
949
            }
950
        })
951
        .collect();
952
    let insert_for = |node: &NodeId, index: u32| -> Option<usize> {
953
        insert_ranges
954
            .iter()
955
            .position(|r| matches!(r, Some((n, a, b)) if n == node && *a <= index && index < *b))
956
    };
957
    // Closure.
958
    let mut changed = true;
959
    while changed {
960
        changed = false;
961
        for i in 0..n_ops {
962
            if !ops_sel[i] {
963
                continue;
964
            }
965
            if let TreeOp::AddNode { node, parent, .. } = &wc.tree_ops[i] {
966
                if let Some(&p) = add_index.get(parent) {
967
                    if !ops_sel[p] {
968
                        ops_sel[p] = true;
969
                        changed = true;
970
                    }
971
                }
972
                // A new node's content belongs with its addition: nobody
973
                // wants an empty file committed apart from its lines.
974
                for (j, e) in wc.edits.iter().enumerate() {
975
                    if e.node() == *node && !edits_sel[j] {
976
                        edits_sel[j] = true;
977
                        changed = true;
978
                    }
979
                }
980
            }
981
        }
982
        for j in 0..wc.edits.len() {
983
            if !edits_sel[j] {
984
                continue;
985
            }
986
            let e = &wc.edits[j];
987
            if let Some(&a) = add_index.get(&e.node()) {
988
                if !ops_sel[a] {
989
                    ops_sel[a] = true;
990
                    changed = true;
991
                }
992
            }
993
            if let Edit::Insert {
994
                node,
995
                after,
996
                before,
997
                ..
998
            } = e
999
            {
1000
                for r in [after, before] {
1001
                    if let AtomRef::Atom { change, index } = r {
1002
                        if change.is_zero() {
1003
                            if let Some(k) = insert_for(node, *index) {
1004
                                if !edits_sel[k] {
1005
                                    edits_sel[k] = true;
1006
                                    changed = true;
1007
                                }
1008
                            }
1009
                        }
1010
                    }
1011
                }
1012
            }
1013
        }
1014
    }
1015
    // Renumber self-references: old atom index → new index among selected inserts.
1016
    let mut remap: HashMap<(NodeId, u32), u32> = HashMap::new();
1017
    let mut new_counts: HashMap<NodeId, u32> = HashMap::new();
1018
    for (j, r) in insert_ranges.iter().enumerate() {
1019
        if let (true, Some((node, a, b))) = (edits_sel[j], r) {
1020
            let next = new_counts.entry(*node).or_default();
1021
            for old in *a..*b {
1022
                remap.insert((*node, old), *next);
1023
                *next += 1;
1024
            }
1025
        }
1026
    }
1027
    let map_ref = |r: AtomRef, node: &NodeId| -> AtomRef {
1028
        match r {
1029
            AtomRef::Atom { change, index } if change.is_zero() => AtomRef::Atom {
1030
                change,
1031
                index: remap.get(&(*node, index)).copied().unwrap_or(index),
1032
            },
1033
            other => other,
1034
        }
1035
    };
1036
    let ops = wc
1037
        .tree_ops
1038
        .iter()
1039
        .enumerate()
1040
        .filter(|(i, _)| ops_sel[*i])
1041
        .map(|(_, o)| o.clone())
1042
        .collect();
1043
    let edits = wc
1044
        .edits
1045
        .iter()
1046
        .enumerate()
1047
        .filter(|(j, _)| edits_sel[*j])
1048
        .map(|(_, e)| match e {
1049
            Edit::Insert {
1050
                node,
1051
                after,
1052
                before,
1053
                lines,
1054
            } => Edit::Insert {
1055
                node: *node,
1056
                after: map_ref(*after, node),
1057
                before: map_ref(*before, node),
1058
                lines: lines.clone(),
1059
            },
1060
            other => other.clone(),
1061
        })
1062
        .collect();
1063
    (ops, edits)
1064
}
1065
1066
/// Render each tree operation and edit of `change` on its own, numbered.
1067
pub fn render_hunks(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<Hunk> {
1068
    let by_node = base_snapshot.tree.by_node();
1069
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
1070
    for op in &change.tree_ops {
1071
        if let TreeOp::AddNode {
1072
            node,
1073
            parent,
1074
            name,
1075
            kind,
1076
            ..
1077
        } = op
1078
        {
1079
            new_names.insert(*node, (*parent, name.clone(), *kind));
1080
        }
1081
    }
1082
    let mut out = Vec::new();
1083
    for (i, op) in change.tree_ops.iter().enumerate() {
1084
        let one = Change {
1085
            tree_ops: vec![op.clone()],
1086
            edits: vec![],
1087
            ..change.clone()
1088
        };
1089
        let path = path_of_node(&op.node(), &by_node, &new_names).unwrap_or_default();
1090
        out.push(Hunk {
1091
            index: i,
1092
            path,
1093
            text: render_change(&one, base, base_snapshot),
1094
        });
1095
    }
1096
    for (j, e) in change.edits.iter().enumerate() {
1097
        let one = Change {
1098
            tree_ops: vec![],
1099
            edits: vec![e.clone()],
1100
            ..change.clone()
1101
        };
1102
        let path = path_of_node(&e.node(), &by_node, &new_names).unwrap_or_default();
1103
        out.push(Hunk {
1104
            index: change.tree_ops.len() + j,
1105
            path,
1106
            text: render_change(&one, base, base_snapshot),
1107
        });
1108
    }
1109
    out
1110
}
1111
1112
/// One file's part of a diff, for `oxy diff --json`.
1113
#[derive(Clone, Debug)]
1114
pub struct FileDiff {
1115
    pub path: String,
1116
    /// `added`, `removed`, `modified`, `renamed`, `mode`, `replaced`.
1117
    pub kind: String,
1118
    pub hunks: Vec<DiffHunk>,
1119
    /// For renames, the previous path.
1120
    pub from: Option<String>,
1121
}
1122
1123
/// Lines inserted after `base_line` (0 = before the first line) and lines
1124
/// removed, with their base line numbers.
1125
#[derive(Clone, Debug, Default)]
1126
pub struct DiffHunk {
1127
    pub base_line: u32,
1128
    pub added: Vec<Vec<u8>>,
1129
    pub removed: Vec<(u32, Vec<u8>)>,
1130
}
1131
1132
impl Repo {
1133
    /// The content of `path` in a state: `heads`, or the working copy's base.
1134
    pub fn file_at(&self, path: &str, heads: Option<&[ChangeHash]>) -> Result<Vec<u8>> {
1135
        let snapshot = match heads {
1136
            Some(h) => self.load_state(h)?.materialize(),
1137
            None => {
1138
                let (_, wc) = self.working_copy()?;
1139
                self.load_state(&wc.deps)?.materialize()
1140
            }
1141
        };
1142
        let Some(f) = snapshot.files.get(path) else {
1143
            return fail(format!("{path} is not in that state"));
1144
        };
1145
        match &f.content {
1146
            FileContent::Dir => fail(format!("{path} is a directory")),
1147
            FileContent::Text { bytes, .. } => Ok(bytes.clone()),
1148
            FileContent::Opaque { blobs } => match blobs.first() {
1149
                Some(b) => self.read_blob(b),
1150
                None => Ok(vec![]),
1151
            },
1152
        }
1153
    }
1154
1155
    /// Restore `paths` (files or directories) to the working copy's base:
1156
    /// edits are undone, additions removed, removals put back. Returns the
1157
    /// number of paths touched.
1158
    pub fn discard(&self, paths: &[String]) -> Result<usize> {
1159
        let _wc = self.lock_working_copy()?;
1160
        let snap = self.snapshot_status("oxy discard")?;
1161
        let under = |p: &str| {
1162
            paths
1163
                .iter()
1164
                .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
1165
        };
1166
        let attrs = Repo::state_attributes(&snap.base_snapshot);
1167
        let mut n = 0;
1168
        for s in &snap.statuses {
1169
            if !under(&s.path) {
1170
                continue;
1171
            }
1172
            match snap.base_snapshot.files.get(&s.path) {
1173
                None => {
1174
                    // Added (or the new name of a rename): remove it.
1175
                    let full = self.root.join(&s.path);
1176
                    if full.is_dir() {
1177
                        let _ = fs::remove_dir_all(&full);
1178
                    } else {
1179
                        let _ = fs::remove_file(&full);
1180
                    }
1181
                    n += 1;
1182
                }
1183
                Some(f) => {
1184
                    match &f.content {
1185
                        FileContent::Dir => fs::create_dir_all(self.root.join(&s.path))?,
1186
                        FileContent::Text { bytes, .. } => {
1187
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&s.path).eol
1188
                                == Some(crate::attributes::Eol::Crlf)
1189
                            {
1190
                                crate::attributes::to_crlf(bytes).into()
1191
                            } else {
1192
                                bytes.as_slice().into()
1193
                            };
1194
                            self.write_file(&s.path, &on_disk, f.mode.exec)?;
1195
                        }
1196
                        FileContent::Opaque { blobs } => {
1197
                            let bytes = match blobs.first() {
1198
                                Some(b) => self.read_blob(b)?,
1199
                                None => vec![],
1200
                            };
1201
                            if f.kind == NodeKind::Symlink {
1202
                                self.write_symlink(&s.path, &bytes)?;
1203
                            } else if f.kind == NodeKind::File {
1204
                                self.write_file(&s.path, &bytes, f.mode.exec)?;
1205
                            }
1206
                        }
1207
                    }
1208
                    n += 1;
1209
                }
1210
            }
1211
        }
1212
        // A rename's old name is not in the statuses; put it back too.
1213
        for (p, f) in &snap.base_snapshot.files {
1214
            if under(p)
1215
                && !self.root.join(p).exists()
1216
                && f.kind != NodeKind::Dir
1217
                && f.kind != NodeKind::Subrepo
1218
                && !snap.statuses.iter().any(|s| s.path == *p)
1219
            {
1220
                let bytes = match &f.content {
1221
                    FileContent::Text { bytes, .. } => bytes.clone(),
1222
                    FileContent::Opaque { blobs } => blobs
1223
                        .first()
1224
                        .map(|b| self.read_blob(b))
1225
                        .transpose()?
1226
                        .unwrap_or_default(),
1227
                    FileContent::Dir => continue,
1228
                };
1229
                if f.kind == NodeKind::Symlink {
1230
                    self.write_symlink(p, &bytes)?
1231
                } else {
1232
                    self.write_file(p, &bytes, f.mode.exec)?
1233
                }
1234
                n += 1;
1235
            }
1236
        }
1237
        self.drop_manifest();
1238
        Ok(n)
1239
    }
1240
1241
    /// Undo the working-copy hunks `indices` (as `hunks` numbers them) on
1242
    /// disk, keeping the others: the files are rewritten from the base plus
1243
    /// the remaining hunks.
1244
    pub fn discard_hunks(&self, indices: &[usize]) -> Result<usize> {
1245
        let _wc = self.lock_working_copy()?;
1246
        let (snap, hunks) = self.hunks()?;
1247
        let keep: Vec<usize> = hunks
1248
            .iter()
1249
            .map(|h| h.index)
1250
            .filter(|i| !indices.contains(i))
1251
            .collect();
1252
        let (ops, edits) = select_hunks(&snap.wc, &keep);
1253
        let dropped_nodes: BTreeSet<NodeId> = hunks
1254
            .iter()
1255
            .filter(|h| indices.contains(&h.index))
1256
            .map(|h| {
1257
                if h.index < snap.wc.tree_ops.len() {
1258
                    snap.wc.tree_ops[h.index].node()
1259
                } else {
1260
                    snap.wc.edits[h.index - snap.wc.tree_ops.len()].node()
1261
                }
1262
            })
1263
            .collect();
1264
        let mut partial = snap.wc.clone();
1265
        partial.tree_ops = ops;
1266
        partial.edits = edits;
1267
        partial.normalize();
1268
        let mut st = snap.base.clone();
1269
        st.add_unchecked(partial);
1270
        // Every file of the target is rendered: build the graphs the
1271
        // partial snapshot left out.
1272
        self.load_all_graphs(&mut st)?;
1273
        let target = st.materialize();
1274
        let by_node_target = target.tree.by_node();
1275
        let by_node_base = snap.base_snapshot.tree.by_node();
1276
        let attrs = Repo::state_attributes(&target);
1277
        let mut n = 0;
1278
        for node in dropped_nodes {
1279
            match by_node_target.get(&node) {
1280
                Some(e) => {
1281
                    let f = &target.files[&e.path];
1282
                    match &f.content {
1283
                        FileContent::Dir => {}
1284
                        FileContent::Text { bytes, .. } => {
1285
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&e.path).eol
1286
                                == Some(crate::attributes::Eol::Crlf)
1287
                            {
1288
                                crate::attributes::to_crlf(bytes).into()
1289
                            } else {
1290
                                bytes.as_slice().into()
1291
                            };
1292
                            self.write_file(&e.path, &on_disk, f.mode.exec)?;
1293
                        }
1294
                        FileContent::Opaque { blobs } => {
1295
                            let bytes = blobs
1296
                                .first()
1297
                                .map(|b| self.read_blob(b))
1298
                                .transpose()?
1299
                                .unwrap_or_default();
1300
                            if f.kind == NodeKind::Symlink {
1301
                                self.write_symlink(&e.path, &bytes)?
1302
                            } else {
1303
                                self.write_file(&e.path, &bytes, f.mode.exec)?
1304
                            }
1305
                        }
1306
                    }
1307
                    // A discarded rename: the new name goes.
1308
                    if let Some(old_path) = snap
1309
                        .statuses
1310
                        .iter()
1311
                        .find(|s| {
1312
                            s.status == ChangeStatus::Renamed && !target.files.contains_key(&s.path)
1313
                        })
1314
                        .map(|s| s.path.clone())
1315
                    {
1316
                        let _ = fs::remove_file(self.root.join(old_path));
1317
                    }
1318
                    n += 1;
1319
                }
1320
                None => {
1321
                    // The node exists in neither target nor base with a live path: an addition to remove.
1322
                    if let Some(added) = snap.statuses.iter().find(|s| {
1323
                        s.status == ChangeStatus::Added
1324
                            && !target.files.contains_key(&s.path)
1325
                            && !by_node_base.contains_key(&node)
1326
                    }) {
1327
                        let full = self.root.join(&added.path);
1328
                        if full.is_dir() {
1329
                            let _ = fs::remove_dir_all(&full);
1330
                        } else {
1331
                            let _ = fs::remove_file(&full);
1332
                        }
1333
                        n += 1;
1334
                    }
1335
                }
1336
            }
1337
        }
1338
        self.drop_manifest();
1339
        Ok(n)
1340
    }
1341
1342
    /// The diff of the working copy (`None`) or of a change, per file with
1343
    /// line-numbered hunks (spec: `oxy diff --json`).
1344
    pub fn diff_files(&self, change: Option<&ChangeHash>) -> Result<Vec<FileDiff>> {
1345
        let (c, base, base_snapshot) = match change {
1346
            Some(h) => {
1347
                let c = self.store.get_change(h)?;
1348
                let (base, snapshot) = self.change_base(&c)?;
1349
                (c, base, snapshot)
1350
            }
1351
            None => {
1352
                let snap = self.snapshot_status("oxy diff")?;
1353
                (snap.wc, snap.base, snap.base_snapshot)
1354
            }
1355
        };
1356
        Ok(diff_files_of(&c, &base, &base_snapshot))
1357
    }
1358
1359
    /// The log restricted to changes touching `path` (its node, so renames
1360
    /// are followed), on the current bookmark.
1361
    pub fn log_for_path(&self, path: &str) -> Result<Vec<LogEntry>> {
1362
        let heads = self.bookmark_heads()?;
1363
        if self.is_bare() {
1364
            return self.log_for_path_in(path, &heads);
1365
        }
1366
        let entries = self.log()?;
1367
        let (_, wc) = self.working_copy()?;
1368
        let base_snapshot = self.load_state(&wc.deps)?.materialize();
1369
        let state_snapshot = self.load_state(&heads)?.materialize();
1370
        let node = base_snapshot
1371
            .files
1372
            .get(path)
1373
            .or_else(|| state_snapshot.files.get(path))
1374
            .map(|f| f.node);
1375
        let Some(node) = node else {
1376
            return fail(format!("{path} is not tracked"));
1377
        };
1378
        Ok(self.entries_touching(entries, node))
1379
    }
1380
1381
    /// The log of the state with these heads restricted to changes touching
1382
    /// `path` (its node, so renames are followed).
1383
    pub fn log_for_path_in(&self, path: &str, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> {
1384
        self.log_for_path_of(&self.load_state_history(heads)?, path, None)
1385
    }
1386
1387
    /// The changes of a loaded state touching `path`, newest first, at most
1388
    /// `limit` of them. The path is found in the state's tree alone, so no
1389
    /// file is rendered, and the walk stops once `limit` are found.
1390
    pub fn log_for_path_of(
1391
        &self,
1392
        state: &State,
1393
        path: &str,
1394
        limit: Option<usize>,
1395
    ) -> Result<Vec<LogEntry>> {
1396
        let Some(node) = state.tree().view().entries.get(path).map(|e| e.node) else {
1397
            return fail(format!("{path} is not in that state"));
1398
        };
1399
        Ok(self.entries_touching_up_to(self.log_of(state), node, limit))
1400
    }
1401
1402
    fn entries_touching(&self, entries: Vec<LogEntry>, node: NodeId) -> Vec<LogEntry> {
1403
        self.entries_touching_up_to(entries, node, None)
1404
    }
1405
1406
    fn entries_touching_up_to(
1407
        &self,
1408
        entries: Vec<LogEntry>,
1409
        node: NodeId,
1410
        limit: Option<usize>,
1411
    ) -> Vec<LogEntry> {
1412
        let mut out = Vec::new();
1413
        for e in entries {
1414
            if limit.is_some_and(|n| out.len() >= n) {
1415
                break;
1416
            }
1417
            let Ok(c) = self.store.get_change(&e.hash) else {
1418
                continue;
1419
            };
1420
            if c.tree_ops.iter().any(|o| o.node() == node)
1421
                || c.edits.iter().any(|x| x.node() == node)
1422
            {
1423
                out.push(e);
1424
            }
1425
        }
1426
        out
1427
    }
1428
}
1429
1430
/// Per-file hunks of `change` against `base`.
1431
pub fn diff_files_of(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<FileDiff> {
1432
    let by_node = base_snapshot.tree.by_node();
1433
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
1434
    for op in &change.tree_ops {
1435
        if let TreeOp::AddNode {
1436
            node,
1437
            parent,
1438
            name,
1439
            kind,
1440
            ..
1441
        } = op
1442
        {
1443
            new_names.insert(*node, (*parent, name.clone(), *kind));
1444
        }
1445
    }
1446
    let path_of = |n: &NodeId| -> String {
1447
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
1448
    };
1449
    let mut files: BTreeMap<NodeId, FileDiff> = BTreeMap::new();
1450
    let mut order: Vec<NodeId> = Vec::new();
1451
    let mut entry = |files: &mut BTreeMap<NodeId, FileDiff>, n: NodeId, kind: &str| {
1452
        if let std::collections::btree_map::Entry::Vacant(v) = files.entry(n) {
1453
            order.push(n);
1454
            v.insert(FileDiff {
1455
                path: path_of(&n),
1456
                kind: kind.to_string(),
1457
                hunks: vec![],
1458
                from: None,
1459
            });
1460
        }
1461
    };
1462
    for op in &change.tree_ops {
1463
        match op {
1464
            TreeOp::AddNode { node, kind, .. } => {
1465
                if *kind != NodeKind::Dir {
1466
                    entry(&mut files, *node, "added");
1467
                }
1468
            }
1469
            TreeOp::RemoveNode { node, .. } => {
1470
                entry(&mut files, *node, "removed");
1471
                if let Some(e) = by_node.get(node) {
1472
                    files.get_mut(node).unwrap().path = e.path.clone();
1473
                }
1474
            }
1475
            TreeOp::Move {
1476
                node, parent, name, ..
1477
            } => {
1478
                entry(&mut files, *node, "renamed");
1479
                let f = files.get_mut(node).unwrap();
1480
                f.from = by_node.get(node).map(|e| e.path.clone());
1481
                let dir = if parent.is_root() {
1482
                    String::new()
1483
                } else {
1484
                    path_of_node(parent, &by_node, &new_names).unwrap_or_default()
1485
                };
1486
                f.path = if dir.is_empty() {
1487
                    name.clone()
1488
                } else {
1489
                    format!("{dir}/{name}")
1490
                };
1491
            }
1492
            TreeOp::SetMode { node, .. } => entry(&mut files, *node, "mode"),
1493
        }
1494
    }
1495
    // Base line numbers per node, 1-based.
1496
    let mut line_of: HashMap<NodeId, HashMap<(ChangeHash, u32), u32>> = HashMap::new();
1497
    let mut lines_for = |n: &NodeId| -> HashMap<(ChangeHash, u32), u32> {
1498
        line_of
1499
            .entry(*n)
1500
            .or_insert_with(|| {
1501
                base.live_lines(n)
1502
                    .into_iter()
1503
                    .enumerate()
1504
                    .map(|(i, (k, _))| (k, i as u32 + 1))
1505
                    .collect()
1506
            })
1507
            .clone()
1508
    };
1509
    for e in &change.edits {
1510
        let n = e.node();
1511
        let is_new = new_names.contains_key(&n);
1512
        entry(&mut files, n, if is_new { "added" } else { "modified" });
1513
        let f = files.get_mut(&n).unwrap();
1514
        match e {
1515
            Edit::Insert { after, lines, .. } => {
1516
                let map = lines_for(&n);
1517
                let base_line = match after {
1518
                    AtomRef::Start => 0,
1519
                    AtomRef::End => map.len() as u32,
1520
                    AtomRef::Atom { change, index } if change.is_zero() => {
1521
                        f.hunks.last().map(|h| h.base_line).unwrap_or(0)
1522
                    }
1523
                    AtomRef::Atom { change, index } => {
1524
                        map.get(&(*change, *index)).copied().unwrap_or(0)
1525
                    }
1526
                };
1527
                f.hunks.push(DiffHunk {
1528
                    base_line,
1529
                    added: lines.to_vec(),
1530
                    removed: vec![],
1531
                });
1532
            }
1533
            Edit::Delete { atoms, .. } => {
1534
                let map = lines_for(&n);
1535
                let mut removed = Vec::new();
1536
                for r in atoms {
1537
                    for i in r.from..r.to {
1538
                        let line = map.get(&(r.change, i)).copied().unwrap_or(0);
1539
                        let text = base
1540
                            .file(&n)
1541
                            .and_then(|g| g.content(&(r.change, i)))
1542
                            .unwrap_or(b"")
1543
                            .to_vec();
1544
                        removed.push((line, text));
1545
                    }
1546
                }
1547
                let base_line = removed
1548
                    .first()
1549
                    .map(|(l, _)| l.saturating_sub(1))
1550
                    .unwrap_or(0);
1551
                f.hunks.push(DiffHunk {
1552
                    base_line,
1553
                    added: vec![],
1554
                    removed,
1555
                });
1556
            }
1557
            Edit::Replace { .. } => {
1558
                if !is_new {
1559
                    f.kind = "replaced".into();
1560
                }
1561
            }
1562
        }
1563
    }
1564
    order.into_iter().filter_map(|n| files.remove(&n)).collect()
1565
}
1566
1567
/// Builds the tree ops and edits of a working-copy change against a base.
1568
/// `BLAKE3("oxy node 1" 0 seed kind path)`, truncated: a node identifier
1569
/// that the same seed and path always give (never the root's).
1570
fn seeded_node(seed: &[u8; 32], path: &str, kind: NodeKind) -> NodeId {
1571
    let mut h = blake3::Hasher::new();
1572
    h.update(b"oxy node 1\0");
1573
    h.update(seed);
1574
    h.update(&[match kind {
1575
        NodeKind::File => 1,
1576
        NodeKind::Dir => 2,
1577
        NodeKind::Symlink => 3,
1578
        NodeKind::Subrepo => 4,
1579
    }]);
1580
    h.update(path.as_bytes());
1581
    let mut id = [0u8; 16];
1582
    id.copy_from_slice(&h.finalize().as_bytes()[..16]);
1583
    if id == [0; 16] {
1584
        id[15] = 1;
1585
    }
1586
    NodeId(id)
1587
}
1588
1589
struct ChangeBuilder<'a> {
1590
    base: &'a State,
1591
    base_snapshot: &'a Snapshot,
1592
    tree_ops: Vec<TreeOp>,
1593
    edits: Vec<Edit>,
1594
    statuses: Vec<PathStatus>,
1595
    dir_nodes: HashMap<String, NodeId>,
1596
    reuse: HashMap<String, (NodeId, NodeKind)>,
1597
    /// Changes that contributed to conflicts this working copy resolves.
1598
    contributors: BTreeSet<ChangeHash>,
1599
    resolves: bool,
1600
    attrs: crate::attributes::Attributes,
1601
    blobs: HashMap<String, ObjectRef>,
1602
    /// Opaque files whose replace conflict the content settles.
1603
    resolve: HashSet<String>,
1604
    /// When set, new nodes' identifiers derive from it and their paths.
1605
    node_seed: Option<[u8; 32]>,
1606
    /// For a Git import, the base's dead directories by where they were
1607
    /// (see `ensure_dir`), worked out when first needed.
1608
    dead_dirs: Option<HashMap<(NodeId, String), (NodeId, crate::tree::OpKey)>>,
1609
}
1610
1611
impl<'a> ChangeBuilder<'a> {
1612
    fn new(
1613
        base: &'a State,
1614
        base_snapshot: &'a Snapshot,
1615
        reuse: HashMap<String, (NodeId, NodeKind)>,
1616
    ) -> Self {
1617
        let mut dir_nodes: HashMap<String, NodeId> = HashMap::new();
1618
        dir_nodes.insert(String::new(), NodeId::ROOT);
1619
        for (p, e) in base_snapshot.tree.entries.iter() {
1620
            if e.kind == NodeKind::Dir {
1621
                dir_nodes.insert(p.clone(), e.node);
1622
            }
1623
        }
1624
        ChangeBuilder {
1625
            base,
1626
            base_snapshot,
1627
            tree_ops: vec![],
1628
            edits: vec![],
1629
            statuses: vec![],
1630
            dir_nodes,
1631
            reuse,
1632
            contributors: BTreeSet::new(),
1633
            resolves: false,
1634
            attrs: crate::attributes::Attributes::default(),
1635
            blobs: HashMap::new(),
1636
            resolve: HashSet::new(),
1637
            node_seed: None,
1638
            dead_dirs: None,
1639
        }
1640
    }
1641
1642
    fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self {
1643
        self.attrs = attrs;
1644
        self
1645
    }
1646
1647
    /// Whether a file is opaque: forced by `binary`, forced text by `text`
1648
    /// only when it has no NUL bytes, else sniffed.
1649
    /// The blob for an opaque entry: the cached one, else stored now.
1650
    fn blob_of(
1651
        &mut self,
1652
        repo: &Repo,
1653
        path: &str,
1654
        e: &FsEntry,
1655
        nocompress: bool,
1656
    ) -> Result<ObjectRef> {
1657
        let b = match e.blob {
1658
            Some(b) => b,
1659
            None => repo.store_blob_for(&e.content, nocompress)?,
1660
        };
1661
        self.blobs.insert(path.to_string(), b);
1662
        Ok(b)
1663
    }
1664
1665
    fn opaque_for(&self, path: &str, e: &FsEntry) -> bool {
1666
        if e.blob.is_some() {
1667
            return true;
1668
        }
1669
        if e.kind == NodeKind::Symlink || e.kind == NodeKind::Subrepo {
1670
            return true;
1671
        }
1672
        match self.attrs.lookup(path).binary {
1673
            Some(true) => true,
1674
            Some(false) => is_binary(&e.content),
1675
            None => is_binary(&e.content),
1676
        }
1677
    }
1678
1679
    /// Text content as stored: `\r\n` normalized for `eol=` files.
1680
    fn stored_text(
1681
        &self,
1682
        path: &str,
1683
        content: &std::sync::Arc<Vec<u8>>,
1684
    ) -> std::sync::Arc<Vec<u8>> {
1685
        if self.attrs.lookup(path).eol.is_some() {
1686
            std::sync::Arc::new(crate::attributes::normalize_eol(content))
1687
        } else {
1688
            content.clone()
1689
        }
1690
    }
1691
1692
    /// The identifier of a node added at `path`: reused when asked, derived
1693
    /// from the seed when there is one, random otherwise.
1694
    fn new_node(&self, path: &str, kind: NodeKind) -> NodeId {
1695
        if let Some(n) = self.reused_node(path, kind) {
1696
            return n;
1697
        }
1698
        match &self.node_seed {
1699
            Some(seed) => seeded_node(seed, path, kind),
1700
            None => NodeId::random(),
1701
        }
1702
    }
1703
1704
    fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> {
1705
        self.reuse
1706
            .get(path)
1707
            .filter(|(_, k)| *k == kind)
1708
            .map(|(n, _)| *n)
1709
    }
1710
1711
    /// A dead directory last placed at (`parent`, `name`) in the base.
1712
    fn revivable_dir(
1713
        &mut self,
1714
        parent: NodeId,
1715
        name: &str,
1716
    ) -> Option<(NodeId, crate::tree::OpKey)> {
1717
        let base = self.base;
1718
        self.dead_dirs
1719
            .get_or_insert_with(|| base.tree().dead_dirs())
1720
            .get(&(parent, name.to_string()))
1721
            .copied()
1722
    }
1723
1724
    fn ensure_dir(&mut self, path: &str) -> NodeId {
1725
        if let Some(n) = self.dir_nodes.get(path) {
1726
            return *n;
1727
        }
1728
        let mut parent = NodeId::ROOT;
1729
        let mut prefix = String::new();
1730
        for comp in path.split('/') {
1731
            if !prefix.is_empty() {
1732
                prefix.push('/');
1733
            }
1734
            prefix.push_str(comp);
1735
            let known = self.dir_nodes.get(&prefix).copied();
1736
            let revive = match known {
1737
                Some(_) => None,
1738
                None if self.node_seed.is_some() => self.revivable_dir(parent, comp),
1739
                None => None,
1740
            };
1741
            parent = match (known, revive) {
1742
                (Some(n), _) => n,
1743
                (None, Some((node, old))) => {
1744
                    // Git has no identity for a directory: one that comes
1745
                    // back where it was is the same directory, brought back
1746
                    // with a move, so lines of history that placed files in
1747
                    // it before and after agree (oxygen#30).
1748
                    self.tree_ops.push(TreeOp::Move {
1749
                        node,
1750
                        old: OpRef {
1751
                            change: old.0,
1752
                            index: old.1,
1753
                        },
1754
                        parent,
1755
                        name: comp.to_string(),
1756
                    });
1757
                    self.dir_nodes.insert(prefix.clone(), node);
1758
                    node
1759
                }
1760
                (None, None) => {
1761
                    let node = self.new_node(&prefix, NodeKind::Dir);
1762
                    self.tree_ops.push(TreeOp::AddNode {
1763
                        node,
1764
                        kind: NodeKind::Dir,
1765
                        mode: Mode::default(),
1766
                        parent,
1767
                        name: comp.to_string(),
1768
                    });
1769
                    self.dir_nodes.insert(prefix.clone(), node);
1770
                    node
1771
                }
1772
            };
1773
        }
1774
        parent
1775
    }
1776
1777
    fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> {
1778
        let (dir, name) = split_path(path);
1779
        let parent = self.ensure_dir(dir);
1780
        let node = self.new_node(path, e.kind);
1781
        let opaque = self.opaque_for(path, e);
1782
        let nocompress = self.attrs.lookup(path).nocompress;
1783
        self.tree_ops.push(TreeOp::AddNode {
1784
            node,
1785
            kind: e.kind,
1786
            mode: Mode {
1787
                exec: e.exec,
1788
                opaque,
1789
            },
1790
            parent,
1791
            name: name.to_string(),
1792
        });
1793
        if opaque {
1794
            let blob = self.blob_of(repo, path, e, nocompress)?;
1795
            self.edits.push(Edit::Replace {
1796
                node,
1797
                old: None,
1798
                new: Some(blob),
1799
            });
1800
        } else if !e.content.is_empty() {
1801
            let content = self.stored_text(path, &e.content);
1802
            self.edits.push(Edit::Insert {
1803
                node,
1804
                after: crate::objects::AtomRef::Start,
1805
                before: crate::objects::AtomRef::End,
1806
                lines: crate::lines::Lines::split_shared(content),
1807
            });
1808
        }
1809
        self.statuses.push(PathStatus {
1810
            path: path.to_string(),
1811
            status: ChangeStatus::Added,
1812
        });
1813
        Ok(())
1814
    }
1815
1816
    /// Move the node at `old` to `new` and apply `e`'s content to it.
1817
    fn move_path(
1818
        &mut self,
1819
        repo: &Repo,
1820
        old: &str,
1821
        new: &str,
1822
        e: &FsEntry,
1823
        expected: &crate::state::FileState,
1824
    ) -> Result<()> {
1825
        let entry = &self.base_snapshot.tree.entries[old];
1826
        let (dir, name) = split_path(new);
1827
        let parent = self.ensure_dir(dir);
1828
        self.tree_ops.push(TreeOp::Move {
1829
            node: expected.node,
1830
            old: op_ref(entry.placement),
1831
            parent,
1832
            name: name.to_string(),
1833
        });
1834
        self.update_path(repo, old, e, expected)?;
1835
        // update_path reports under the old path; the rename is what the user sees.
1836
        self.statuses.retain(|s| s.path != old);
1837
        self.statuses.push(PathStatus {
1838
            path: new.to_string(),
1839
            status: ChangeStatus::Renamed,
1840
        });
1841
        Ok(())
1842
    }
1843
1844
    /// For a Git import: a node the commit keeps is in one place, the one
1845
    /// its tree names, so any other live placement it has in the base goes;
1846
    /// and a node the tree cannot show at all (an orphan) goes too. Such a
1847
    /// placement is out of sight, under a directory another line of history
1848
    /// removed, where no diff of the tree would ever reach it (oxygen#30).
1849
    fn retire_stray_placements(&mut self) {
1850
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
1851
        let mut placed: BTreeSet<NodeId> = BTreeSet::new();
1852
        for op in &self.tree_ops {
1853
            match op {
1854
                TreeOp::Move { node, old, .. } => {
1855
                    retired.insert((old.change, old.index));
1856
                    placed.insert(*node);
1857
                }
1858
                TreeOp::RemoveNode { placement, .. } => {
1859
                    retired.insert((placement.change, placement.index));
1860
                }
1861
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
1862
            }
1863
        }
1864
        // Nodes kept where they were: what the base's tree view shows and
1865
        // this change neither moves nor removes.
1866
        let mut kept: HashMap<NodeId, (ChangeHash, u32)> = HashMap::new();
1867
        for e in self.base_snapshot.tree.entries.values() {
1868
            if !retired.contains(&e.placement) {
1869
                kept.insert(e.node, e.placement);
1870
            }
1871
        }
1872
        // Only a node with several live placements can have a stray one,
1873
        // and the tree view names each as a conflict.
1874
        let multi: BTreeSet<NodeId> = self
1875
            .base_snapshot
1876
            .tree
1877
            .conflicts
1878
            .iter()
1879
            .filter_map(|c| match c {
1880
                crate::tree::TreeConflict::MultiPlacement { node, .. } => Some(*node),
1881
                _ => None,
1882
            })
1883
            .collect();
1884
        let mut extra = Vec::new();
1885
        // A node whose only placements are under a directory another line
1886
        // removed (an orphan) is nowhere the tree can show, and Git's tree
1887
        // has no such path: it goes.
1888
        for c in &self.base_snapshot.tree.conflicts {
1889
            let crate::tree::TreeConflict::Orphan { node, .. } = c else {
1890
                continue;
1891
            };
1892
            if kept.contains_key(node) || placed.contains(node) {
1893
                continue;
1894
            }
1895
            for (k, _) in self.base.tree().live_placements(node) {
1896
                if retired.insert(k) {
1897
                    extra.push(TreeOp::RemoveNode {
1898
                        node: *node,
1899
                        placement: op_ref(k),
1900
                    });
1901
                }
1902
            }
1903
        }
1904
        for node in placed
1905
            .iter()
1906
            .chain(multi.iter().filter(|n| kept.contains_key(*n)))
1907
        {
1908
            for (k, _) in self.base.tree().live_placements(node) {
1909
                if retired.contains(&k) || kept.get(node) == Some(&k) {
1910
                    continue;
1911
                }
1912
                retired.insert(k);
1913
                extra.push(TreeOp::RemoveNode {
1914
                    node: *node,
1915
                    placement: op_ref(k),
1916
                });
1917
            }
1918
        }
1919
        if !extra.is_empty() {
1920
            self.resolves = true;
1921
            self.tree_ops.extend(extra);
1922
        }
1923
    }
1924
1925
    fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) {
1926
        let entry = &self.base_snapshot.tree.entries[path];
1927
        self.tree_ops.push(TreeOp::RemoveNode {
1928
            node: expected.node,
1929
            placement: op_ref(entry.placement),
1930
        });
1931
        if path.contains(".oxy-conflict-") || expected.is_conflicted() {
1932
            self.resolves = true;
1933
        }
1934
        self.statuses.push(PathStatus {
1935
            path: path.to_string(),
1936
            status: ChangeStatus::Removed,
1937
        });
1938
    }
1939
1940
    fn update_path(
1941
        &mut self,
1942
        repo: &Repo,
1943
        path: &str,
1944
        e: &FsEntry,
1945
        expected: &crate::state::FileState,
1946
    ) -> Result<()> {
1947
        if expected.kind != e.kind {
1948
            self.remove_path(path, expected);
1949
            self.statuses.pop();
1950
            self.add_path(repo, path, e)?;
1951
            self.statuses.pop();
1952
            self.statuses.push(PathStatus {
1953
                path: path.to_string(),
1954
                status: ChangeStatus::Modified,
1955
            });
1956
            return Ok(());
1957
        }
1958
        let entry = &self.base_snapshot.tree.entries[path];
1959
        let node = expected.node;
1960
        let mode_atom = entry.mode_atom.map(op_ref);
1961
        if e.unchanged {
1962
            // Known equal to the base's unconflicted rendering (spec
1963
            // §12.3); only the executable bit can differ.
1964
            if e.exec != expected.mode.exec {
1965
                if let Some(old) = mode_atom {
1966
                    self.tree_ops.push(TreeOp::SetMode {
1967
                        node,
1968
                        old,
1969
                        mode: Mode {
1970
                            exec: e.exec,
1971
                            opaque: false,
1972
                        },
1973
                    });
1974
                    self.statuses.push(PathStatus {
1975
                        path: path.to_string(),
1976
                        status: ChangeStatus::ModeChanged,
1977
                    });
1978
                }
1979
            }
1980
            return Ok(());
1981
        }
1982
        let mut status: Option<ChangeStatus> = None;
1983
        let wants_opaque = self.opaque_for(path, e);
1984
        let nocompress = self.attrs.lookup(path).nocompress;
1985
        let disk_text = self.stored_text(path, &e.content);
1986
        match &expected.content {
1987
            FileContent::Dir => {}
1988
            FileContent::Text {
1989
                bytes,
1990
                materialized,
1991
            } => {
1992
                if materialized.is_conflicted() {
1993
                    if bytes == &*disk_text {
1994
                        return Ok(());
1995
                    }
1996
                    if has_conflict_markers(&e.content) {
1997
                        self.statuses.push(PathStatus {
1998
                            path: path.to_string(),
1999
                            status: ChangeStatus::ConflictEdited,
2000
                        });
2001
                        return Ok(());
2002
                    }
2003
                    // Resolution (spec §9.6): diff against the unconflicted lines
2004
                    // with each region as a gap, then retire every region atom.
2005
                    // Whatever the author kept from a region is re-inserted as
2006
                    // new atoms, which leaves the graph totally ordered.
2007
                    let regions = self.base.conflict_regions(&node);
2008
                    let mut edits = text_edits(node, &regions.plain, &disk_text);
2009
                    if !regions.region_atoms.is_empty() {
2010
                        edits.insert(
2011
                            0,
2012
                            Edit::Delete {
2013
                                node,
2014
                                atoms: ranges(&regions.region_atoms),
2015
                            },
2016
                        );
2017
                    }
2018
                    self.edits.extend(edits);
2019
                    self.contributors.extend(regions.contributors);
2020
                    self.resolves = true;
2021
                    if e.exec != expected.mode.exec {
2022
                        if let Some(old) = mode_atom {
2023
                            self.tree_ops.push(TreeOp::SetMode {
2024
                                node,
2025
                                old,
2026
                                mode: Mode {
2027
                                    exec: e.exec,
2028
                                    opaque: false,
2029
                                },
2030
                            });
2031
                        }
2032
                    }
2033
                    self.statuses.push(PathStatus {
2034
                        path: path.to_string(),
2035
                        status: ChangeStatus::Resolved,
2036
                    });
2037
                    return Ok(());
2038
                }
2039
                if wants_opaque {
2040
                    // Text → opaque (spec §7.5), by content or by attribute.
2041
                    if let Some(old) = mode_atom {
2042
                        self.tree_ops.push(TreeOp::SetMode {
2043
                            node,
2044
                            old,
2045
                            mode: Mode {
2046
                                exec: e.exec,
2047
                                opaque: true,
2048
                            },
2049
                        });
2050
                    }
2051
                    let live: Vec<_> = repo_live_keys(self.base, &node);
2052
                    if !live.is_empty() {
2053
                        self.edits.push(Edit::Delete {
2054
                            node,
2055
                            atoms: ranges(&live),
2056
                        });
2057
                    }
2058
                    let blob = self.blob_of(repo, path, e, nocompress)?;
2059
                    self.edits.push(Edit::Replace {
2060
                        node,
2061
                        old: None,
2062
                        new: Some(blob),
2063
                    });
2064
                    status = Some(ChangeStatus::Modified);
2065
                } else if bytes != &*disk_text {
2066
                    let mut laps = crate::timing::Laps::start();
2067
                    // The base's rendering holds its live atoms in order,
2068
                    // one per line (it is not conflicted here).
2069
                    let keys = materialized.live_atoms();
2070
                    let lines = crate::diff::split_lines(bytes);
2071
                    let edits = if keys.len() == lines.len() {
2072
                        crate::diff::text_edits_of(node, &keys, &lines, &disk_text)
2073
                    } else {
2074
                        text_edits(node, &self.base.live_lines(&node), &disk_text)
2075
                    };
2076
                    laps.lap("build: text diff");
2077
                    if !edits.is_empty() {
2078
                        self.edits.extend(edits);
2079
                        status = Some(ChangeStatus::Modified);
2080
                    }
2081
                }
2082
                let switched_to_opaque = wants_opaque;
2083
                if !switched_to_opaque && e.exec != expected.mode.exec {
2084
                    if let Some(old) = mode_atom {
2085
                        self.tree_ops.push(TreeOp::SetMode {
2086
                            node,
2087
                            old,
2088
                            mode: Mode {
2089
                                exec: e.exec,
2090
                                opaque: false,
2091
                            },
2092
                        });
2093
                        status.get_or_insert(ChangeStatus::ModeChanged);
2094
                    }
2095
                }
2096
            }
2097
            FileContent::Opaque { blobs } => {
2098
                let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content));
2099
                if e.kind == NodeKind::File && wants_opaque {
2100
                    // Unchanged or replaced, the file stays opaque: remember its blob.
2101
                    self.blobs.insert(path.to_string(), new_ref);
2102
                }
2103
                if blobs.len() > 1 && self.resolve.contains(path) && e.kind == expected.kind {
2104
                    // Settle the replace conflict (spec §9.3): replace every
2105
                    // live candidate with the chosen blob, depending on each
2106
                    // change that made one live, so all are superseded.
2107
                    let chosen = self.blob_of(repo, path, e, nocompress)?;
2108
                    for b in blobs {
2109
                        self.edits.push(Edit::Replace {
2110
                            node,
2111
                            old: Some(*b),
2112
                            new: Some(chosen),
2113
                        });
2114
                    }
2115
                    self.contributors
2116
                        .extend(self.base.blob_contributors(&node, blobs));
2117
                    self.resolves = true;
2118
                    self.statuses.push(PathStatus {
2119
                        path: path.to_string(),
2120
                        status: ChangeStatus::Resolved,
2121
                    });
2122
                    return Ok(());
2123
                }
2124
                if blobs.len() > 1 {
2125
                    if !blobs.contains(&new_ref) {
2126
                        self.statuses.push(PathStatus {
2127
                            path: path.to_string(),
2128
                            status: ChangeStatus::ConflictEdited,
2129
                        });
2130
                    }
2131
                    return Ok(());
2132
                }
2133
                let current = blobs.first().copied();
2134
                if current != Some(new_ref) {
2135
                    if e.kind == NodeKind::File && !wants_opaque {
2136
                        // Opaque → text (spec §7.5).
2137
                        if let Some(old) = mode_atom {
2138
                            self.tree_ops.push(TreeOp::SetMode {
2139
                                node,
2140
                                old,
2141
                                mode: Mode {
2142
                                    exec: e.exec,
2143
                                    opaque: false,
2144
                                },
2145
                            });
2146
                        }
2147
                        self.edits.push(Edit::Replace {
2148
                            node,
2149
                            old: current,
2150
                            new: None,
2151
                        });
2152
                        if !e.content.is_empty() {
2153
                            self.edits.push(Edit::Insert {
2154
                                node,
2155
                                after: crate::objects::AtomRef::Start,
2156
                                before: crate::objects::AtomRef::End,
2157
                                lines: crate::lines::Lines::split(&e.content),
2158
                            });
2159
                        }
2160
                    } else {
2161
                        let blob = self.blob_of(repo, path, e, nocompress)?;
2162
                        self.edits.push(Edit::Replace {
2163
                            node,
2164
                            old: current,
2165
                            new: Some(blob),
2166
                        });
2167
                    }
2168
                    status = Some(ChangeStatus::Modified);
2169
                } else if e.kind == NodeKind::File && e.exec != expected.mode.exec {
2170
                    if let Some(old) = mode_atom {
2171
                        self.tree_ops.push(TreeOp::SetMode {
2172
                            node,
2173
                            old,
2174
                            mode: Mode {
2175
                                exec: e.exec,
2176
                                opaque: true,
2177
                            },
2178
                        });
2179
                        status = Some(ChangeStatus::ModeChanged);
2180
                    }
2181
                }
2182
            }
2183
        }
2184
        if let Some(s) = status {
2185
            self.statuses.push(PathStatus {
2186
                path: path.to_string(),
2187
                status: s,
2188
            });
2189
        }
2190
        Ok(())
2191
    }
2192
}
2193
2194
fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> {
2195
    base.live_lines(node).into_iter().map(|(k, _)| k).collect()
2196
}
2197
2198
/// The changes a change's scope references (spec §8.1): introducers of every
2199
/// atom, placement, and mode it touches, and of every node it operates on.
2200
pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> {
2201
    let mut out = BTreeSet::new();
2202
    let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| {
2203
        if !r.change.is_zero() {
2204
            out.insert(r.change);
2205
        }
2206
    };
2207
    let mut nodes: Vec<NodeId> = Vec::new();
2208
    for op in &change.tree_ops {
2209
        match op {
2210
            TreeOp::AddNode { parent, .. } => nodes.push(*parent),
2211
            TreeOp::RemoveNode { node, placement } => {
2212
                nodes.push(*node);
2213
                opref(placement, &mut out);
2214
            }
2215
            TreeOp::Move {
2216
                node, old, parent, ..
2217
            } => {
2218
                nodes.push(*node);
2219
                nodes.push(*parent);
2220
                opref(old, &mut out);
2221
            }
2222
            TreeOp::SetMode { node, old, .. } => {
2223
                nodes.push(*node);
2224
                opref(old, &mut out);
2225
            }
2226
        }
2227
    }
2228
    for e in &change.edits {
2229
        nodes.push(e.node());
2230
        match e {
2231
            Edit::Insert { after, before, .. } => {
2232
                for r in [after, before] {
2233
                    if let crate::objects::AtomRef::Atom { change, .. } = r {
2234
                        if !change.is_zero() {
2235
                            out.insert(*change);
2236
                        }
2237
                    }
2238
                }
2239
            }
2240
            Edit::Delete { atoms, .. } => {
2241
                // Deleting an atom also depends on whoever inserted the live
2242
                // atoms right after it: the author saw those insertions, and
2243
                // without the dependency spec §9.1 would call the deletion
2244
                // concurrent with them (a delete/insert conflict).
2245
                let graph = base.file(&e.node());
2246
                for r in atoms {
2247
                    if !r.change.is_zero() {
2248
                        out.insert(r.change);
2249
                    }
2250
                    if let Some(g) = graph {
2251
                        for i in r.from..r.to {
2252
                            out.extend(
2253
                                g.live_successor_changes(&(r.change, i))
2254
                                    .into_iter()
2255
                                    .filter(|c| !c.is_zero()),
2256
                            );
2257
                        }
2258
                    }
2259
                }
2260
            }
2261
            // Replacing a blob depends on the change that made it current,
2262
            // as deleting a line depends on the line's introducer.
2263
            Edit::Replace {
2264
                node,
2265
                old: Some(old),
2266
                ..
2267
            } => {
2268
                out.extend(
2269
                    base.live_replaces(node)
2270
                        .into_iter()
2271
                        .filter(|(c, b)| b == old && !c.is_zero())
2272
                        .map(|(c, _)| c),
2273
                );
2274
            }
2275
            Edit::Replace { .. } => {}
2276
        }
2277
    }
2278
    for n in nodes {
2279
        if let Some(h) = base.tree().introducer(&n) {
2280
            out.insert(h);
2281
        }
2282
    }
2283
    out
2284
}
2285
2286
/// Paths of the nodes a change adds on top of `base_snapshot`.
2287
pub fn added_node_paths(
2288
    change: &Change,
2289
    base_snapshot: &Snapshot,
2290
) -> HashMap<String, (NodeId, NodeKind)> {
2291
    let by_node = base_snapshot.tree.by_node();
2292
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
2293
    for op in &change.tree_ops {
2294
        if let TreeOp::AddNode {
2295
            node,
2296
            parent,
2297
            name,
2298
            kind,
2299
            ..
2300
        } = op
2301
        {
2302
            new_names.insert(*node, (*parent, name.clone(), *kind));
2303
        }
2304
    }
2305
    let mut out = HashMap::new();
2306
    for (node, (_, _, kind)) in &new_names {
2307
        if let Some(path) = path_of_node(node, &by_node, &new_names) {
2308
            out.insert(path, (*node, *kind));
2309
        }
2310
    }
2311
    out
2312
}
2313
2314
fn path_of_node(
2315
    n: &NodeId,
2316
    by_node: &HashMap<NodeId, &crate::tree::TreeEntry>,
2317
    new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>,
2318
) -> Option<String> {
2319
    if let Some(e) = by_node.get(n) {
2320
        return Some(e.path.clone());
2321
    }
2322
    let mut parts = Vec::new();
2323
    let mut cur = *n;
2324
    loop {
2325
        if cur.is_root() {
2326
            break;
2327
        }
2328
        if let Some(e) = by_node.get(&cur) {
2329
            if !e.path.is_empty() {
2330
                parts.push(e.path.clone());
2331
            }
2332
            break;
2333
        }
2334
        let (parent, name, _) = new_names.get(&cur)?;
2335
        parts.push(name.clone());
2336
        cur = *parent;
2337
    }
2338
    parts.reverse();
2339
    Some(parts.join("/"))
2340
}
2341
2342
/// Render a change's operations with line content, using `base` for deleted lines.
2343
pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String {
2344
    let by_node = base_snapshot.tree.by_node();
2345
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
2346
    for op in &change.tree_ops {
2347
        if let TreeOp::AddNode {
2348
            node,
2349
            parent,
2350
            name,
2351
            kind,
2352
            ..
2353
        } = op
2354
        {
2355
            new_names.insert(*node, (*parent, name.clone(), *kind));
2356
        }
2357
    }
2358
    let path_of = |n: &NodeId| -> String {
2359
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
2360
    };
2361
2362
    let mut out = String::new();
2363
    for op in &change.tree_ops {
2364
        match op {
2365
            TreeOp::AddNode {
2366
                node, kind, mode, ..
2367
            } => {
2368
                out.push_str(&format!(
2369
                    "A {} ({:?}{}{})\n",
2370
                    path_of(node),
2371
                    kind,
2372
                    if mode.exec { ", exec" } else { "" },
2373
                    if mode.opaque { ", opaque" } else { "" }
2374
                ));
2375
            }
2376
            TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))),
2377
            TreeOp::Move { node, name, .. } => {
2378
                out.push_str(&format!("R {} -> {}\n", path_of(node), name))
2379
            }
2380
            TreeOp::SetMode { node, mode, .. } => {
2381
                out.push_str(&format!(
2382
                    "M {} mode: exec={} opaque={}\n",
2383
                    path_of(node),
2384
                    mode.exec,
2385
                    mode.opaque
2386
                ));
2387
            }
2388
        }
2389
    }
2390
    let mut last_node: Option<NodeId> = None;
2391
    for e in &change.edits {
2392
        let n = e.node();
2393
        if last_node != Some(n) {
2394
            out.push_str(&format!("--- {}\n", path_of(&n)));
2395
            last_node = Some(n);
2396
        }
2397
        match e {
2398
            Edit::Insert { lines, .. } => {
2399
                for l in lines.iter() {
2400
                    out.push('+');
2401
                    out.push_str(&String::from_utf8_lossy(l));
2402
                    if !l.ends_with(b"\n") {
2403
                        out.push('\n');
2404
                    }
2405
                }
2406
            }
2407
            Edit::Delete { atoms, .. } => {
2408
                for r in atoms {
2409
                    for i in r.from..r.to {
2410
                        let content = base
2411
                            .file(&n)
2412
                            .and_then(|g| g.content(&(r.change, i)))
2413
                            .unwrap_or(b"<unknown>\n");
2414
                        out.push('-');
2415
                        out.push_str(&String::from_utf8_lossy(content));
2416
                        if !content.ends_with(b"\n") {
2417
                            out.push('\n');
2418
                        }
2419
                    }
2420
                }
2421
            }
2422
            Edit::Replace { old, new, .. } => {
2423
                out.push_str(&format!(
2424
                    "  opaque content {} -> {}\n",
2425
                    old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()),
2426
                    new.map(|o| o.short(12)).unwrap_or_else(|| "none".into())
2427
                ));
2428
            }
2429
        }
2430
    }
2431
    out
2432
}
2433
2434
/// Pair removed files with added files of the same kind whose contents are
2435
/// at least `RENAME_THRESHOLD` percent alike, best matches first. Beyond
2436
/// `RENAME_LIMIT` candidate pairs only identical contents are paired.
2437
const RENAME_THRESHOLD: u32 = 50;
2438
const RENAME_LIMIT: usize = 10_000;
2439
2440
/// What a Git import adds to building a change: the seed its new nodes'
2441
/// identifiers derive from, and, when it builds from part of the tree,
2442
/// whether the new tree has anything under a directory.
2443
struct Import<'a> {
2444
    seed: [u8; 32],
2445
    dir_needed: Option<&'a dyn Fn(&str) -> bool>,
2446
}
2447
2448
fn detect_renames(
2449
    base_snapshot: &Snapshot,
2450
    fs_entries: &BTreeMap<String, FsEntry>,
2451
    in_scope: impl Fn(&str) -> bool,
2452
) -> Vec<(String, String)> {
2453
    let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot
2454
        .files
2455
        .iter()
2456
        .filter(|(p, f)| {
2457
            f.kind != NodeKind::Dir
2458
                && !fs_entries.contains_key(*p)
2459
                && in_scope(p)
2460
                && !f.is_conflicted()
2461
        })
2462
        .collect();
2463
    let added: Vec<(&String, &FsEntry)> = fs_entries
2464
        .iter()
2465
        .filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p))
2466
        .collect();
2467
    if removed.is_empty() || added.is_empty() {
2468
        return vec![];
2469
    }
2470
    let exact_only = removed.len() * added.len() > RENAME_LIMIT;
2471
    let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> {
2472
        match &f.content {
2473
            FileContent::Text { bytes, .. } => Some(bytes.clone()),
2474
            _ => None,
2475
        }
2476
    };
2477
    let mut scored: Vec<(u32, usize, usize)> = Vec::new();
2478
    for (ri, (_, f)) in removed.iter().enumerate() {
2479
        let old = old_content(f);
2480
        let old_blob = match &f.content {
2481
            FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]),
2482
            _ => None,
2483
        };
2484
        for (ai, (_, e)) in added.iter().enumerate() {
2485
            if e.kind != f.kind {
2486
                continue;
2487
            }
2488
            let score = match (&old, old_blob) {
2489
                (Some(o), _) => {
2490
                    if exact_only {
2491
                        if *o == *e.content {
2492
                            100
2493
                        } else {
2494
                            0
2495
                        }
2496
                    } else {
2497
                        similarity(o, &e.content)
2498
                    }
2499
                }
2500
                (None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100,
2501
                _ => 0,
2502
            };
2503
            if score >= RENAME_THRESHOLD {
2504
                scored.push((score, ri, ai));
2505
            }
2506
        }
2507
    }
2508
    scored.sort_by(|a, b| {
2509
        b.0.cmp(&a.0)
2510
            .then(removed[a.1].0.cmp(removed[b.1].0))
2511
            .then(added[a.2].0.cmp(added[b.2].0))
2512
    });
2513
    let mut used_r = vec![false; removed.len()];
2514
    let mut used_a = vec![false; added.len()];
2515
    let mut out = Vec::new();
2516
    for (_, ri, ai) in scored {
2517
        if used_r[ri] || used_a[ai] {
2518
            continue;
2519
        }
2520
        used_r[ri] = true;
2521
        used_a[ai] = true;
2522
        out.push((removed[ri].0.clone(), added[ai].0.clone()));
2523
    }
2524
    out
2525
}
2526
2527
/// Percentage of the larger file's bytes found in lines the two share
2528
/// (multiset intersection of lines), like Git's similarity index.
2529
pub fn similarity(a: &[u8], b: &[u8]) -> u32 {
2530
    if a == b {
2531
        return 100;
2532
    }
2533
    if a.is_empty() || b.is_empty() {
2534
        return 0;
2535
    }
2536
    let mut counts: HashMap<&[u8], usize> = HashMap::new();
2537
    for l in split_lines(a) {
2538
        *counts.entry(l).or_default() += 1;
2539
    }
2540
    let mut shared = 0usize;
2541
    for l in split_lines(b) {
2542
        if let Some(n) = counts.get_mut(l) {
2543
            if *n > 0 {
2544
                *n -= 1;
2545
                shared += l.len();
2546
            }
2547
        }
2548
    }
2549
    (shared * 100 / a.len().max(b.len())) as u32
2550
}