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