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