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