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