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