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