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