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