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