OXYGENHUB
◆ v0.9.0 oxygen / crates / oxy-core / src / repo / snapshot.rs
138.7 KiB · Rust · last changed Identical conflict sides settle by themselves, keeping one, in any file (closes #146) 9 days ago source blame history raw
1
//! Working-copy snapshots, commits, and the change builder (spec §12.3).
2
3
use super::*;
4
use crate::objects::AtomRef;
5
6
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7
pub enum ChangeStatus {
8
    Added,
9
    Modified,
10
    Removed,
11
    /// Moved from another path (detected by similarity, tracked as a placement change).
12
    Renamed,
13
    ModeChanged,
14
    /// The file is conflicted in the base state and still contains conflict markers.
15
    ConflictEdited,
16
    /// The file was conflicted in the base state and the working copy resolves it.
17
    Resolved,
18
}
19
20
/// Lines that mark a conflict region in a materialized file (spec §9.5).
21
pub fn has_conflict_markers(content: &[u8]) -> bool {
22
    split_lines(content).iter().any(|l| {
23
        l.starts_with(b"<<<<<<< oxy conflict")
24
            || l.starts_with(b"======= oxy conflict")
25
            || l.starts_with(b">>>>>>> oxy conflict")
26
    })
27
}
28
29
/// Whether `content` still holds a conflict marker oxy wrote for this file
30
/// (oxygen#126): a marker-like line of the materialized conflicted file
31
/// that is not one of its unconflicted lines (`plain`). Text that merely
32
/// looks like a marker, as oxy's own documentation or a test fixture has,
33
/// is in the unconflicted lines, so it is not taken for one.
34
pub fn has_own_conflict_markers(content: &[u8], materialized: &[u8], plain: &[&[u8]]) -> bool {
35
    let line = |l: &[u8]| -> Vec<u8> {
36
        let l = l.strip_suffix(b"\n").unwrap_or(l);
37
        l.strip_suffix(b"\r").unwrap_or(l).to_vec()
38
    };
39
    let marker = |l: &[u8]| {
40
        l.starts_with(b"<<<<<<< oxy conflict")
41
            || l.starts_with(b"======= oxy conflict")
42
            || l.starts_with(b">>>>>>> oxy conflict")
43
    };
44
    let unconflicted: HashSet<Vec<u8>> = plain.iter().map(|l| line(l)).collect();
45
    let own: HashSet<Vec<u8>> = split_lines(materialized)
46
        .into_iter()
47
        .map(line)
48
        .filter(|l| marker(l) && !unconflicted.contains(l))
49
        .collect();
50
    !own.is_empty()
51
        && split_lines(content)
52
            .into_iter()
53
            .any(|l| own.contains(&line(l)))
54
}
55
56
#[derive(Clone, Debug, PartialEq, Eq)]
57
pub struct PathStatus {
58
    pub path: String,
59
    pub status: ChangeStatus,
60
}
61
62
/// Operations of a change built from the working directory against a base.
63
pub(crate) struct BuiltChange {
64
    pub tree_ops: Vec<TreeOp>,
65
    pub edits: Vec<Edit>,
66
    pub contributors: Vec<ChangeHash>,
67
    pub resolves: bool,
68
    pub statuses: Vec<PathStatus>,
69
    /// The stored blob of every opaque path, for the manifest's stat cache.
70
    pub blobs: HashMap<String, ObjectRef>,
71
}
72
73
/// Result of snapshotting the working copy.
74
pub struct SnapshotResult {
75
    /// The working-copy change's hash; for a commit's own snapshot, which
76
    /// does not encode it, the hash it had before.
77
    pub wc_hash: ChangeHash,
78
    pub wc: Change,
79
    /// The base state (closure of the working-copy change's dependencies).
80
    pub base: State,
81
    pub base_snapshot: Snapshot,
82
    pub statuses: Vec<PathStatus>,
83
    /// Whether the snapshot rewrote the working-copy change.
84
    pub changed: bool,
85
    /// Whether the working copy resolves conflicts of its base (spec §9.6).
86
    pub resolves: bool,
87
    /// Changes that contributed to the conflicts being resolved.
88
    pub contributors: Vec<ChangeHash>,
89
    /// The `.oxyattributes` in force on disk, from the files the walk read.
90
    pub(crate) attributes: crate::attributes::Attributes,
91
    /// Folders holding a repository of their own (`.git` or `.oxy`) that
92
    /// are not subrepos, so nothing in them is tracked (oxygen#43).
93
    pub untracked_nested: Vec<String>,
94
    /// A commit's snapshot leaves the manifest to the commit, which writes
95
    /// it once with the new base's graph keys.
96
    pub(crate) manifest: Option<PendingManifest>,
97
}
98
99
/// The manifest a commit's snapshot would have written, as of the moment
100
/// it would have written it (that moment is the racy-window stamp).
101
pub(crate) struct PendingManifest {
102
    entries: BTreeMap<String, FsEntry>,
103
    blobs: HashMap<String, ObjectRef>,
104
    dirs: Option<super::WalkedDirs>,
105
    written: u128,
106
}
107
108
/// Whether `base` has two or more live versions of the opaque file or
109
/// subrepo pin at `path` (a replace conflict, spec §9.3).
110
fn has_replace_conflict(base: &Snapshot, path: &str) -> bool {
111
    matches!(base.files.get(path).map(|f| &f.content), Some(FileContent::Opaque { blobs }) if blobs.len() > 1)
112
}
113
114
/// What `oxy resolve --both` did to one file (oxygen#121).
115
#[derive(Debug, Default, Clone, PartialEq, Eq)]
116
pub struct KeptBoth {
117
    pub path: String,
118
    /// The conflicts settled by keeping every side, by number.
119
    pub settled: Vec<usize>,
120
    /// Of those, the ones whose sides were identical, settled by keeping
121
    /// one (oxygen#146).
122
    pub identical: Vec<usize>,
123
    /// The conflicts that keep their markers, by number, and why.
124
    pub left: Vec<(usize, &'static str)>,
125
}
126
127
/// `text` (a working-copy file) with the marker block of each conflict that
128
/// `sides` (from [`State::both_sides`]) says keeping both settles replaced
129
/// by its sides' lines; the conflicts settled, and those left and why.
130
pub(crate) fn keep_both_text(
131
    text: &[u8],
132
    sides: &[Option<Vec<Vec<u8>>>],
133
    reverse: bool,
134
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) {
135
    let plans: Vec<Option<crate::state::Settle>> = sides
136
        .iter()
137
        .map(|s| {
138
            s.as_ref().map(|w| crate::state::Settle {
139
                sides: w.clone(),
140
                kept: w.clone(),
141
                identical: false,
142
            })
143
        })
144
        .collect();
145
    settle_text(text, &plans, reverse)
146
}
147
148
/// `text` (a working-copy file) with the marker block of each conflict
149
/// that `plans` (from [`State::settle_plans`]) settle replaced by its
150
/// kept text, the sides in order (`reverse`d on request); the conflicts
151
/// settled, and those left and why. A block whose text between the
152
/// markers is not its sides' keeps its markers.
153
pub(crate) fn settle_text(
154
    text: &[u8],
155
    plans: &[Option<crate::state::Settle>],
156
    reverse: bool,
157
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) {
158
    // A side as the markers hold it: the renderer ends each with a newline.
159
    let with_newline = |s: &[u8]| {
160
        let mut s = s.to_vec();
161
        if !s.is_empty() && !s.ends_with(b"\n") {
162
            s.push(b'\n');
163
        }
164
        s
165
    };
166
    let lines: Vec<&[u8]> = text.split_inclusive(|&b| b == b'\n').collect();
167
    let (mut out, mut settled, mut left) = (Vec::new(), Vec::new(), Vec::new());
168
    let mut i = 0;
169
    while i < lines.len() {
170
        let Some((n, total)) = conflict_header(lines[i]) else {
171
            out.extend_from_slice(lines[i]);
172
            i += 1;
173
            continue;
174
        };
175
        let end = format!(">>>>>>> oxy conflict {n} of {total} end");
176
        let next_side = format!("======= oxy conflict {n} of {total}, side ");
177
        let Some(stop) =
178
            (i + 1..lines.len()).find(|&j| lines[j].trim_ascii_end() == end.as_bytes())
179
        else {
180
            left.push((n, "its end marker is missing; edit it by hand"));
181
            out.extend_from_slice(lines[i]);
182
            i += 1;
183
            continue;
184
        };
185
        let mut found: Vec<Vec<u8>> = vec![Vec::new()];
186
        for line in &lines[i + 1..stop] {
187
            if line.starts_with(next_side.as_bytes()) {
188
                found.push(Vec::new());
189
            } else {
190
                found.last_mut().unwrap().extend_from_slice(line);
191
            }
192
        }
193
        let wanted = match plans.get(n.wrapping_sub(1)) {
194
            _ if total != plans.len() => {
195
                Err("the markers are not the file's conflicts now; see `oxy status`")
196
            }
197
            Some(Some(w)) => Ok(w),
198
            _ => Err("both sides changed these lines; edit the markers"),
199
        };
200
        let keep = wanted.and_then(|w| {
201
            let mut a: Vec<Vec<u8>> = w.sides.iter().map(|s| with_newline(s)).collect();
202
            let mut b = found.clone();
203
            a.sort();
204
            b.sort();
205
            if a == b {
206
                Ok(&w.kept)
207
            } else {
208
                Err("the lines between its markers were edited; finish it by hand")
209
            }
210
        });
211
        match keep {
212
            Ok(w) => {
213
                let order: Vec<&Vec<u8>> = if reverse {
214
                    w.iter().rev().collect()
215
                } else {
216
                    w.iter().collect()
217
                };
218
                for s in order {
219
                    out.extend_from_slice(&with_newline(s));
220
                }
221
                settled.push(n);
222
            }
223
            Err(why) => {
224
                left.push((n, why));
225
                for line in &lines[i..=stop] {
226
                    out.extend_from_slice(line);
227
                }
228
            }
229
        }
230
        i = stop + 1;
231
    }
232
    (out, settled, left)
233
}
234
235
/// `(n, total)` from a conflict's opening marker (spec §9.5).
236
fn conflict_header(line: &[u8]) -> Option<(usize, usize)> {
237
    let rest = line.strip_prefix(b"<<<<<<< oxy conflict ")?;
238
    let rest = std::str::from_utf8(rest).ok()?;
239
    let (n, rest) = rest.split_once(" of ")?;
240
    let total = rest.split(|c: char| !c.is_ascii_digit()).next()?;
241
    Some((n.parse().ok()?, total.parse().ok()?))
242
}
243
244
pub fn is_binary(content: &[u8]) -> bool {
245
    content[..content.len().min(BINARY_SNIFF)].contains(&0)
246
}
247
248
impl Repo {
249
    /// Diff the files on disk against `base` and build the operations of a
250
    /// change from `base` to the working directory. `reuse` maps paths to the
251
    /// NodeIds an earlier version of the same change assigned to new paths.
252
    pub(crate) fn build_against(
253
        &self,
254
        base: &State,
255
        base_snapshot: &Snapshot,
256
        reuse: HashMap<String, (NodeId, NodeKind)>,
257
    ) -> Result<BuiltChange> {
258
        let mut fs_entries =
259
            self.walk_working_copy_with(None, &Self::tracked_kinds(base_snapshot), None)?;
260
        self.platform_normalize(&mut fs_entries, base_snapshot);
261
        self.build_from_entries(base, base_snapshot, reuse, &fs_entries)
262
    }
263
264
    /// Read the working copy as the platform left it (spec §7.7): without
265
    /// executable bits a file keeps the bit it has in the base, and without
266
    /// symbolic links a plain file where the base has a symlink is that
267
    /// symlink, written out as its target, not a new file.
268
    fn platform_normalize(&self, entries: &mut BTreeMap<String, FsEntry>, base: &Snapshot) {
269
        let p = self.platform;
270
        if p.exec_bits && p.symlinks {
271
            return;
272
        }
273
        for (path, e) in entries.iter_mut() {
274
            let Some(f) = base.files.get(path) else {
275
                continue;
276
            };
277
            if !p.symlinks && f.kind == NodeKind::Symlink && e.kind == NodeKind::File {
278
                e.kind = NodeKind::Symlink;
279
                e.exec = false;
280
            }
281
            if !p.exec_bits && f.kind == NodeKind::File && e.kind == NodeKind::File {
282
                e.exec = f.mode.exec;
283
            }
284
        }
285
    }
286
287
    /// Like `build_against`, for files given in memory (imports).
288
    pub(crate) fn build_from_entries(
289
        &self,
290
        base: &State,
291
        base_snapshot: &Snapshot,
292
        reuse: HashMap<String, (NodeId, NodeKind)>,
293
        fs_entries: &BTreeMap<String, FsEntry>,
294
    ) -> Result<BuiltChange> {
295
        self.build_from_entries_resolving(base, base_snapshot, reuse, fs_entries, &HashSet::new())
296
    }
297
298
    /// Like `build_from_entries`, settling the replace conflicts of the
299
    /// opaque files in `resolve` to their content (spec §9.3, §9.6): a
300
    /// caller that knows the user chose it, such as the hub's resolver. A
301
    /// snapshot never settles one on its own, since a checkout writes one
302
    /// candidate at the path.
303
    pub(crate) fn build_from_entries_resolving(
304
        &self,
305
        base: &State,
306
        base_snapshot: &Snapshot,
307
        reuse: HashMap<String, (NodeId, NodeKind)>,
308
        fs_entries: &BTreeMap<String, FsEntry>,
309
        resolve: &HashSet<String>,
310
    ) -> Result<BuiltChange> {
311
        self.build_from_entries_with(
312
            base,
313
            base_snapshot,
314
            reuse,
315
            fs_entries,
316
            resolve,
317
            Scope::default(),
318
        )
319
    }
320
321
    /// `build_from_entries` whose new nodes take identifiers derived from
322
    /// `seed` and their paths instead of random ones, so the same input
323
    /// always builds the same change (a Git import, spec §23).
324
    ///
325
    /// With `dir_needed`, the snapshot and entries hold only the paths the
326
    /// import touches (and the directories a removal may leave empty); a
327
    /// directory stays while `dir_needed` says the new tree has something
328
    /// under it.
329
    pub(crate) fn build_from_entries_seeded(
330
        &self,
331
        base: &State,
332
        base_snapshot: &Snapshot,
333
        fs_entries: &BTreeMap<String, FsEntry>,
334
        seed: [u8; 32],
335
        dir_needed: Option<&dyn Fn(&str) -> bool>,
336
    ) -> Result<BuiltChange> {
337
        self.build_from_entries_with(
338
            base,
339
            base_snapshot,
340
            HashMap::new(),
341
            fs_entries,
342
            &HashSet::new(),
343
            Scope {
344
                seed: Some(seed),
345
                dir_needed,
346
            },
347
        )
348
    }
349
350
    /// `build_from_entries` for a snapshot most of whose files are known
351
    /// unchanged (oxygen#41): only the paths that may differ are built
352
    /// (files read rather than known unchanged, base files gone from disk,
353
    /// and the directories those may leave empty), against a snapshot of
354
    /// just those, so a large tree with a few edits costs what they touch.
355
    fn build_changed_only(
356
        &self,
357
        base: &State,
358
        base_snapshot: &Snapshot,
359
        reuse: HashMap<String, (NodeId, NodeKind)>,
360
        fs_entries: &BTreeMap<String, FsEntry>,
361
    ) -> Result<BuiltChange> {
362
        let pick = crate::timing::phase("snapshot:   of which picking the changed");
363
        // A file known unchanged may still have changed mode: that alters
364
        // neither its size nor its time.
365
        let changed: BTreeMap<String, FsEntry> = join(fs_entries, &base_snapshot.files)
366
            .filter_map(|(p, e, f)| {
367
                let e = e?;
368
                (!e.unchanged || f.is_none_or(|f| f.kind != e.kind || f.mode.exec != e.exec))
369
                    .then(|| (p.clone(), e.clone()))
370
            })
371
            .collect();
372
        let mut files: BTreeMap<String, crate::state::FileState> = BTreeMap::new();
373
        for p in changed.keys() {
374
            if let Some(f) = base_snapshot.files.get(p) {
375
                files.insert(p.clone(), f.clone());
376
            }
377
        }
378
        // Paths the base has and the disk does not.
379
        let gone = join(&base_snapshot.files, fs_entries)
380
            .filter_map(|(p, f, e)| (e.is_none()).then_some((p, f?)));
381
        for (p, f) in gone {
382
            if f.kind == NodeKind::Dir {
383
                continue;
384
            }
385
            files.insert(p.clone(), f.clone());
386
            let mut end = p.len();
387
            while let Some(i) = p[..end].rfind('/') {
388
                let dir = &p[..i];
389
                if files.contains_key(dir) {
390
                    break;
391
                }
392
                if let Some(d) = base_snapshot.files.get(dir) {
393
                    files.insert(dir.to_string(), d.clone());
394
                }
395
                end = i;
396
            }
397
        }
398
        let part = Snapshot {
399
            tree: base_snapshot.tree.clone(),
400
            files,
401
            unrendered: BTreeSet::new(),
402
        };
403
        drop(pick);
404
        let _build = crate::timing::phase("snapshot:   of which building the change");
405
        // Whether anything on disk is under `dir`.
406
        let dir_needed = |dir: &str| {
407
            let prefix = format!("{dir}/");
408
            fs_entries
409
                .range(prefix.clone()..)
410
                .next()
411
                .is_some_and(|(k, _)| k.starts_with(&prefix))
412
        };
413
        self.build_from_entries_with(
414
            base,
415
            &part,
416
            reuse,
417
            &changed,
418
            &HashSet::new(),
419
            Scope {
420
                seed: None,
421
                dir_needed: Some(&dir_needed),
422
            },
423
        )
424
    }
425
426
    fn build_from_entries_with(
427
        &self,
428
        base: &State,
429
        base_snapshot: &Snapshot,
430
        reuse: HashMap<String, (NodeId, NodeKind)>,
431
        fs_entries: &BTreeMap<String, FsEntry>,
432
        resolve: &HashSet<String>,
433
        scope: Scope,
434
    ) -> Result<BuiltChange> {
435
        let Scope { seed, dir_needed } = scope;
436
        let mut laps = crate::timing::Laps::start();
437
        // Attributes come from the files being committed, so a new
438
        // `.oxyattributes` applies to the same change that adds it.
439
        let attrs = crate::attributes::Attributes::from_files(
440
            fs_entries
441
                .iter()
442
                .filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes"))
443
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
444
        );
445
        let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs);
446
        b.resolve = resolve.clone();
447
        b.node_seed = seed;
448
        laps.lap("build: builder");
449
        // Rename detection (spec §7.1, §23): a removed file and an added file
450
        // of the same kind whose contents are at least half alike become one
451
        // move of the same node, so identity survives across the rename.
452
        let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p));
453
        let renamed_from: HashMap<&str, &str> = renames
454
            .iter()
455
            .map(|(old, new)| (new.as_str(), old.as_str()))
456
            .collect();
457
        let renamed_to: HashMap<&str, &str> = renames
458
            .iter()
459
            .map(|(old, new)| (old.as_str(), new.as_str()))
460
            .collect();
461
        laps.lap("build: renames");
462
        b.diff_in_parallel(fs_entries);
463
        laps.lap("build: diffs");
464
        for (path, fs_entry) in fs_entries {
465
            match base_snapshot.files.get(path) {
466
                None => match renamed_from.get(path.as_str()) {
467
                    Some(old) => {
468
                        b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])?
469
                    }
470
                    None => b.add_path(self, path, fs_entry)?,
471
                },
472
                Some(expected) => b.update_path(self, path, fs_entry, expected)?,
473
            }
474
        }
475
        laps.lap("build: paths");
476
        // A directory stays while anything under it exists or is out of
477
        // scope: the directories above each such path, gathered once.
478
        let mut needed_dirs: HashSet<&str> = HashSet::new();
479
        fn parents_of<'a>(p: &'a str, set: &mut HashSet<&'a str>) {
480
            let mut end = p.len();
481
            while let Some(i) = p[..end].rfind('/') {
482
                if !set.insert(&p[..i]) {
483
                    break;
484
                }
485
                end = i;
486
            }
487
        }
488
        for p in fs_entries.keys() {
489
            parents_of(p, &mut needed_dirs);
490
        }
491
        for p in base_snapshot.files.keys() {
492
            if !self.in_scope(p) {
493
                parents_of(p, &mut needed_dirs);
494
            }
495
        }
496
        for (path, expected) in &base_snapshot.files {
497
            if fs_entries.contains_key(path)
498
                || !self.in_scope(path)
499
                || renamed_to.contains_key(path.as_str())
500
            {
501
                continue;
502
            }
503
            let still_needed = expected.kind == NodeKind::Dir
504
                && match dir_needed {
505
                    Some(f) => f(path),
506
                    None => needed_dirs.contains(path.as_str()),
507
                };
508
            if !still_needed {
509
                b.remove_path(path, expected);
510
            }
511
        }
512
        laps.lap("build: removals");
513
        if seed.is_some() {
514
            b.retire_stray_placements();
515
        }
516
        b.retire_unified();
517
        laps.lap("build: stray placements");
518
        let mut statuses = b.statuses;
519
        statuses.sort_by(|a, b| a.path.cmp(&b.path));
520
        Ok(BuiltChange {
521
            tree_ops: b.tree_ops,
522
            edits: b.edits,
523
            contributors: b.contributors.into_iter().collect(),
524
            resolves: b.resolves,
525
            statuses,
526
            blobs: b.blobs,
527
        })
528
    }
529
530
    /// Store a change and, when `core.sign = change` and we are its author,
531
    /// sign it. A rewritten change by another author is stored unsigned: only
532
    /// its author (or a delegate) can vouch for it.
533
    pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> {
534
        let ours = change.author == self.author();
535
        if self.config.sign_changes && ours && self.secret_key()?.is_none() {
536
            return fail("core.sign is \"change\" but no secret key is available for author.key");
537
        }
538
        let storing = crate::timing::phase("store: the change");
539
        let hash = self.store.put_change(change)?;
540
        drop(storing);
541
        if self.config.sign_changes && ours {
542
            let _t = crate::timing::phase("store: sign it");
543
            self.sign_change(&hash)?;
544
        }
545
        Ok(hash)
546
    }
547
548
    fn resolutions_path(&self) -> PathBuf {
549
        self.oxy
550
            .join("workspaces")
551
            .join(self.workspace.to_hex())
552
            .join("resolve")
553
    }
554
555
    /// The paths whose replace conflicts the user chose to settle with what
556
    /// the working copy holds there (`oxy resolve PATH`, spec §9.3).
557
    pub fn resolutions(&self) -> Result<BTreeSet<String>> {
558
        match fs::read_to_string(self.resolutions_path()) {
559
            Ok(text) => Ok(text
560
                .lines()
561
                .filter(|l| !l.is_empty())
562
                .map(String::from)
563
                .collect()),
564
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()),
565
            Err(e) => Err(e.into()),
566
        }
567
    }
568
569
    /// Settle the replace conflicts at `paths` (a file's or a subrepo pin's,
570
    /// spec §9.3) with what the working copy holds there: the next snapshot
571
    /// replaces every live candidate with it. Refused for a path whose base
572
    /// has no replace conflict.
573
    pub fn choose_resolutions(&self, base: &Snapshot, paths: &[String]) -> Result<()> {
574
        for p in paths {
575
            if !has_replace_conflict(base, p) {
576
                return Err(RepoError::Msg(format!(
577
                    "{p} has no replace conflict to settle (a text file's conflict is settled by editing out its markers, or `oxy resolve --both {p}`)"
578
                )));
579
            }
580
        }
581
        let mut all = self.resolutions()?;
582
        all.extend(paths.iter().cloned());
583
        let text: String = all.iter().map(|p| format!("{p}\n")).collect();
584
        crate::durable::write_atomic(&self.resolutions_path(), text.as_bytes())?;
585
        Ok(())
586
    }
587
588
    /// Settle the adjacent-insertion conflicts of the text files at `paths`
589
    /// (oxygen#121) by keeping every side, earliest change first (latest
590
    /// first with `reverse`): each one's markers in the working copy give
591
    /// way to its sides' lines, and the next snapshot records the result.
592
    /// A conflict whose sides changed the same lines, or whose text between
593
    /// the markers was edited, keeps its markers. With no paths, every
594
    /// conflicted text file.
595
    pub fn keep_both(
596
        &self,
597
        snap: &SnapshotResult,
598
        paths: &[String],
599
        reverse: bool,
600
    ) -> Result<Vec<KeptBoth>> {
601
        let paths: Vec<String> = if paths.is_empty() {
602
            snap.base_snapshot
603
                .files
604
                .iter()
605
                .filter(|(_, f)| matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted()))
606
                .map(|(p, _)| p.clone())
607
                .collect()
608
        } else {
609
            paths.to_vec()
610
        };
611
        let mut out = Vec::new();
612
        for p in paths {
613
            let conflicted = match snap.base_snapshot.files.get(&p).map(|f| (f, &f.content)) {
614
                Some((f, FileContent::Text { materialized, .. }))
615
                    if materialized.is_conflicted() =>
616
                {
617
                    f
618
                }
619
                Some((_, FileContent::Opaque { blobs })) if blobs.len() > 1 => {
620
                    return Err(RepoError::Msg(format!(
621
                        "{p} has a replace conflict: keep one version with `oxy resolve {p}`"
622
                    )))
623
                }
624
                _ => {
625
                    return Err(RepoError::Msg(format!(
626
                        "{p} has no text conflict to settle"
627
                    )))
628
                }
629
            };
630
            let sides = snap.base.both_sides(&conflicted.node);
631
            let disk = self.root().join(&p);
632
            let text = fs::read(&disk)?;
633
            let (bytes, settled, left) = keep_both_text(&text, &sides, reverse);
634
            if !settled.is_empty() {
635
                fs::write(&disk, bytes)?;
636
            }
637
            out.push(KeptBoth {
638
                path: p,
639
                settled,
640
                identical: Vec::new(),
641
                left,
642
            });
643
        }
644
        Ok(out)
645
    }
646
647
    /// Settle what a checkout may settle by itself in each conflicted text
648
    /// file ([`State::settle_plans`]): conflicts whose sides are identical,
649
    /// keeping one (oxygen#146), and in files whose `.oxyattributes` say
650
    /// `merge=union`, adjacent insertions, keeping both sides earliest
651
    /// change first (oxygen#145). The next commit records the resolution.
652
    /// The files it settled something in.
653
    pub(crate) fn settle_conflicts(&self, command: &str) -> Result<Vec<KeptBoth>> {
654
        let snap = self.snapshot(command)?;
655
        let mut out = Vec::new();
656
        for (p, f) in &snap.base_snapshot.files {
657
            if !matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted())
658
            {
659
                continue;
660
            }
661
            let plans = snap
662
                .base
663
                .settle_plans(&f.node, snap.attributes.lookup(p).union);
664
            if plans.iter().all(Option::is_none) {
665
                continue;
666
            }
667
            let disk = self.root().join(p);
668
            let Ok(text) = fs::read(&disk) else { continue };
669
            let (bytes, settled, left) = settle_text(&text, &plans, false);
670
            if settled.is_empty() {
671
                continue;
672
            }
673
            fs::write(&disk, bytes)?;
674
            let identical = settled
675
                .iter()
676
                .copied()
677
                .filter(|n| plans[n - 1].as_ref().is_some_and(|w| w.identical))
678
                .collect();
679
            out.push(KeptBoth {
680
                path: p.clone(),
681
                settled,
682
                identical,
683
                left,
684
            });
685
        }
686
        Ok(out)
687
    }
688
689
    /// Every conflict of `snap`, `state`'s materialization, settled as a
690
    /// checkout settles them ([`State::settle_plans`]): the new text of
691
    /// each conflicted file, when each is a text file whose every conflict
692
    /// has identical sides (one kept, oxygen#146) or, in a file its
693
    /// `.oxyattributes` mark `merge=union`, is an adjacent insertion (both
694
    /// kept, oxygen#145); `None` when any conflict needs a person, or there
695
    /// is none. A host lands a proposal with these as a resolution, as a
696
    /// checkout settles them in a working copy.
697
    pub fn union_resolution(state: &State, snap: &Snapshot) -> Option<Vec<(String, Vec<u8>)>> {
698
        if snap.tree.is_conflicted() {
699
            return None;
700
        }
701
        let attrs = Repo::state_attributes(snap);
702
        let mut out = Vec::new();
703
        for (p, f) in &snap.files {
704
            if !f.is_conflicted() {
705
                continue;
706
            }
707
            let FileContent::Text { bytes, .. } = &f.content else {
708
                return None;
709
            };
710
            let plans = state.settle_plans(&f.node, attrs.lookup(p).union);
711
            let (text, _, left) = settle_text(bytes, &plans, false);
712
            if !left.is_empty() || plans.iter().any(Option::is_none) {
713
                return None;
714
            }
715
            out.push((p.clone(), text));
716
        }
717
        (!out.is_empty()).then_some(out)
718
    }
719
720
    /// The chosen resolutions `base` still needs; the rest are forgotten.
721
    fn resolutions_for(&self, base: &Snapshot) -> Result<HashSet<String>> {
722
        let all = self.resolutions()?;
723
        if all.is_empty() {
724
            return Ok(HashSet::new());
725
        }
726
        let live: HashSet<String> = all
727
            .iter()
728
            .filter(|p| has_replace_conflict(base, p))
729
            .cloned()
730
            .collect();
731
        if live.is_empty() {
732
            match fs::remove_file(self.resolutions_path()) {
733
                Ok(()) => {}
734
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
735
                Err(e) => return Err(e.into()),
736
            }
737
        }
738
        Ok(live)
739
    }
740
741
    /// Snapshot the working copy into the working-copy change (spec §12.3).
742
    /// The result carries the full base state and its whole rendering.
743
    pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> {
744
        self.snapshot_as(command, false, false)
745
    }
746
747
    /// Snapshot the working copy as [`Repo::snapshot`] does, for commands
748
    /// that need its statuses, conflicts and base history, and the graphs
749
    /// of the files it changes: status, log, show, commit, diff, hunks and
750
    /// discard. Text files known unchanged since the last manifest are
751
    /// neither read nor rendered: the result's `base` is a history-only
752
    /// state without their graphs, and its `base_snapshot` lists them in
753
    /// `unrendered` with empty bytes. They are never conflicted, and no
754
    /// change the working copy makes touches them.
755
    pub fn snapshot_status(&self, command: &str) -> Result<SnapshotResult> {
756
        self.snapshot_as(command, true, false)
757
    }
758
759
    /// `snapshot_as`; `for_commit` writes the manifest even where a status
760
    /// would not bother (an edited file is read next time whatever it
761
    /// says), since after the commit that file is unchanged, and records no
762
    /// operation: the commit's own, recorded straight after, replaces the
763
    /// working copy, so the snapshot's would cost a durable write for
764
    /// nothing. Should the commit fail, the files are still on disk and the
765
    /// next snapshot finds them.
766
    fn snapshot_as(
767
        &self,
768
        command: &str,
769
        partial: bool,
770
        for_commit: bool,
771
    ) -> Result<SnapshotResult> {
772
        let _lock = self.lock_snapshot()?;
773
        let batch = self.store.begin_batch()?;
774
        let result = self.snapshot_locked(command, partial, for_commit);
775
        batch.finish()?;
776
        result
777
    }
778
779
    fn snapshot_locked(
780
        &self,
781
        command: &str,
782
        partial: bool,
783
        for_commit: bool,
784
    ) -> Result<SnapshotResult> {
785
        let _all = crate::timing::phase("snapshot");
786
        let (op_ref, op) = self.head_op()?;
787
        let (old_hash, old_wc) = self.working_copy()?;
788
        // The base's history first: its graph keys say which files the
789
        // manifest already knows equal to it.
790
        let mut base = {
791
            let _t = crate::timing::phase("snapshot: load base history");
792
            self.load_state_indexed(&old_wc.deps)?
793
        };
794
        let view = {
795
            let _t = crate::timing::phase("snapshot: tree view");
796
            self.tree_view(&mut base)?
797
        };
798
        let manifest = {
799
            let _t = crate::timing::phase("snapshot: read manifest");
800
            self.read_manifest()?.map(|(_, _, m)| m)
801
        };
802
        let tracked: BTreeMap<String, (NodeKind, Option<ObjectRef>)> = {
803
            let _t = crate::timing::phase("snapshot: tracked paths");
804
            // In the view's path order, so the map is built without searching.
805
            view.entries
806
                .iter()
807
                .filter(|(_, e)| e.kind != NodeKind::Dir)
808
                .map(|(p, e)| {
809
                    let blob = if e.kind != NodeKind::File || e.mode.opaque {
810
                        base.live_blobs(&e.node).first().copied()
811
                    } else {
812
                        None
813
                    };
814
                    (p.clone(), (e.kind, blob))
815
                })
816
                .collect()
817
        };
818
        // A text file's graph key, worked out when the walk asks for it.
819
        let key_of = |p: &str| -> Option<[u8; 32]> {
820
            let e = view.entries.get(p)?;
821
            if e.kind != NodeKind::File || e.mode.opaque {
822
                return None;
823
            }
824
            base.graph_key(&e.node)
825
        };
826
        let mut fs_entries = {
827
            let _t = crate::timing::phase("snapshot: read working copy");
828
            self.walk_working_copy_with(manifest.as_deref(), &tracked, Some(&key_of))?
829
        };
830
        // The keys of the files read, before building graphs drops them: a
831
        // file found equal to its base rendering keeps its key.
832
        let keys: HashMap<String, [u8; 32]> = fs_entries
833
            .iter()
834
            .filter(|(_, e)| !e.unchanged && e.kind == NodeKind::File)
835
            .filter_map(|(p, _)| Some((p.clone(), key_of(p)?)))
836
            .collect();
837
        let base_snapshot = {
838
            let _t = crate::timing::phase("snapshot: load and render base");
839
            if partial {
840
                let sets = crate::timing::phase("snapshot:   of which choosing graphs");
841
                // Every file but those on disk known unchanged.
842
                let need: Vec<(&str, NodeId)> = join(&view.entries, &fs_entries)
843
                    .filter_map(|(p, v, f)| {
844
                        let v = v?;
845
                        (v.kind == NodeKind::File && !f.is_some_and(|f| f.unchanged))
846
                            .then_some((p.as_str(), v.node))
847
                    })
848
                    .collect();
849
                let render: std::collections::HashSet<&str> =
850
                    need.iter().map(|(p, _)| *p).collect();
851
                let need: Vec<NodeId> = need.iter().map(|(_, n)| *n).collect();
852
                drop(sets);
853
                {
854
                    let _t = crate::timing::phase("snapshot:   of which building graphs");
855
                    self.load_graphs_of(&mut base, &need)?;
856
                }
857
                let _t = crate::timing::phase("snapshot:   of which rendering");
858
                base.materialize_only(&render)
859
            } else {
860
                self.load_all_graphs(&mut base)?;
861
                base.materialize()
862
            }
863
        };
864
865
        // Reuse the NodeIds the previous working-copy version assigned to new
866
        // paths, so an unchanged working copy hashes identically (spec §12.3).
867
        let reuse = added_node_paths(&old_wc, &base_snapshot);
868
        self.platform_normalize(&mut fs_entries, &base_snapshot);
869
        // Replace conflicts the user settled with `oxy resolve PATH`
870
        // (oxygen#140): those still in the base; a choice the base no longer
871
        // needs (once committed) is forgotten.
872
        let resolve = self.resolutions_for(&base_snapshot)?;
873
        let built = {
874
            let _t = crate::timing::phase("snapshot: diff against base");
875
            if !resolve.is_empty() {
876
                self.build_from_entries_resolving(
877
                    &base,
878
                    &base_snapshot,
879
                    reuse,
880
                    &fs_entries,
881
                    &resolve,
882
                )?
883
            } else if partial && self.config.sparse_paths.is_empty() {
884
                #[cfg(test)]
885
                let full =
886
                    self.build_from_entries(&base, &base_snapshot, reuse.clone(), &fs_entries)?;
887
                let built = self.build_changed_only(&base, &base_snapshot, reuse, &fs_entries)?;
888
                #[cfg(test)]
889
                {
890
                    // New nodes take random identifiers: compare them by
891
                    // order of appearance.
892
                    let norm = |b: &BuiltChange| {
893
                        let text = format!("{:?} {:?} {:?}", b.tree_ops, b.edits, b.statuses);
894
                        let mut seen: Vec<String> = Vec::new();
895
                        let mut out = String::new();
896
                        let mut rest = text.as_str();
897
                        while let Some(i) = rest.find("NodeId(") {
898
                            out.push_str(&rest[..i]);
899
                            let end = i + rest[i..].find(')').unwrap() + 1;
900
                            let id = rest[i..end].to_string();
901
                            let n = seen.iter().position(|x| *x == id).unwrap_or_else(|| {
902
                                seen.push(id);
903
                                seen.len() - 1
904
                            });
905
                            out.push_str(&format!("N{n}"));
906
                            rest = &rest[end..];
907
                        }
908
                        out.push_str(rest);
909
                        out
910
                    };
911
                    assert_eq!(
912
                        norm(&built),
913
                        norm(&full),
914
                        "a changed-only build is the full one"
915
                    );
916
                }
917
                built
918
            } else {
919
                self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)?
920
            }
921
        };
922
        // Text files found equal to an unconflicted base rendering keep its
923
        // graph key, so the next snapshot need not read or render them.
924
        {
925
            let changed: HashSet<&str> = built.statuses.iter().map(|s| s.path.as_str()).collect();
926
            for (p, e) in fs_entries.iter_mut() {
927
                if e.unchanged || e.kind != NodeKind::File || changed.contains(p.as_str()) {
928
                    continue;
929
                }
930
                e.graph_key = match base_snapshot.files.get(p).map(|f| &f.content) {
931
                    Some(FileContent::Text {
932
                        bytes,
933
                        materialized,
934
                    }) if !materialized.is_conflicted() && *bytes == *e.content => {
935
                        keys.get(p).copied()
936
                    }
937
                    _ => None,
938
                };
939
            }
940
        }
941
942
        let mut wc = Change {
943
            version: crate::objects::CHANGE_VERSION,
944
            change_id: old_wc.change_id,
945
            deps: vec![],
946
            author: self.author(),
947
            hlc: old_wc.hlc,
948
            message: old_wc.message.clone(),
949
            tree_ops: built.tree_ops,
950
            edits: built.edits,
951
            kind: ChangeKind::WorkingCopy,
952
        };
953
        let base_heads = base.maximal(&old_wc.deps);
954
        let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect();
955
        deps.extend(referenced_changes(&wc, &base));
956
        deps.extend(built.contributors.iter().copied());
957
        wc.deps = deps.into_iter().collect();
958
        wc.normalize();
959
        let resolves = built.resolves;
960
        let contributors = built.contributors;
961
        let statuses = built.statuses;
962
        // Empty snapshot: hash the candidate before storing; only record an op on change.
963
        let record = crate::timing::phase("snapshot: hash and record");
964
        // Encoded once, its texts stored a batch at a time as they are
965
        // made (a stored blob is not stored again); the change itself only
966
        // when it changed.
967
        // For a commit the change is neither encoded, stored nor recorded:
968
        // the commit stores its own, built from this one's operations, and
969
        // its texts with it. The hash it would have is not needed.
970
        let (new_hash, changed, encoded) = if for_commit {
971
            (old_hash, true, Vec::new())
972
        } else {
973
            let encoded = wc.encode_streaming(|batch| self.store.put_texts(&batch))?;
974
            let new_hash = crate::hash::hash_object(crate::hash::ObjectType::Change, &encoded);
975
            (new_hash, new_hash != old_hash, encoded)
976
        };
977
        if changed && !for_commit {
978
            self.store.put(crate::hash::ObjectType::Change, &encoded)?;
979
            let mut view = op.view.clone();
980
            view.workspaces.insert(self.workspace, new_hash);
981
            self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?;
982
        }
983
        drop(record);
984
        // The prompt compares against this without recording anything.
985
        let _t = crate::timing::phase("snapshot: write manifest");
986
        let conflicted = base_snapshot.is_conflicted();
987
        let uncommitted = !wc.tree_ops.is_empty() || !wc.edits.is_empty();
988
        // Rewriting the manifest pays only when it would teach the next
989
        // snapshot something: other flags, a file found equal to the base
990
        // (it need not be read again), a new blob, a path added or gone.
991
        // An edited file is read next time whatever it says.
992
        let worth = match self.read_manifest()? {
993
            _ if for_commit => true,
994
            None => true,
995
            Some((c, u, m)) => {
996
                c != conflicted
997
                    || u != uncommitted
998
                    || m.entries.len() != fs_entries.len()
999
                    || fs_entries.iter().any(|(p, e)| {
1000
                        !e.unchanged && (e.graph_key.is_some() || !m.entries.contains_key(p))
1001
                    })
1002
                    || built
1003
                        .blobs
1004
                        .iter()
1005
                        .any(|(p, b)| m.entries.get(p).and_then(|e| e.blob) != Some(*b))
1006
                    // With uncommitted work, the status cache below holds
1007
                    // only while the manifest describes the edited files as
1008
                    // they are now.
1009
                    || (uncommitted && statuses.iter().any(|st| {
1010
                        let Some(e) = fs_entries.get(&st.path) else {
1011
                            return m.entries.contains_key(&st.path);
1012
                        };
1013
                        let Some(me) = m.entries.get(&st.path) else {
1014
                            return true;
1015
                        };
1016
                        let hash = e
1017
                            .content_hash
1018
                            .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes());
1019
                        let stat = e.stat.or_else(|| {
1020
                            fs::symlink_metadata(self.root.join(&st.path))
1021
                                .ok()
1022
                                .map(|md| (md.len(), super::prompt::mtime_ns(&md)))
1023
                        });
1024
                        // The executable bit too: a mode change moves
1025
                        // neither size nor time.
1026
                        me.hash != hash || me.exec != e.exec || stat != Some((me.size, me.mtime_ns))
1027
                    }))
1028
            }
1029
        };
1030
        let attributes = crate::attributes::Attributes::from_files(
1031
            fs_entries
1032
                .iter()
1033
                .filter(|(p, _)| is_attributes(p))
1034
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
1035
        );
1036
        let mut manifest = None;
1037
        if for_commit {
1038
            manifest = Some(PendingManifest {
1039
                entries: fs_entries,
1040
                blobs: built.blobs.clone(),
1041
                dirs: self.walked_dirs.take(),
1042
                written: super::prompt::now_ns(),
1043
            });
1044
        } else {
1045
            if worth {
1046
                self.write_manifest_entries(conflicted, uncommitted, &fs_entries, &built.blobs)?;
1047
            }
1048
            self.write_status_cache((uncommitted && !conflicted).then_some(&new_hash), &statuses);
1049
        }
1050
        Ok(SnapshotResult {
1051
            wc_hash: new_hash,
1052
            wc,
1053
            base,
1054
            base_snapshot,
1055
            statuses,
1056
            changed,
1057
            resolves,
1058
            contributors,
1059
            attributes,
1060
            untracked_nested: self.untracked_nested.borrow().clone(),
1061
            manifest,
1062
        })
1063
    }
1064
1065
    /// `oxy status --json` (spec §9.5): the changed paths and every conflict
1066
    /// of the working copy's base with its kind, path, and contributing
1067
    /// ChangeIds.
1068
    pub fn status_json(&self, snap: &SnapshotResult) -> Result<String> {
1069
        let heads = self.bookmark_heads()?;
1070
        let base_snapshot = &snap.base_snapshot;
1071
        let by_node = base_snapshot.tree.by_node();
1072
        let path_of = |n: &NodeId| by_node.get(n).map(|e| e.path.clone());
1073
        let id_of = |h: &ChangeHash| {
1074
            snap.base
1075
                .get(h)
1076
                .map(|m| m.change_id.to_hex())
1077
                .unwrap_or_else(|| h.to_hex())
1078
        };
1079
        let paths = json_array(snap.statuses.iter().map(|s| {
1080
            format!(
1081
                "{{\"path\":{},\"status\":{}}}",
1082
                json_str(&s.path),
1083
                json_str(&format!("{:?}", s.status))
1084
            )
1085
        }));
1086
        let mut conflicts: Vec<String> = Vec::new();
1087
        for (p, f) in &base_snapshot.files {
1088
            if !f.is_conflicted() {
1089
                continue;
1090
            }
1091
            let (kind, changes) = match &f.content {
1092
                FileContent::Text { .. } => {
1093
                    ("text", snap.base.conflict_regions(&f.node).contributors)
1094
                }
1095
                FileContent::Opaque { blobs } => {
1096
                    ("replace", snap.base.blob_contributors(&f.node, blobs))
1097
                }
1098
                FileContent::Dir => continue,
1099
            };
1100
            let resolved = match snap
1101
                .statuses
1102
                .iter()
1103
                .find(|s| s.path == *p)
1104
                .map(|s| s.status)
1105
            {
1106
                Some(ChangeStatus::Resolved) => "true",
1107
                Some(ChangeStatus::ConflictEdited) => "\"markers\"",
1108
                _ => "false",
1109
            };
1110
            conflicts.push(format!(
1111
                "{{\"kind\":{},\"path\":{},\"node\":{},\"changes\":{},\"resolved\":{resolved}}}",
1112
                json_str(kind),
1113
                json_str(p),
1114
                json_str(&f.node.to_hex()),
1115
                json_array(changes.iter().map(|h| json_str(&id_of(h))))
1116
            ));
1117
        }
1118
        for c in &base_snapshot.tree.conflicts {
1119
            let (kind, nodes): (&str, Vec<NodeId>) = match c {
1120
                crate::tree::TreeConflict::Name { nodes, .. } => ("name", nodes.clone()),
1121
                crate::tree::TreeConflict::MultiPlacement { node, .. } => {
1122
                    ("multi-placement", vec![*node])
1123
                }
1124
                crate::tree::TreeConflict::Orphan { node, .. } => ("orphan", vec![*node]),
1125
                crate::tree::TreeConflict::Cycle { node } => ("cycle", vec![*node]),
1126
                crate::tree::TreeConflict::Mode { node, .. } => ("mode", vec![*node]),
1127
            };
1128
            let detail = match c {
1129
                crate::tree::TreeConflict::Name { name, parent, .. } => {
1130
                    let dir = if *parent == NodeId::ROOT {
1131
                        String::new()
1132
                    } else {
1133
                        path_of(parent).unwrap_or_default()
1134
                    };
1135
                    format!(
1136
                        ",\"path\":{}",
1137
                        json_str(&if dir.is_empty() {
1138
                            name.clone()
1139
                        } else {
1140
                            format!("{dir}/{name}")
1141
                        })
1142
                    )
1143
                }
1144
                _ => nodes
1145
                    .first()
1146
                    .and_then(path_of)
1147
                    .map(|p| format!(",\"path\":{}", json_str(&p)))
1148
                    .unwrap_or_default(),
1149
            };
1150
            conflicts.push(format!(
1151
                "{{\"kind\":{},\"nodes\":{}{detail}}}",
1152
                json_str(kind),
1153
                json_array(nodes.iter().map(|n| json_str(&n.to_hex())))
1154
            ));
1155
        }
1156
        Ok(format!(
1157
            "{{\"bookmark\":{},\"working_copy\":{},\"heads\":{},\"clean\":{},\"conflicted\":{},\"paths\":{paths},\"conflicts\":{},\"untracked_nested\":{},\"skipped_large\":{}}}",
1158
            json_str(&self.config.bookmark),
1159
            json_str(&snap.wc.change_id.to_hex()),
1160
            heads.len(),
1161
            snap.statuses.is_empty(),
1162
            base_snapshot.is_conflicted(),
1163
            json_array(conflicts),
1164
            json_array(snap.untracked_nested.iter().map(|p| json_str(p))),
1165
            json_array(
1166
                self.skipped_large
1167
                    .borrow()
1168
                    .iter()
1169
                    .map(|(p, n)| format!("{{\"path\":{},\"size\":{n}}}", json_str(p)))
1170
            )
1171
        ))
1172
    }
1173
1174
    /// Turn the working-copy change into a normal change on the bookmark (spec §12.3).
1175
    pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> {
1176
        self.commit_selection(message, extra_deps, &Selection::All)
1177
    }
1178
1179
    /// The hunks of the working copy, for `oxy commit --interactive`: each
1180
    /// tree operation and each edit, in the order `select_hunks` numbers them.
1181
    pub fn hunks(&self) -> Result<(SnapshotResult, Vec<Hunk>)> {
1182
        let snap = self.snapshot_status("oxy commit")?;
1183
        let hunks = render_hunks(&snap.wc, &snap.base, &snap.base_snapshot);
1184
        Ok((snap, hunks))
1185
    }
1186
1187
    /// Commit part of the working copy (spec §12.3). What is not selected
1188
    /// stays on disk as uncommitted work: the next snapshot rebuilds it
1189
    /// against the new base, so the working copy always describes the
1190
    /// directory.
1191
    /// The graph key in `st` of each text file at `paths`, the paths a
1192
    /// commit changed: only their graphs changed, so the keys the manifest
1193
    /// holds for the others still stand (and any that does not is simply
1194
    /// not trusted by the next snapshot, which compares keys). Files with
1195
    /// an `eol` attribute are left out: their bytes on disk are not the
1196
    /// rendering.
1197
    fn committed_graph_keys(
1198
        &self,
1199
        st: &State,
1200
        paths: &[String],
1201
        attrs: &crate::attributes::Attributes,
1202
    ) -> HashMap<String, [u8; 32]> {
1203
        let _t = crate::timing::phase("commit: graph keys");
1204
        let view = st.view();
1205
        paths
1206
            .iter()
1207
            .filter_map(|p| {
1208
                let e = view.entries.get(p)?;
1209
                (e.kind == NodeKind::File && !e.mode.opaque && attrs.lookup(p).eol.is_none())
1210
                    .then_some(())?;
1211
                Some((p.clone(), st.graph_key(&e.node)?))
1212
            })
1213
            .collect()
1214
    }
1215
1216
    pub fn commit_selection(
1217
        &self,
1218
        message: &str,
1219
        extra_deps: &[ChangeHash],
1220
        selection: &Selection,
1221
    ) -> Result<ChangeHash> {
1222
        Ok(self
1223
            .commit_selection_partial(message, extra_deps, selection)?
1224
            .0)
1225
    }
1226
1227
    /// As [`Repo::commit_selection`], and whether the commit left part of
1228
    /// the working copy uncommitted: when it did not, nothing is left, and
1229
    /// no snapshot is needed to say so.
1230
    pub fn commit_selection_partial(
1231
        &self,
1232
        message: &str,
1233
        extra_deps: &[ChangeHash],
1234
        selection: &Selection,
1235
    ) -> Result<(ChangeHash, bool)> {
1236
        // The commit's objects are written together, made durable at once
1237
        // before the operation that names them (spec §13.6).
1238
        let batch = self.store.begin_batch()?;
1239
        let out = self.commit_selection_batched(message, extra_deps, selection);
1240
        batch.finish()?;
1241
        out
1242
    }
1243
1244
    /// Write the manifest a commit's snapshot left to the commit, with the
1245
    /// graph keys `keys` sets.
1246
    fn write_pending_manifest(
1247
        &self,
1248
        mut m: PendingManifest,
1249
        conflicted: bool,
1250
        keys: HashMap<String, [u8; 32]>,
1251
    ) -> Result<()> {
1252
        for (path, k) in keys {
1253
            if let Some(e) = m.entries.get_mut(&path) {
1254
                e.graph_key = Some(k);
1255
            }
1256
        }
1257
        self.write_manifest_entries_as_of(
1258
            conflicted,
1259
            false,
1260
            &m.entries,
1261
            &m.blobs,
1262
            m.written,
1263
            m.dirs.unwrap_or_default(),
1264
        )
1265
    }
1266
1267
    fn commit_selection_batched(
1268
        &self,
1269
        message: &str,
1270
        extra_deps: &[ChangeHash],
1271
        selection: &Selection,
1272
    ) -> Result<(ChangeHash, bool)> {
1273
        let _wc = self.lock_working_copy()?;
1274
        let mut snap = match self.snapshot_from_cache()? {
1275
            Some(snap) => snap,
1276
            None => self.snapshot_as("oxy commit", true, true)?,
1277
        };
1278
        let pending = snap.manifest.take();
1279
        if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() {
1280
            if let Some(m) = pending {
1281
                let conflicted = snap.base_snapshot.is_conflicted();
1282
                self.write_pending_manifest(m, conflicted, HashMap::new())?;
1283
            }
1284
            return fail("nothing to commit: the working copy matches its base");
1285
        }
1286
        if let Some(s) = snap
1287
            .statuses
1288
            .iter()
1289
            .find(|s| s.status == ChangeStatus::ConflictEdited)
1290
        {
1291
            return fail(format!("{} still contains conflict markers", s.path));
1292
        }
1293
        if message.trim().is_empty() {
1294
            return fail("a commit message is required");
1295
        }
1296
        // What the whole working copy references, before a whole commit
1297
        // takes its operations over (they are moved, not copied: for a
1298
        // large tree they are most of the memory).
1299
        let wc_refs = referenced_changes(&snap.wc, &snap.base);
1300
        let changed_paths: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
1301
        let (tree_ops, edits, partial) = match selection {
1302
            Selection::All => (
1303
                std::mem::take(&mut snap.wc.tree_ops),
1304
                std::mem::take(&mut snap.wc.edits),
1305
                false,
1306
            ),
1307
            Selection::Paths(paths) => {
1308
                let (o, e) = select_paths(&snap.wc, &snap.base_snapshot, paths);
1309
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
1310
                (o, e, partial)
1311
            }
1312
            Selection::Hunks(indices) => {
1313
                let (o, e) = select_hunks(&snap.wc, indices, &snap.base_snapshot.tree.unified);
1314
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
1315
                (o, e, partial)
1316
            }
1317
        };
1318
        if tree_ops.is_empty() && edits.is_empty() {
1319
            return fail("nothing selected to commit");
1320
        }
1321
        // A resolution may leave other work uncommitted (oxygen#118), but
1322
        // takes every file it resolves, whole.
1323
        if partial && snap.resolves {
1324
            let resolved = snap
1325
                .statuses
1326
                .iter()
1327
                .filter(|s| s.status == ChangeStatus::Resolved)
1328
                .map(|s| s.path.as_str());
1329
            let missing: Vec<&str> = match selection {
1330
                Selection::Paths(paths) => resolved
1331
                    .filter(|p| {
1332
                        !paths.iter().any(|x| {
1333
                            p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))
1334
                        })
1335
                    })
1336
                    .collect(),
1337
                _ => resolved.collect(),
1338
            };
1339
            if !missing.is_empty() {
1340
                return fail(format!(
1341
                    "a conflict resolution takes every file it resolves, whole: commit by paths, including {}",
1342
                    missing.join(", ")
1343
                ));
1344
            }
1345
        }
1346
        // pre-commit hook: may veto.
1347
        self.run_hook(
1348
            "pre-commit",
1349
            &format!(
1350
                "{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}",
1351
                json_str(message),
1352
                json_array(snap.statuses.iter().map(|s| format!(
1353
                    "{{\"path\":{},\"status\":{}}}",
1354
                    json_str(&s.path),
1355
                    json_str(&format!("{:?}", s.status))
1356
                )))
1357
            ),
1358
        )?;
1359
        let (op_ref, op) = self.head_op()?;
1360
        let mut deps: BTreeSet<ChangeHash> = wc_refs;
1361
        // semantic-deps hook: may add explicit dependencies (spec §8.1).
1362
        if self.config.semantic_deps {
1363
            let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
1364
            let by_node = snap.base_snapshot.tree.by_node();
1365
            let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new();
1366
            let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
1367
            for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) {
1368
                if !seen.insert(*h) {
1369
                    continue;
1370
                }
1371
                let c = self.store.get_change(h)?;
1372
                let mut paths: BTreeSet<String> = BTreeSet::new();
1373
                for n in c
1374
                    .tree_ops
1375
                    .iter()
1376
                    .map(|o| o.node())
1377
                    .chain(c.edits.iter().map(|e| e.node()))
1378
                {
1379
                    if let Some(e) = by_node.get(&n) {
1380
                        paths.insert(e.path.clone());
1381
                    }
1382
                }
1383
                candidates.push((c.change_id, *h, paths.into_iter().collect()));
1384
            }
1385
            for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? {
1386
                if snap.base.contains(&d) {
1387
                    deps.insert(d);
1388
                }
1389
            }
1390
        }
1391
        // A resolution depends on every change that contributed to the
1392
        // conflict (spec §8.1). Tree and opaque conflicts are resolved through
1393
        // ordinary operations whose references already cover the contributors.
1394
        deps.extend(snap.contributors.iter().copied());
1395
        for d in extra_deps {
1396
            if !snap.base.contains(d) {
1397
                return fail(format!(
1398
                    "--depends-on {d}: not in the working copy's base state"
1399
                ));
1400
            }
1401
            deps.insert(*d);
1402
        }
1403
        let mut change = Change {
1404
            version: crate::objects::CHANGE_VERSION,
1405
            change_id: if partial {
1406
                ChangeId::random()
1407
            } else {
1408
                snap.wc.change_id
1409
            },
1410
            deps: vec![],
1411
            author: self.author(),
1412
            hlc: Hlc::now(),
1413
            message: message.trim_end().to_string(),
1414
            tree_ops,
1415
            edits,
1416
            kind: if snap.resolves {
1417
                ChangeKind::Resolution
1418
            } else {
1419
                ChangeKind::Normal
1420
            },
1421
        };
1422
        if partial {
1423
            // Dependencies come from what the selected part references, not
1424
            // from the whole working copy.
1425
            deps = referenced_changes(&change, &snap.base);
1426
            for d in extra_deps {
1427
                deps.insert(*d);
1428
            }
1429
        }
1430
        change.deps = deps.into_iter().collect();
1431
        change.normalize();
1432
        let signing = crate::timing::phase("commit: sign and store");
1433
        let hash = self.store_signed(&change)?;
1434
        drop(signing);
1435
        let extending = crate::timing::phase("commit: extend the state");
1436
1437
        // New bookmark heads: maximal elements of old heads plus the new change.
1438
        // The working copy continues on its own base plus the new change, which
1439
        // is exactly what the files on disk contain; the bookmark may hold
1440
        // other heads (and conflicts) the working copy has not checked out.
1441
        let old_heads = self.heads_of(&op.view, &self.config.bookmark)?;
1442
        let old_base = snap.base.maximal(&snap.wc.deps);
1443
        let mut all: Vec<ChangeHash> = old_heads.clone();
1444
        all.extend(old_base.iter().copied());
1445
        all.push(hash);
1446
        // The snapshot already loaded the working copy's base; when that also
1447
        // holds every bookmark head, it is the state to extend.
1448
        let mut st = if old_heads.iter().all(|h| snap.base.contains(h)) {
1449
            snap.base
1450
        } else {
1451
            self.load_state(&all)?
1452
        };
1453
        st.add_hashed(hash, change)?;
1454
        let mut with_new = old_heads.clone();
1455
        with_new.push(hash);
1456
        let new_heads = st.maximal(&with_new);
1457
        // Automatic checkpoint once enough changes accumulated (spec §11).
1458
        let since = self
1459
            .latest_checkpoint(&self.config.bookmark)?
1460
            .map(|(_, cp)| cp.count)
1461
            .unwrap_or(0);
1462
        if self.config.checkpoint_interval > 0
1463
            && (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval
1464
        {
1465
            let cp_state = self.load_state_full(&new_heads)?;
1466
            self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?;
1467
        }
1468
        let mut wc_base = old_base.clone();
1469
        wc_base.push(hash);
1470
        let wc_base = st.maximal(&wc_base);
1471
        drop(extending);
1472
1473
        // The graph keys of the files as they now are, from the state the
1474
        // commit just extended: a file whose graph it built has none, and
1475
        // is read by the next snapshot; a key the next base does not share
1476
        // (the bookmark held other heads) is simply not trusted then.
1477
        let keying = crate::timing::phase("commit: graph keys of the files");
1478
        let committed_keys =
1479
            (!partial).then(|| self.committed_graph_keys(&st, &changed_paths, &snap.attributes));
1480
        drop(keying);
1481
        let recording = crate::timing::phase("commit: record");
1482
        let wc = self.new_working_copy(wc_base, "");
1483
        let wc_hash = self.store.put_change(&wc)?;
1484
        let mut view = op.view.clone();
1485
        view.bookmarks.insert(
1486
            self.config.bookmark.clone(),
1487
            self.store.put_state(&StateObject::new(new_heads))?,
1488
        );
1489
        view.workspaces.insert(self.workspace, wc_hash);
1490
        self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?;
1491
        drop(recording);
1492
        let rewriting = crate::timing::phase("commit: manifest");
1493
        if partial {
1494
            // The rest is rebuilt from disk by the next snapshot.
1495
            self.drop_manifest();
1496
        } else {
1497
            let conflicted = snap.base_snapshot.is_conflicted() && !snap.resolves;
1498
            // The files on disk are now exactly the new base's renderings:
1499
            // record their graph keys, so the next snapshot neither reads nor
1500
            // renders them (oxygen#35).
1501
            let keys = if conflicted {
1502
                HashMap::new()
1503
            } else {
1504
                committed_keys.unwrap_or_default()
1505
            };
1506
            match pending {
1507
                Some(m) => self.write_pending_manifest(m, conflicted, keys)?,
1508
                None => self.rewrite_manifest(conflicted, false, &keys)?,
1509
            }
1510
        }
1511
        drop(rewriting);
1512
        let packing = crate::timing::phase("commit: autopack");
1513
        self.maybe_autopack()?;
1514
        drop(packing);
1515
        let _hook = crate::timing::phase("commit: hook");
1516
        let _ = self.run_hook(
1517
            "post-commit",
1518
            &format!(
1519
                "{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}",
1520
                json_str(&snap.wc.change_id.to_hex()),
1521
                json_str(&hash.to_hex()),
1522
                json_str(message)
1523
            ),
1524
        );
1525
        Ok((hash, partial))
1526
    }
1527
}
1528
1529
/// What part of the working copy a commit takes (spec §12.3).
1530
#[derive(Clone, Debug)]
1531
pub enum Selection {
1532
    All,
1533
    /// Every operation and edit on nodes under these paths.
1534
    Paths(Vec<String>),
1535
    /// Hunk indices as `Repo::hunks` numbers them; dependent hunks are added.
1536
    Hunks(Vec<usize>),
1537
}
1538
1539
/// One selectable piece of the working copy: a tree operation or an edit.
1540
#[derive(Clone, Debug)]
1541
pub struct Hunk {
1542
    pub index: usize,
1543
    pub path: String,
1544
    /// Rendered like `oxy diff`.
1545
    pub text: String,
1546
}
1547
1548
/// The tree operations and edits on nodes under `paths`, plus the
1549
/// additions of any new parent directories they need.
1550
fn select_paths(
1551
    wc: &Change,
1552
    base_snapshot: &Snapshot,
1553
    paths: &[String],
1554
) -> (Vec<TreeOp>, Vec<Edit>) {
1555
    let selected = |p: &str| {
1556
        paths
1557
            .iter()
1558
            .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
1559
    };
1560
    let by_node = base_snapshot.tree.by_node();
1561
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
1562
    for op in &wc.tree_ops {
1563
        if let TreeOp::AddNode {
1564
            node,
1565
            parent,
1566
            name,
1567
            kind,
1568
            ..
1569
        } = op
1570
        {
1571
            new_names.insert(*node, (*parent, name.clone(), *kind));
1572
        }
1573
    }
1574
    let mut nodes: BTreeSet<NodeId> = BTreeSet::new();
1575
    for n in wc
1576
        .tree_ops
1577
        .iter()
1578
        .map(|o| o.node())
1579
        .chain(wc.edits.iter().map(|e| e.node()))
1580
    {
1581
        if path_of_node(&n, &by_node, &new_names).is_some_and(|p| selected(&p)) {
1582
            nodes.insert(n);
1583
        }
1584
    }
1585
    // The nodes shown as one with a selected node are retired with it.
1586
    for (dup, shown) in &base_snapshot.tree.unified {
1587
        if nodes.contains(shown) {
1588
            nodes.insert(*dup);
1589
        }
1590
    }
1591
    // New parents of selected new nodes come along.
1592
    let mut changed = true;
1593
    while changed {
1594
        changed = false;
1595
        for (n, (parent, _, _)) in &new_names {
1596
            if nodes.contains(n) && new_names.contains_key(parent) && nodes.insert(*parent) {
1597
                changed = true;
1598
            }
1599
        }
1600
    }
1601
    let ops = wc
1602
        .tree_ops
1603
        .iter()
1604
        .filter(|o| nodes.contains(&o.node()))
1605
        .cloned()
1606
        .collect();
1607
    let edits = wc
1608
        .edits
1609
        .iter()
1610
        .filter(|e| nodes.contains(&e.node()))
1611
        .cloned()
1612
        .collect();
1613
    (ops, edits)
1614
}
1615
1616
/// The hunks `indices` name plus whatever they refer to within the working
1617
/// copy (the parent directory of an added node, the insert that introduced
1618
/// an anchor line), renumbered so the result is a well-formed change.
1619
/// The nodes `unified` shows as one with another (a base's
1620
/// [`TreeView::unified`](crate::tree::TreeView::unified)) are retired with
1621
/// any hunk of the node shown.
1622
fn select_hunks(
1623
    wc: &Change,
1624
    indices: &[usize],
1625
    unified: &BTreeMap<NodeId, NodeId>,
1626
) -> (Vec<TreeOp>, Vec<Edit>) {
1627
    let n_ops = wc.tree_ops.len();
1628
    let mut ops_sel = vec![false; n_ops];
1629
    let mut edits_sel = vec![false; wc.edits.len()];
1630
    for &i in indices {
1631
        if i < n_ops {
1632
            ops_sel[i] = true;
1633
        } else if i - n_ops < wc.edits.len() {
1634
            edits_sel[i - n_ops] = true;
1635
        }
1636
    }
1637
    // Atom ranges each insert introduces, per node, in the working copy's numbering.
1638
    let mut per_node: HashMap<NodeId, u32> = HashMap::new();
1639
    let mut insert_ranges: Vec<Option<(NodeId, u32, u32)>> = Vec::new();
1640
    for e in &wc.edits {
1641
        insert_ranges.push(match e {
1642
            Edit::Insert { node, lines, .. } => {
1643
                let start = per_node.entry(*node).or_default();
1644
                let r = (*node, *start, *start + lines.len() as u32);
1645
                *start += lines.len() as u32;
1646
                Some(r)
1647
            }
1648
            _ => None,
1649
        });
1650
    }
1651
    let add_index: HashMap<NodeId, usize> = wc
1652
        .tree_ops
1653
        .iter()
1654
        .enumerate()
1655
        .filter_map(|(i, o)| {
1656
            if let TreeOp::AddNode { node, .. } = o {
1657
                Some((*node, i))
1658
            } else {
1659
                None
1660
            }
1661
        })
1662
        .collect();
1663
    let insert_for = |node: &NodeId, index: u32| -> Option<usize> {
1664
        insert_ranges
1665
            .iter()
1666
            .position(|r| matches!(r, Some((n, a, b)) if n == node && *a <= index && index < *b))
1667
    };
1668
    // Closure.
1669
    let mut changed = true;
1670
    while changed {
1671
        changed = false;
1672
        for i in 0..n_ops {
1673
            if !ops_sel[i] {
1674
                continue;
1675
            }
1676
            if let TreeOp::AddNode { node, parent, .. } = &wc.tree_ops[i] {
1677
                if let Some(&p) = add_index.get(parent) {
1678
                    if !ops_sel[p] {
1679
                        ops_sel[p] = true;
1680
                        changed = true;
1681
                    }
1682
                }
1683
                // A new node's content belongs with its addition: nobody
1684
                // wants an empty file committed apart from its lines.
1685
                for (j, e) in wc.edits.iter().enumerate() {
1686
                    if e.node() == *node && !edits_sel[j] {
1687
                        edits_sel[j] = true;
1688
                        changed = true;
1689
                    }
1690
                }
1691
            }
1692
        }
1693
        for j in 0..wc.edits.len() {
1694
            if !edits_sel[j] {
1695
                continue;
1696
            }
1697
            let e = &wc.edits[j];
1698
            if let Some(&a) = add_index.get(&e.node()) {
1699
                if !ops_sel[a] {
1700
                    ops_sel[a] = true;
1701
                    changed = true;
1702
                }
1703
            }
1704
            if let Edit::Insert {
1705
                node,
1706
                after,
1707
                before,
1708
                ..
1709
            } = e
1710
            {
1711
                for r in [after, before] {
1712
                    if let AtomRef::Atom { change, index } = r {
1713
                        if change.is_zero() {
1714
                            if let Some(k) = insert_for(node, *index) {
1715
                                if !edits_sel[k] {
1716
                                    edits_sel[k] = true;
1717
                                    changed = true;
1718
                                }
1719
                            }
1720
                        }
1721
                    }
1722
                }
1723
            }
1724
        }
1725
    }
1726
    // The retirement of a node shown as one with a selected node.
1727
    if !unified.is_empty() {
1728
        let chosen: HashSet<NodeId> = (0..n_ops)
1729
            .filter(|i| ops_sel[*i])
1730
            .map(|i| wc.tree_ops[i].node())
1731
            .chain(
1732
                (0..wc.edits.len())
1733
                    .filter(|j| edits_sel[*j])
1734
                    .map(|j| wc.edits[j].node()),
1735
            )
1736
            .collect();
1737
        for (i, o) in wc.tree_ops.iter().enumerate() {
1738
            if let TreeOp::RemoveNode { node, .. } = o {
1739
                if unified.get(node).is_some_and(|s| chosen.contains(s)) {
1740
                    ops_sel[i] = true;
1741
                }
1742
            }
1743
        }
1744
    }
1745
    // Renumber self-references: old atom index → new index among selected inserts.
1746
    let mut remap: HashMap<(NodeId, u32), u32> = HashMap::new();
1747
    let mut new_counts: HashMap<NodeId, u32> = HashMap::new();
1748
    for (j, r) in insert_ranges.iter().enumerate() {
1749
        if let (true, Some((node, a, b))) = (edits_sel[j], r) {
1750
            let next = new_counts.entry(*node).or_default();
1751
            for old in *a..*b {
1752
                remap.insert((*node, old), *next);
1753
                *next += 1;
1754
            }
1755
        }
1756
    }
1757
    let map_ref = |r: AtomRef, node: &NodeId| -> AtomRef {
1758
        match r {
1759
            AtomRef::Atom { change, index } if change.is_zero() => AtomRef::Atom {
1760
                change,
1761
                index: remap.get(&(*node, index)).copied().unwrap_or(index),
1762
            },
1763
            other => other,
1764
        }
1765
    };
1766
    let ops = wc
1767
        .tree_ops
1768
        .iter()
1769
        .enumerate()
1770
        .filter(|(i, _)| ops_sel[*i])
1771
        .map(|(_, o)| o.clone())
1772
        .collect();
1773
    let edits = wc
1774
        .edits
1775
        .iter()
1776
        .enumerate()
1777
        .filter(|(j, _)| edits_sel[*j])
1778
        .map(|(_, e)| match e {
1779
            Edit::Insert {
1780
                node,
1781
                after,
1782
                before,
1783
                lines,
1784
            } => Edit::Insert {
1785
                node: *node,
1786
                after: map_ref(*after, node),
1787
                before: map_ref(*before, node),
1788
                lines: lines.clone(),
1789
            },
1790
            other => other.clone(),
1791
        })
1792
        .collect();
1793
    (ops, edits)
1794
}
1795
1796
/// Render each tree operation and edit of `change` on its own, numbered.
1797
pub fn render_hunks(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<Hunk> {
1798
    let by_node = base_snapshot.tree.by_node();
1799
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
1800
    for op in &change.tree_ops {
1801
        if let TreeOp::AddNode {
1802
            node,
1803
            parent,
1804
            name,
1805
            kind,
1806
            ..
1807
        } = op
1808
        {
1809
            new_names.insert(*node, (*parent, name.clone(), *kind));
1810
        }
1811
    }
1812
    let mut out = Vec::new();
1813
    for (i, op) in change.tree_ops.iter().enumerate() {
1814
        // A retired duplicate goes with the node shown: no hunk of its own.
1815
        if is_unified_retirement(op, base_snapshot) {
1816
            continue;
1817
        }
1818
        let one = Change {
1819
            tree_ops: vec![op.clone()],
1820
            edits: vec![],
1821
            ..change.clone()
1822
        };
1823
        let path = path_of_node(&op.node(), &by_node, &new_names).unwrap_or_default();
1824
        out.push(Hunk {
1825
            index: i,
1826
            path,
1827
            text: render_change(&one, base, base_snapshot),
1828
        });
1829
    }
1830
    for (j, e) in change.edits.iter().enumerate() {
1831
        let one = Change {
1832
            tree_ops: vec![],
1833
            edits: vec![e.clone()],
1834
            ..change.clone()
1835
        };
1836
        let path = path_of_node(&e.node(), &by_node, &new_names).unwrap_or_default();
1837
        out.push(Hunk {
1838
            index: change.tree_ops.len() + j,
1839
            path,
1840
            text: render_change(&one, base, base_snapshot),
1841
        });
1842
    }
1843
    out
1844
}
1845
1846
/// One file's part of a diff, for `oxy diff --json`.
1847
#[derive(Clone, Debug)]
1848
pub struct FileDiff {
1849
    pub path: String,
1850
    /// `added`, `removed`, `modified`, `renamed`, `mode`, `replaced`.
1851
    pub kind: String,
1852
    pub hunks: Vec<DiffHunk>,
1853
    /// For renames, the previous path.
1854
    pub from: Option<String>,
1855
}
1856
1857
/// Lines inserted after `base_line` (0 = before the first line) and lines
1858
/// removed, with their base line numbers.
1859
#[derive(Clone, Debug, Default)]
1860
pub struct DiffHunk {
1861
    pub base_line: u32,
1862
    pub added: Vec<Vec<u8>>,
1863
    pub removed: Vec<(u32, Vec<u8>)>,
1864
}
1865
1866
impl Repo {
1867
    /// The content of `path` in a state: `heads`, or the working copy's base.
1868
    pub fn file_at(&self, path: &str, heads: Option<&[ChangeHash]>) -> Result<Vec<u8>> {
1869
        // The state's history and tree, then the one file's graph: nothing
1870
        // else is built or rendered (oxygen#92).
1871
        // The working copy's base, and a state named by its heads (`-r
1872
        // main` on main), come from the workspace's state index.
1873
        let base = if self.is_bare() {
1874
            None
1875
        } else {
1876
            Some(self.working_copy()?.1.deps)
1877
        };
1878
        let same = |a: &[ChangeHash], b: &[ChangeHash]| {
1879
            let (mut a, mut b) = (a.to_vec(), b.to_vec());
1880
            a.sort();
1881
            b.sort();
1882
            a == b
1883
        };
1884
        let mut state = match (heads, base) {
1885
            (Some(h), Some(b)) if same(h, &b) => self.load_state_indexed(&b)?,
1886
            (Some(h), _) => self.load_state_history(h)?,
1887
            (None, Some(b)) => self.load_state_indexed(&b)?,
1888
            (None, None) => return fail("this is a bare repository: it has no working copy"),
1889
        };
1890
        let view = self.tree_view(&mut state)?;
1891
        let Some(node) = view.entries.get(path).map(|e| e.node) else {
1892
            return fail(format!("{path} is not in that state"));
1893
        };
1894
        self.load_graphs_of(&mut state, &[node])?;
1895
        let snapshot = state.materialize_paths(view, [path]);
1896
        let Some(f) = snapshot.files.get(path) else {
1897
            return fail(format!("{path} is not in that state"));
1898
        };
1899
        match &f.content {
1900
            FileContent::Dir => fail(format!("{path} is a directory")),
1901
            FileContent::Text { bytes, .. } => Ok(bytes.clone()),
1902
            FileContent::Opaque { blobs } => match blobs.first() {
1903
                Some(b) => self.read_blob(b),
1904
                None => Ok(vec![]),
1905
            },
1906
        }
1907
    }
1908
1909
    /// Restore `paths` (files or directories) to the working copy's base:
1910
    /// edits are undone, additions removed, removals put back. Returns the
1911
    /// number of paths touched.
1912
    pub fn discard(&self, paths: &[String]) -> Result<usize> {
1913
        let _wc = self.lock_working_copy()?;
1914
        let snap = self.snapshot_status("oxy discard")?;
1915
        let under = |p: &str| {
1916
            paths
1917
                .iter()
1918
                .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
1919
        };
1920
        let attrs = Repo::state_attributes(&snap.base_snapshot);
1921
        let mut n = 0;
1922
        for s in &snap.statuses {
1923
            if !under(&s.path) {
1924
                continue;
1925
            }
1926
            match snap.base_snapshot.files.get(&s.path) {
1927
                None => {
1928
                    // Added (or the new name of a rename): remove it.
1929
                    let full = self.root.join(&s.path);
1930
                    if full.is_dir() {
1931
                        let _ = fs::remove_dir_all(&full);
1932
                    } else {
1933
                        let _ = fs::remove_file(&full);
1934
                    }
1935
                    n += 1;
1936
                }
1937
                Some(f) => {
1938
                    match &f.content {
1939
                        FileContent::Dir => fs::create_dir_all(self.root.join(&s.path))?,
1940
                        FileContent::Text { bytes, .. } => {
1941
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&s.path).eol
1942
                                == Some(crate::attributes::Eol::Crlf)
1943
                            {
1944
                                crate::attributes::to_crlf(bytes).into()
1945
                            } else {
1946
                                bytes.as_slice().into()
1947
                            };
1948
                            self.write_file(&s.path, &on_disk, f.mode.exec)?;
1949
                        }
1950
                        FileContent::Opaque { blobs } => {
1951
                            let bytes = match blobs.first() {
1952
                                Some(b) => self.read_blob(b)?,
1953
                                None => vec![],
1954
                            };
1955
                            if f.kind == NodeKind::Symlink {
1956
                                self.write_symlink(&s.path, &bytes)?;
1957
                            } else if f.kind == NodeKind::File {
1958
                                self.write_file(&s.path, &bytes, f.mode.exec)?;
1959
                            }
1960
                        }
1961
                    }
1962
                    n += 1;
1963
                }
1964
            }
1965
        }
1966
        // A rename's old name is not in the statuses; put it back too.
1967
        for (p, f) in &snap.base_snapshot.files {
1968
            if under(p)
1969
                && !self.root.join(p).exists()
1970
                && f.kind != NodeKind::Dir
1971
                && f.kind != NodeKind::Subrepo
1972
                && !snap.statuses.iter().any(|s| s.path == *p)
1973
            {
1974
                let bytes = match &f.content {
1975
                    FileContent::Text { bytes, .. } => bytes.clone(),
1976
                    FileContent::Opaque { blobs } => blobs
1977
                        .first()
1978
                        .map(|b| self.read_blob(b))
1979
                        .transpose()?
1980
                        .unwrap_or_default(),
1981
                    FileContent::Dir => continue,
1982
                };
1983
                if f.kind == NodeKind::Symlink {
1984
                    self.write_symlink(p, &bytes)?
1985
                } else {
1986
                    self.write_file(p, &bytes, f.mode.exec)?
1987
                }
1988
                n += 1;
1989
            }
1990
        }
1991
        self.drop_manifest();
1992
        Ok(n)
1993
    }
1994
1995
    /// Undo the working-copy hunks `indices` (as `hunks` numbers them) on
1996
    /// disk, keeping the others: the files are rewritten from the base plus
1997
    /// the remaining hunks.
1998
    pub fn discard_hunks(&self, indices: &[usize]) -> Result<usize> {
1999
        let _wc = self.lock_working_copy()?;
2000
        let (snap, hunks) = self.hunks()?;
2001
        let keep: Vec<usize> = hunks
2002
            .iter()
2003
            .map(|h| h.index)
2004
            .filter(|i| !indices.contains(i))
2005
            .collect();
2006
        let (ops, edits) = select_hunks(&snap.wc, &keep, &snap.base_snapshot.tree.unified);
2007
        let dropped_nodes: BTreeSet<NodeId> = hunks
2008
            .iter()
2009
            .filter(|h| indices.contains(&h.index))
2010
            .map(|h| {
2011
                if h.index < snap.wc.tree_ops.len() {
2012
                    snap.wc.tree_ops[h.index].node()
2013
                } else {
2014
                    snap.wc.edits[h.index - snap.wc.tree_ops.len()].node()
2015
                }
2016
            })
2017
            .collect();
2018
        let mut partial = snap.wc.clone();
2019
        partial.tree_ops = ops;
2020
        partial.edits = edits;
2021
        partial.normalize();
2022
        let mut st = snap.base.clone();
2023
        st.add_unchecked(partial);
2024
        // Every file of the target is rendered: build the graphs the
2025
        // partial snapshot left out.
2026
        self.load_all_graphs(&mut st)?;
2027
        let target = st.materialize();
2028
        let by_node_target = target.tree.by_node();
2029
        let by_node_base = snap.base_snapshot.tree.by_node();
2030
        let attrs = Repo::state_attributes(&target);
2031
        let mut n = 0;
2032
        for node in dropped_nodes {
2033
            match by_node_target.get(&node) {
2034
                Some(e) => {
2035
                    let f = &target.files[&e.path];
2036
                    match &f.content {
2037
                        FileContent::Dir => {}
2038
                        FileContent::Text { bytes, .. } => {
2039
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&e.path).eol
2040
                                == Some(crate::attributes::Eol::Crlf)
2041
                            {
2042
                                crate::attributes::to_crlf(bytes).into()
2043
                            } else {
2044
                                bytes.as_slice().into()
2045
                            };
2046
                            self.write_file(&e.path, &on_disk, f.mode.exec)?;
2047
                        }
2048
                        FileContent::Opaque { blobs } => {
2049
                            let bytes = blobs
2050
                                .first()
2051
                                .map(|b| self.read_blob(b))
2052
                                .transpose()?
2053
                                .unwrap_or_default();
2054
                            if f.kind == NodeKind::Symlink {
2055
                                self.write_symlink(&e.path, &bytes)?
2056
                            } else {
2057
                                self.write_file(&e.path, &bytes, f.mode.exec)?
2058
                            }
2059
                        }
2060
                    }
2061
                    // A discarded rename: the new name goes.
2062
                    if let Some(old_path) = snap
2063
                        .statuses
2064
                        .iter()
2065
                        .find(|s| {
2066
                            s.status == ChangeStatus::Renamed && !target.files.contains_key(&s.path)
2067
                        })
2068
                        .map(|s| s.path.clone())
2069
                    {
2070
                        let _ = fs::remove_file(self.root.join(old_path));
2071
                    }
2072
                    n += 1;
2073
                }
2074
                None => {
2075
                    // The node exists in neither target nor base with a live path: an addition to remove.
2076
                    if let Some(added) = snap.statuses.iter().find(|s| {
2077
                        s.status == ChangeStatus::Added
2078
                            && !target.files.contains_key(&s.path)
2079
                            && !by_node_base.contains_key(&node)
2080
                    }) {
2081
                        let full = self.root.join(&added.path);
2082
                        if full.is_dir() {
2083
                            let _ = fs::remove_dir_all(&full);
2084
                        } else {
2085
                            let _ = fs::remove_file(&full);
2086
                        }
2087
                        n += 1;
2088
                    }
2089
                }
2090
            }
2091
        }
2092
        self.drop_manifest();
2093
        Ok(n)
2094
    }
2095
2096
    /// The diff of the working copy (`None`) or of a change, per file with
2097
    /// line-numbered hunks (spec: `oxy diff --json`).
2098
    pub fn diff_files(&self, change: Option<&ChangeHash>) -> Result<Vec<FileDiff>> {
2099
        let (c, base, base_snapshot) = match change {
2100
            Some(h) => {
2101
                let c = self.store.get_change(h)?;
2102
                let (base, snapshot) = self.change_base(&c)?;
2103
                (c, base, snapshot)
2104
            }
2105
            None => match self.working_copy_with_base_unless_unchanged()? {
2106
                Some(all) => all,
2107
                None => return Ok(Vec::new()),
2108
            },
2109
        };
2110
        Ok(diff_files_of(&c, &base, &base_snapshot))
2111
    }
2112
2113
    /// The snapshot a command would take, without taking it, when the
2114
    /// status cache holds: every file is as the last snapshot left it, so
2115
    /// that snapshot's working-copy change is the answer. Its base comes
2116
    /// from the state index, the `.oxyattributes` files from disk. `None`
2117
    /// when a snapshot must be taken, and when there is nothing to commit
2118
    /// (the snapshot then says so). A conflicted base never gets here: the
2119
    /// cache does not hold for one.
2120
    pub(crate) fn snapshot_from_cache(&self) -> Result<Option<SnapshotResult>> {
2121
        let Some(statuses) = self.status_by_manifest()? else {
2122
            return Ok(None);
2123
        };
2124
        if statuses.is_empty() {
2125
            return Ok(None);
2126
        }
2127
        let _t = crate::timing::phase("snapshot: from the status cache");
2128
        let (wc_hash, wc) = self.working_copy()?;
2129
        let base = self.load_state_indexed(&wc.deps)?;
2130
        let (base, base_snapshot) = self.render_base_for(&wc, base)?;
2131
        let attribute_files: Vec<(String, Vec<u8>)> = match self.read_manifest()? {
2132
            Some((_, _, m)) => m
2133
                .entries
2134
                .keys()
2135
                .filter(|p| is_attributes(p))
2136
                .filter_map(|p| fs::read(self.root.join(p)).ok().map(|b| (p.clone(), b)))
2137
                .collect(),
2138
            None => Vec::new(),
2139
        };
2140
        let attributes = crate::attributes::Attributes::from_files(
2141
            attribute_files
2142
                .iter()
2143
                .map(|(p, b)| (p.as_str(), b.as_slice())),
2144
        );
2145
        Ok(Some(SnapshotResult {
2146
            wc_hash,
2147
            wc,
2148
            base,
2149
            base_snapshot,
2150
            statuses,
2151
            changed: false,
2152
            resolves: false,
2153
            contributors: Vec::new(),
2154
            attributes,
2155
            untracked_nested: self.untracked_nested.borrow().clone(),
2156
            manifest: None,
2157
        }))
2158
    }
2159
2160
    /// The working-copy change with its base state and a snapshot of the
2161
    /// base, for showing what the working copy changes ([`render_change`],
2162
    /// [`diff_files_of`]). While the status cache holds (every file as the
2163
    /// last snapshot left it), the stored working-copy change is the
2164
    /// answer, no snapshot is taken, and the base's snapshot holds its tree
2165
    /// but no file; otherwise a snapshot makes all three.
2166
    pub fn working_copy_with_base(&self) -> Result<(Change, State, Snapshot)> {
2167
        match self.working_copy_with_base_unless_unchanged()? {
2168
            Some(all) => Ok(all),
2169
            None => {
2170
                let (_, wc) = self.working_copy()?;
2171
                let base = self.load_state_indexed(&wc.deps)?;
2172
                let (base, snapshot) = self.tree_base_for(&wc, base)?;
2173
                Ok((wc, base, snapshot))
2174
            }
2175
        }
2176
    }
2177
2178
    /// [`Repo::working_copy_with_base`], or `None` when the status cache
2179
    /// holds and the working-copy change changes nothing: its diff is
2180
    /// empty, and the base is not loaded for it (oxygen#94).
2181
    pub fn working_copy_with_base_unless_unchanged(
2182
        &self,
2183
    ) -> Result<Option<(Change, State, Snapshot)>> {
2184
        if self.status_by_manifest()?.is_some() {
2185
            let (_, wc) = self.working_copy()?;
2186
            if wc.tree_ops.is_empty() && wc.edits.is_empty() {
2187
                return Ok(None);
2188
            }
2189
            let base = self.load_state_indexed(&wc.deps)?;
2190
            let (base, snapshot) = self.tree_base_for(&wc, base)?;
2191
            return Ok(Some((wc, base, snapshot)));
2192
        }
2193
        let snap = self.snapshot_status("oxy diff")?;
2194
        Ok(Some((snap.wc, snap.base, snap.base_snapshot)))
2195
    }
2196
2197
    /// The log restricted to changes touching `path` (its node, so renames
2198
    /// are followed), on the current bookmark.
2199
    pub fn log_for_path(&self, path: &str) -> Result<Vec<LogEntry>> {
2200
        let heads = self.bookmark_heads()?;
2201
        if self.is_bare() {
2202
            return self.log_for_path_in(path, &heads);
2203
        }
2204
        let entries = self.log()?;
2205
        let (_, wc) = self.working_copy()?;
2206
        // The path's node from the trees alone: the base's (from the
2207
        // workspace's state index), else the bookmark's. Nothing is
2208
        // rendered.
2209
        let node_in = |mut state: State| -> Result<Option<HashSet<NodeId>>> {
2210
            Ok(nodes_at(&self.tree_view(&mut state)?, path))
2211
        };
2212
        let nodes = match node_in(self.load_state_indexed(&wc.deps)?)? {
2213
            Some(n) => Some(n),
2214
            None => node_in(self.load_state_history(&heads)?)?,
2215
        };
2216
        let Some(nodes) = nodes else {
2217
            return fail(format!("{path} is not tracked"));
2218
        };
2219
        Ok(self.entries_touching_up_to(entries, nodes, None))
2220
    }
2221
2222
    /// The log of the state with these heads restricted to changes touching
2223
    /// `path` (its node, so renames are followed).
2224
    pub fn log_for_path_in(&self, path: &str, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> {
2225
        self.log_for_path_of(&self.load_state_history(heads)?, path, None)
2226
    }
2227
2228
    /// The changes of a loaded state touching `path`, newest first, at most
2229
    /// `limit` of them. The path is found in the state's tree alone, so no
2230
    /// file is rendered, and the walk stops once `limit` are found.
2231
    pub fn log_for_path_of(
2232
        &self,
2233
        state: &State,
2234
        path: &str,
2235
        limit: Option<usize>,
2236
    ) -> Result<Vec<LogEntry>> {
2237
        let Some(nodes) = nodes_at(&state.view(), path) else {
2238
            return fail(format!("{path} is not in that state"));
2239
        };
2240
        Ok(self.entries_touching_up_to(self.log_of(state), nodes, limit))
2241
    }
2242
2243
    fn entries_touching_up_to(
2244
        &self,
2245
        entries: Vec<LogEntry>,
2246
        wanted: HashSet<NodeId>,
2247
        limit: Option<usize>,
2248
    ) -> Vec<LogEntry> {
2249
        let mut touched = self.touched();
2250
        if limit.is_none() {
2251
            let hashes: Vec<ChangeHash> = entries.iter().map(|e| e.hash).collect();
2252
            touched.fill(self, &hashes);
2253
        }
2254
        let mut out = Vec::new();
2255
        for e in entries {
2256
            if limit.is_some_and(|n| out.len() >= n) {
2257
                break;
2258
            }
2259
            if touched.touches(self, &e.hash, &wanted) {
2260
                out.push(e);
2261
            }
2262
        }
2263
        touched.save();
2264
        out
2265
    }
2266
}
2267
2268
/// The node at `path` in `view`, with the nodes it shows as one with it
2269
/// (identical additions, spec §9.2): the file's history is all of theirs.
2270
fn nodes_at(view: &crate::tree::TreeView, path: &str) -> Option<HashSet<NodeId>> {
2271
    let node = view.entries.get(path)?.node;
2272
    let mut out = HashSet::from([node]);
2273
    out.extend(
2274
        view.unified
2275
            .iter()
2276
            .filter(|(_, shown)| **shown == node)
2277
            .map(|(dup, _)| *dup),
2278
    );
2279
    Some(out)
2280
}
2281
2282
/// Per-file hunks of `change` against `base`.
2283
pub fn diff_files_of(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<FileDiff> {
2284
    let by_node = base_snapshot.tree.by_node();
2285
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
2286
    for op in &change.tree_ops {
2287
        if let TreeOp::AddNode {
2288
            node,
2289
            parent,
2290
            name,
2291
            kind,
2292
            ..
2293
        } = op
2294
        {
2295
            new_names.insert(*node, (*parent, name.clone(), *kind));
2296
        }
2297
    }
2298
    let path_of = |n: &NodeId| -> String {
2299
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
2300
    };
2301
    let mut files: BTreeMap<NodeId, FileDiff> = BTreeMap::new();
2302
    let mut order: Vec<NodeId> = Vec::new();
2303
    let mut entry = |files: &mut BTreeMap<NodeId, FileDiff>, n: NodeId, kind: &str| {
2304
        if let std::collections::btree_map::Entry::Vacant(v) = files.entry(n) {
2305
            order.push(n);
2306
            v.insert(FileDiff {
2307
                path: path_of(&n),
2308
                kind: kind.to_string(),
2309
                hunks: vec![],
2310
                from: None,
2311
            });
2312
        }
2313
    };
2314
    for op in change
2315
        .tree_ops
2316
        .iter()
2317
        .filter(|o| !is_unified_retirement(o, base_snapshot))
2318
    {
2319
        match op {
2320
            TreeOp::AddNode { node, kind, .. } => {
2321
                if *kind != NodeKind::Dir {
2322
                    entry(&mut files, *node, "added");
2323
                }
2324
            }
2325
            TreeOp::RemoveNode { node, .. } => {
2326
                entry(&mut files, *node, "removed");
2327
                if let Some(e) = by_node.get(node) {
2328
                    files.get_mut(node).unwrap().path = e.path.clone();
2329
                }
2330
            }
2331
            TreeOp::Move {
2332
                node, parent, name, ..
2333
            } => {
2334
                entry(&mut files, *node, "renamed");
2335
                let f = files.get_mut(node).unwrap();
2336
                f.from = by_node.get(node).map(|e| e.path.clone());
2337
                let dir = if parent.is_root() {
2338
                    String::new()
2339
                } else {
2340
                    path_of_node(parent, &by_node, &new_names).unwrap_or_default()
2341
                };
2342
                f.path = if dir.is_empty() {
2343
                    name.clone()
2344
                } else {
2345
                    format!("{dir}/{name}")
2346
                };
2347
            }
2348
            TreeOp::SetMode { node, .. } => entry(&mut files, *node, "mode"),
2349
        }
2350
    }
2351
    // Base line numbers per node, 1-based.
2352
    let mut line_of: HashMap<NodeId, HashMap<(ChangeHash, u32), u32>> = HashMap::new();
2353
    let mut lines_for = |n: &NodeId| -> HashMap<(ChangeHash, u32), u32> {
2354
        line_of
2355
            .entry(*n)
2356
            .or_insert_with(|| {
2357
                base.live_lines(n)
2358
                    .into_iter()
2359
                    .enumerate()
2360
                    .map(|(i, (k, _))| (k, i as u32 + 1))
2361
                    .collect()
2362
            })
2363
            .clone()
2364
    };
2365
    for e in &change.edits {
2366
        let n = e.node();
2367
        let is_new = new_names.contains_key(&n);
2368
        entry(&mut files, n, if is_new { "added" } else { "modified" });
2369
        let f = files.get_mut(&n).unwrap();
2370
        match e {
2371
            Edit::Insert { after, lines, .. } => {
2372
                let map = lines_for(&n);
2373
                let base_line = match after {
2374
                    AtomRef::Start => 0,
2375
                    AtomRef::End => map.len() as u32,
2376
                    AtomRef::Atom { change, index } if change.is_zero() => {
2377
                        f.hunks.last().map(|h| h.base_line).unwrap_or(0)
2378
                    }
2379
                    AtomRef::Atom { change, index } => {
2380
                        map.get(&(*change, *index)).copied().unwrap_or(0)
2381
                    }
2382
                };
2383
                f.hunks.push(DiffHunk {
2384
                    base_line,
2385
                    added: lines.to_vec(),
2386
                    removed: vec![],
2387
                });
2388
            }
2389
            Edit::Delete { atoms, .. } => {
2390
                let map = lines_for(&n);
2391
                let mut removed = Vec::new();
2392
                for r in atoms {
2393
                    for i in r.from..r.to {
2394
                        let line = map.get(&(r.change, i)).copied().unwrap_or(0);
2395
                        let text = base
2396
                            .file(&n)
2397
                            .and_then(|g| g.content(&(r.change, i)))
2398
                            .unwrap_or(b"")
2399
                            .to_vec();
2400
                        removed.push((line, text));
2401
                    }
2402
                }
2403
                let base_line = removed
2404
                    .first()
2405
                    .map(|(l, _)| l.saturating_sub(1))
2406
                    .unwrap_or(0);
2407
                f.hunks.push(DiffHunk {
2408
                    base_line,
2409
                    added: vec![],
2410
                    removed,
2411
                });
2412
            }
2413
            Edit::Replace { .. } => {
2414
                if !is_new {
2415
                    f.kind = "replaced".into();
2416
                }
2417
            }
2418
        }
2419
    }
2420
    order.into_iter().filter_map(|n| files.remove(&n)).collect()
2421
}
2422
2423
/// Builds the tree ops and edits of a working-copy change against a base.
2424
/// `BLAKE3("oxy node 1" 0 seed kind path)`, truncated: a node identifier
2425
/// that the same seed and path always give (never the root's).
2426
fn seeded_node(seed: &[u8; 32], path: &str, kind: NodeKind) -> NodeId {
2427
    let mut h = blake3::Hasher::new();
2428
    h.update(b"oxy node 1\0");
2429
    h.update(seed);
2430
    h.update(&[match kind {
2431
        NodeKind::File => 1,
2432
        NodeKind::Dir => 2,
2433
        NodeKind::Symlink => 3,
2434
        NodeKind::Subrepo => 4,
2435
    }]);
2436
    h.update(path.as_bytes());
2437
    let mut id = [0u8; 16];
2438
    id.copy_from_slice(&h.finalize().as_bytes()[..16]);
2439
    if id == [0; 16] {
2440
        id[15] = 1;
2441
    }
2442
    NodeId(id)
2443
}
2444
2445
struct ChangeBuilder<'a> {
2446
    base: &'a State,
2447
    base_snapshot: &'a Snapshot,
2448
    tree_ops: Vec<TreeOp>,
2449
    edits: Vec<Edit>,
2450
    statuses: Vec<PathStatus>,
2451
    dir_nodes: HashMap<String, NodeId>,
2452
    reuse: HashMap<String, (NodeId, NodeKind)>,
2453
    /// Changes that contributed to conflicts this working copy resolves.
2454
    contributors: BTreeSet<ChangeHash>,
2455
    resolves: bool,
2456
    attrs: crate::attributes::Attributes,
2457
    blobs: HashMap<String, ObjectRef>,
2458
    /// Opaque files whose replace conflict the content settles.
2459
    resolve: HashSet<String>,
2460
    /// When set, new nodes' identifiers derive from it and their paths.
2461
    node_seed: Option<[u8; 32]>,
2462
    /// For a Git import, the base's dead directories by where they were
2463
    /// (see `ensure_dir`), worked out when first needed.
2464
    dead_dirs: Option<HashMap<(NodeId, String), (NodeId, crate::tree::OpKey)>>,
2465
    /// Text edits of edited files, worked out in parallel beforehand
2466
    /// (`diff_in_parallel`); a file not here is diffed when it is updated.
2467
    diffs: HashMap<String, Vec<Edit>>,
2468
}
2469
2470
impl<'a> ChangeBuilder<'a> {
2471
    fn new(
2472
        base: &'a State,
2473
        base_snapshot: &'a Snapshot,
2474
        reuse: HashMap<String, (NodeId, NodeKind)>,
2475
    ) -> Self {
2476
        let mut dir_nodes: HashMap<String, NodeId> = HashMap::new();
2477
        dir_nodes.insert(String::new(), NodeId::ROOT);
2478
        for (p, e) in base_snapshot.tree.entries.iter() {
2479
            if e.kind == NodeKind::Dir {
2480
                dir_nodes.insert(p.clone(), e.node);
2481
            }
2482
        }
2483
        ChangeBuilder {
2484
            base,
2485
            base_snapshot,
2486
            tree_ops: vec![],
2487
            edits: vec![],
2488
            statuses: vec![],
2489
            dir_nodes,
2490
            reuse,
2491
            contributors: BTreeSet::new(),
2492
            resolves: false,
2493
            attrs: crate::attributes::Attributes::default(),
2494
            blobs: HashMap::new(),
2495
            resolve: HashSet::new(),
2496
            node_seed: None,
2497
            dead_dirs: None,
2498
            diffs: HashMap::new(),
2499
        }
2500
    }
2501
2502
    /// Diff the edited text files among `entries` against their base
2503
    /// renderings at once, on every core, for `update_path` to take: the
2504
    /// same diff it would make, only not one file after another.
2505
    fn diff_in_parallel(&mut self, entries: &BTreeMap<String, FsEntry>) {
2506
        use rayon::prelude::*;
2507
        let files = &self.base_snapshot.files;
2508
        let edited: Vec<(&String, &FsEntry, &crate::state::FileState)> = entries
2509
            .iter()
2510
            .filter_map(|(p, e)| {
2511
                let x = files.get(p)?;
2512
                (x.kind == e.kind && !e.unchanged && !self.opaque_for(p, e)).then_some((p, e, x))
2513
            })
2514
            .collect();
2515
        // Below this, starting the threads costs more than it saves.
2516
        if edited.len() < 16 {
2517
            return;
2518
        }
2519
        let this = &*self;
2520
        let attrs = &this.attrs;
2521
        let diffs: Vec<(String, Vec<Edit>)> = edited
2522
            .par_iter()
2523
            .filter_map(|(p, e, x)| {
2524
                let FileContent::Text {
2525
                    bytes,
2526
                    materialized,
2527
                } = &x.content
2528
                else {
2529
                    return None;
2530
                };
2531
                if materialized.is_conflicted() {
2532
                    return None;
2533
                }
2534
                let disk_text = if attrs.lookup(p).eol.is_some() {
2535
                    std::sync::Arc::new(crate::attributes::normalize_eol(&e.content))
2536
                } else {
2537
                    e.content.clone()
2538
                };
2539
                if *bytes == *disk_text {
2540
                    return None;
2541
                }
2542
                let keys = materialized.live_atoms();
2543
                let lines = crate::diff::split_lines(bytes);
2544
                (keys.len() == lines.len()).then(|| {
2545
                    let edits = crate::diff::text_edits_of(x.node, &keys, &lines, &disk_text);
2546
                    ((*p).clone(), edits)
2547
                })
2548
            })
2549
            .collect();
2550
        self.diffs = diffs.into_iter().collect();
2551
    }
2552
2553
    fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self {
2554
        self.attrs = attrs;
2555
        self
2556
    }
2557
2558
    /// Whether a file is opaque: forced by `binary`, forced text by `text`
2559
    /// only when it has no NUL bytes, else sniffed.
2560
    /// The blob for an opaque entry: the cached one, else stored now.
2561
    fn blob_of(
2562
        &mut self,
2563
        repo: &Repo,
2564
        path: &str,
2565
        e: &FsEntry,
2566
        nocompress: bool,
2567
    ) -> Result<ObjectRef> {
2568
        let b = match e.blob {
2569
            Some(b) => b,
2570
            None => repo.store_blob_for(&e.content, nocompress)?,
2571
        };
2572
        self.blobs.insert(path.to_string(), b);
2573
        Ok(b)
2574
    }
2575
2576
    fn opaque_for(&self, path: &str, e: &FsEntry) -> bool {
2577
        if e.blob.is_some() {
2578
            return true;
2579
        }
2580
        if e.kind == NodeKind::Symlink || e.kind == NodeKind::Subrepo {
2581
            return true;
2582
        }
2583
        match self.attrs.lookup(path).binary {
2584
            Some(true) => true,
2585
            Some(false) => is_binary(&e.content),
2586
            None => is_binary(&e.content),
2587
        }
2588
    }
2589
2590
    /// Text content as stored: `\r\n` normalized for `eol=` files.
2591
    fn stored_text(
2592
        &self,
2593
        path: &str,
2594
        content: &std::sync::Arc<Vec<u8>>,
2595
    ) -> std::sync::Arc<Vec<u8>> {
2596
        if self.attrs.lookup(path).eol.is_some() {
2597
            std::sync::Arc::new(crate::attributes::normalize_eol(content))
2598
        } else {
2599
            content.clone()
2600
        }
2601
    }
2602
2603
    /// The identifier of a node added at `path`: reused when asked, derived
2604
    /// from the seed when there is one, random otherwise.
2605
    fn new_node(&self, path: &str, kind: NodeKind) -> NodeId {
2606
        if let Some(n) = self.reused_node(path, kind) {
2607
            return n;
2608
        }
2609
        match &self.node_seed {
2610
            Some(seed) => seeded_node(seed, path, kind),
2611
            None => NodeId::random(),
2612
        }
2613
    }
2614
2615
    fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> {
2616
        self.reuse
2617
            .get(path)
2618
            .filter(|(_, k)| *k == kind)
2619
            .map(|(n, _)| *n)
2620
    }
2621
2622
    /// A dead directory last placed at (`parent`, `name`) in the base.
2623
    fn revivable_dir(
2624
        &mut self,
2625
        parent: NodeId,
2626
        name: &str,
2627
    ) -> Option<(NodeId, crate::tree::OpKey)> {
2628
        let base = self.base;
2629
        self.dead_dirs
2630
            .get_or_insert_with(|| base.tree().dead_dirs())
2631
            .get(&(parent, name.to_string()))
2632
            .copied()
2633
    }
2634
2635
    fn ensure_dir(&mut self, path: &str) -> NodeId {
2636
        if let Some(n) = self.dir_nodes.get(path) {
2637
            return *n;
2638
        }
2639
        let mut parent = NodeId::ROOT;
2640
        let mut prefix = String::new();
2641
        for comp in path.split('/') {
2642
            if !prefix.is_empty() {
2643
                prefix.push('/');
2644
            }
2645
            prefix.push_str(comp);
2646
            let known = self.dir_nodes.get(&prefix).copied();
2647
            let revive = match known {
2648
                Some(_) => None,
2649
                None if self.node_seed.is_some() => self.revivable_dir(parent, comp),
2650
                None => None,
2651
            };
2652
            parent = match (known, revive) {
2653
                (Some(n), _) => n,
2654
                (None, Some((node, old))) => {
2655
                    // Git has no identity for a directory: one that comes
2656
                    // back where it was is the same directory, brought back
2657
                    // with a move, so lines of history that placed files in
2658
                    // it before and after agree (oxygen#30).
2659
                    self.tree_ops.push(TreeOp::Move {
2660
                        node,
2661
                        old: OpRef {
2662
                            change: old.0,
2663
                            index: old.1,
2664
                        },
2665
                        parent,
2666
                        name: comp.to_string(),
2667
                    });
2668
                    self.dir_nodes.insert(prefix.clone(), node);
2669
                    node
2670
                }
2671
                (None, None) => {
2672
                    let node = self.new_node(&prefix, NodeKind::Dir);
2673
                    self.tree_ops.push(TreeOp::AddNode {
2674
                        node,
2675
                        kind: NodeKind::Dir,
2676
                        mode: Mode::default(),
2677
                        parent,
2678
                        name: comp.to_string(),
2679
                    });
2680
                    self.dir_nodes.insert(prefix.clone(), node);
2681
                    node
2682
                }
2683
            };
2684
        }
2685
        parent
2686
    }
2687
2688
    fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> {
2689
        let (dir, name) = split_path(path);
2690
        let parent = self.ensure_dir(dir);
2691
        let node = self.new_node(path, e.kind);
2692
        let opaque = self.opaque_for(path, e);
2693
        let nocompress = self.attrs.lookup(path).nocompress;
2694
        self.tree_ops.push(TreeOp::AddNode {
2695
            node,
2696
            kind: e.kind,
2697
            mode: Mode {
2698
                exec: e.exec,
2699
                opaque,
2700
            },
2701
            parent,
2702
            name: name.to_string(),
2703
        });
2704
        if opaque {
2705
            let blob = self.blob_of(repo, path, e, nocompress)?;
2706
            self.edits.push(Edit::Replace {
2707
                node,
2708
                old: None,
2709
                new: Some(blob),
2710
            });
2711
        } else if !e.content.is_empty() {
2712
            let content = self.stored_text(path, &e.content);
2713
            self.edits.push(Edit::Insert {
2714
                node,
2715
                after: crate::objects::AtomRef::Start,
2716
                before: crate::objects::AtomRef::End,
2717
                lines: crate::lines::Lines::split_shared(content),
2718
            });
2719
        }
2720
        self.statuses.push(PathStatus {
2721
            path: path.to_string(),
2722
            status: ChangeStatus::Added,
2723
        });
2724
        Ok(())
2725
    }
2726
2727
    /// Move the node at `old` to `new` and apply `e`'s content to it.
2728
    fn move_path(
2729
        &mut self,
2730
        repo: &Repo,
2731
        old: &str,
2732
        new: &str,
2733
        e: &FsEntry,
2734
        expected: &crate::state::FileState,
2735
    ) -> Result<()> {
2736
        let placement = self.base_snapshot.tree.entries[old].placement;
2737
        let (dir, name) = split_path(new);
2738
        let parent = self.ensure_dir(dir);
2739
        self.tree_ops.push(TreeOp::Move {
2740
            node: expected.node,
2741
            old: op_ref(placement),
2742
            parent,
2743
            name: name.to_string(),
2744
        });
2745
        for k in self.same_place(&expected.node, placement) {
2746
            self.tree_ops.push(TreeOp::RemoveNode {
2747
                node: expected.node,
2748
                placement: op_ref(k),
2749
            });
2750
        }
2751
        self.update_path(repo, old, e, expected)?;
2752
        // update_path reports under the old path; the rename is what the user sees.
2753
        self.statuses.retain(|s| s.path != old);
2754
        self.statuses.push(PathStatus {
2755
            path: new.to_string(),
2756
            status: ChangeStatus::Renamed,
2757
        });
2758
        Ok(())
2759
    }
2760
2761
    /// For a Git import: a node the commit keeps is in one place, the one
2762
    /// its tree names, so any other live placement it has in the base goes;
2763
    /// and a node the tree cannot show at all (an orphan) goes too. Such a
2764
    /// placement is out of sight, under a directory another line of history
2765
    /// removed, where no diff of the tree would ever reach it (oxygen#30).
2766
    fn retire_stray_placements(&mut self) {
2767
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
2768
        let mut placed: BTreeSet<NodeId> = BTreeSet::new();
2769
        for op in &self.tree_ops {
2770
            match op {
2771
                TreeOp::Move { node, old, .. } => {
2772
                    retired.insert((old.change, old.index));
2773
                    placed.insert(*node);
2774
                }
2775
                TreeOp::RemoveNode { placement, .. } => {
2776
                    retired.insert((placement.change, placement.index));
2777
                }
2778
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
2779
            }
2780
        }
2781
        // Nodes kept where they were: what the base's tree view shows and
2782
        // this change neither moves nor removes.
2783
        let mut kept: HashMap<NodeId, (ChangeHash, u32)> = HashMap::new();
2784
        for e in self.base_snapshot.tree.entries.values() {
2785
            if !retired.contains(&e.placement) {
2786
                kept.insert(e.node, e.placement);
2787
            }
2788
        }
2789
        // Only a node with several live placements can have a stray one,
2790
        // and the tree view names each as a conflict.
2791
        let multi: BTreeSet<NodeId> = self
2792
            .base_snapshot
2793
            .tree
2794
            .conflicts
2795
            .iter()
2796
            .filter_map(|c| match c {
2797
                crate::tree::TreeConflict::MultiPlacement { node, .. } => Some(*node),
2798
                _ => None,
2799
            })
2800
            .collect();
2801
        let mut extra = Vec::new();
2802
        // A node whose only placements are under a directory another line
2803
        // removed (an orphan) is nowhere the tree can show, and Git's tree
2804
        // has no such path: it goes.
2805
        for c in &self.base_snapshot.tree.conflicts {
2806
            let crate::tree::TreeConflict::Orphan { node, .. } = c else {
2807
                continue;
2808
            };
2809
            if kept.contains_key(node) || placed.contains(node) {
2810
                continue;
2811
            }
2812
            for (k, _) in self.base.tree().live_placements(node) {
2813
                if retired.insert(k) {
2814
                    extra.push(TreeOp::RemoveNode {
2815
                        node: *node,
2816
                        placement: op_ref(k),
2817
                    });
2818
                }
2819
            }
2820
        }
2821
        for node in placed
2822
            .iter()
2823
            .chain(multi.iter().filter(|n| kept.contains_key(*n)))
2824
        {
2825
            for (k, _) in self.base.tree().live_placements(node) {
2826
                if retired.contains(&k) || kept.get(node) == Some(&k) {
2827
                    continue;
2828
                }
2829
                retired.insert(k);
2830
                extra.push(TreeOp::RemoveNode {
2831
                    node: *node,
2832
                    placement: op_ref(k),
2833
                });
2834
            }
2835
        }
2836
        if !extra.is_empty() {
2837
            self.resolves = true;
2838
            self.tree_ops.extend(extra);
2839
        }
2840
    }
2841
2842
    /// Nodes the base shows as one with another (identical additions at one
2843
    /// path, spec §9.2) are retired by the change that first touches the
2844
    /// node shown: edited, moved, removed or given a mode alone, it would
2845
    /// differ from them and the name conflict would come back. Nothing
2846
    /// else changes for the user, so no status reports it.
2847
    fn retire_unified(&mut self) {
2848
        let unified = &self.base_snapshot.tree.unified;
2849
        if unified.is_empty() {
2850
            return;
2851
        }
2852
        let touched: HashSet<NodeId> = self
2853
            .tree_ops
2854
            .iter()
2855
            .map(|o| o.node())
2856
            .chain(self.edits.iter().map(|e| e.node()))
2857
            .collect();
2858
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
2859
        for op in &self.tree_ops {
2860
            match op {
2861
                TreeOp::Move { old, .. } => {
2862
                    retired.insert((old.change, old.index));
2863
                }
2864
                TreeOp::RemoveNode { placement, .. } => {
2865
                    retired.insert((placement.change, placement.index));
2866
                }
2867
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
2868
            }
2869
        }
2870
        let mut extra = Vec::new();
2871
        for (dup, shown) in unified {
2872
            if !touched.contains(shown) {
2873
                continue;
2874
            }
2875
            for (k, _) in self.base.tree().live_placements(dup) {
2876
                if retired.insert(k) {
2877
                    extra.push(TreeOp::RemoveNode {
2878
                        node: *dup,
2879
                        placement: op_ref(k),
2880
                    });
2881
                }
2882
            }
2883
        }
2884
        self.tree_ops.extend(extra);
2885
    }
2886
2887
    /// The node's other live placements at the same place as `k`, the one
2888
    /// the tree shows: two lines of history that put a node back where it
2889
    /// was each leave a placement, and the tree shows them as one file. A
2890
    /// change that removes or moves it retires them together, or the copy
2891
    /// left behind outlives its directory (oxygen#88).
2892
    fn same_place(&self, node: &NodeId, k: (ChangeHash, u32)) -> Vec<(ChangeHash, u32)> {
2893
        let tree = self.base.tree();
2894
        let Some(shown) = tree.placement(&k) else {
2895
            return Vec::new();
2896
        };
2897
        tree.live_placements(node)
2898
            .into_iter()
2899
            .filter(|(o, p)| *o != k && p.parent == shown.parent && p.name == shown.name)
2900
            .map(|(o, _)| o)
2901
            .collect()
2902
    }
2903
2904
    fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) {
2905
        let entry = &self.base_snapshot.tree.entries[path];
2906
        let placement = entry.placement;
2907
        self.tree_ops.push(TreeOp::RemoveNode {
2908
            node: expected.node,
2909
            placement: op_ref(placement),
2910
        });
2911
        for k in self.same_place(&expected.node, placement) {
2912
            self.tree_ops.push(TreeOp::RemoveNode {
2913
                node: expected.node,
2914
                placement: op_ref(k),
2915
            });
2916
        }
2917
        if path.contains(".oxy-conflict-") || expected.is_conflicted() {
2918
            self.resolves = true;
2919
        }
2920
        self.statuses.push(PathStatus {
2921
            path: path.to_string(),
2922
            status: ChangeStatus::Removed,
2923
        });
2924
    }
2925
2926
    fn update_path(
2927
        &mut self,
2928
        repo: &Repo,
2929
        path: &str,
2930
        e: &FsEntry,
2931
        expected: &crate::state::FileState,
2932
    ) -> Result<()> {
2933
        if expected.kind != e.kind {
2934
            self.remove_path(path, expected);
2935
            self.statuses.pop();
2936
            self.add_path(repo, path, e)?;
2937
            self.statuses.pop();
2938
            self.statuses.push(PathStatus {
2939
                path: path.to_string(),
2940
                status: ChangeStatus::Modified,
2941
            });
2942
            return Ok(());
2943
        }
2944
        let entry = &self.base_snapshot.tree.entries[path];
2945
        let node = expected.node;
2946
        let mode_atom = entry.mode_atom.map(op_ref);
2947
        if e.unchanged {
2948
            // Known equal to the base's unconflicted rendering (spec
2949
            // §12.3); only the executable bit can differ.
2950
            if e.exec != expected.mode.exec {
2951
                if let Some(old) = mode_atom {
2952
                    self.tree_ops.push(TreeOp::SetMode {
2953
                        node,
2954
                        old,
2955
                        mode: Mode {
2956
                            exec: e.exec,
2957
                            opaque: false,
2958
                        },
2959
                    });
2960
                    self.statuses.push(PathStatus {
2961
                        path: path.to_string(),
2962
                        status: ChangeStatus::ModeChanged,
2963
                    });
2964
                }
2965
            }
2966
            return Ok(());
2967
        }
2968
        let mut status: Option<ChangeStatus> = None;
2969
        let wants_opaque = self.opaque_for(path, e);
2970
        let nocompress = self.attrs.lookup(path).nocompress;
2971
        let disk_text = self.stored_text(path, &e.content);
2972
        match &expected.content {
2973
            FileContent::Dir => {}
2974
            FileContent::Text {
2975
                bytes,
2976
                materialized,
2977
            } => {
2978
                if materialized.is_conflicted() {
2979
                    if bytes == &*disk_text {
2980
                        return Ok(());
2981
                    }
2982
                    let regions = self.base.conflict_regions(&node);
2983
                    let plain: Vec<&[u8]> =
2984
                        regions.plain.iter().map(|(_, l)| l.as_slice()).collect();
2985
                    if has_own_conflict_markers(&e.content, bytes, &plain) {
2986
                        self.statuses.push(PathStatus {
2987
                            path: path.to_string(),
2988
                            status: ChangeStatus::ConflictEdited,
2989
                        });
2990
                        return Ok(());
2991
                    }
2992
                    // Resolution (spec §9.6): diff against the unconflicted lines
2993
                    // with each region as a gap, then retire every region atom.
2994
                    // Whatever the author kept from a region is re-inserted as
2995
                    // new atoms, which leaves the graph totally ordered.
2996
                    let mut edits = text_edits(node, &regions.plain, &disk_text);
2997
                    if !regions.region_atoms.is_empty() {
2998
                        edits.insert(
2999
                            0,
3000
                            Edit::Delete {
3001
                                node,
3002
                                atoms: ranges(&regions.region_atoms),
3003
                            },
3004
                        );
3005
                    }
3006
                    self.edits.extend(edits);
3007
                    self.contributors.extend(regions.contributors);
3008
                    self.resolves = true;
3009
                    if e.exec != expected.mode.exec {
3010
                        if let Some(old) = mode_atom {
3011
                            self.tree_ops.push(TreeOp::SetMode {
3012
                                node,
3013
                                old,
3014
                                mode: Mode {
3015
                                    exec: e.exec,
3016
                                    opaque: false,
3017
                                },
3018
                            });
3019
                        }
3020
                    }
3021
                    self.statuses.push(PathStatus {
3022
                        path: path.to_string(),
3023
                        status: ChangeStatus::Resolved,
3024
                    });
3025
                    return Ok(());
3026
                }
3027
                if wants_opaque {
3028
                    // Text → opaque (spec §7.5), by content or by attribute.
3029
                    if let Some(old) = mode_atom {
3030
                        self.tree_ops.push(TreeOp::SetMode {
3031
                            node,
3032
                            old,
3033
                            mode: Mode {
3034
                                exec: e.exec,
3035
                                opaque: true,
3036
                            },
3037
                        });
3038
                    }
3039
                    let live: Vec<_> = repo_live_keys(self.base, &node);
3040
                    if !live.is_empty() {
3041
                        self.edits.push(Edit::Delete {
3042
                            node,
3043
                            atoms: ranges(&live),
3044
                        });
3045
                    }
3046
                    let blob = self.blob_of(repo, path, e, nocompress)?;
3047
                    self.edits.push(Edit::Replace {
3048
                        node,
3049
                        old: None,
3050
                        new: Some(blob),
3051
                    });
3052
                    status = Some(ChangeStatus::Modified);
3053
                } else if bytes != &*disk_text {
3054
                    let mut laps = crate::timing::Laps::start();
3055
                    // The base's rendering holds its live atoms in order,
3056
                    // one per line (it is not conflicted here).
3057
                    let edits = match self.diffs.remove(path) {
3058
                        Some(edits) => edits,
3059
                        None => {
3060
                            let keys = materialized.live_atoms();
3061
                            let lines = crate::diff::split_lines(bytes);
3062
                            if keys.len() == lines.len() {
3063
                                crate::diff::text_edits_of(node, &keys, &lines, &disk_text)
3064
                            } else {
3065
                                text_edits(node, &self.base.live_lines(&node), &disk_text)
3066
                            }
3067
                        }
3068
                    };
3069
                    laps.lap("build: text diff");
3070
                    if !edits.is_empty() {
3071
                        self.edits.extend(edits);
3072
                        status = Some(ChangeStatus::Modified);
3073
                    }
3074
                }
3075
                let switched_to_opaque = wants_opaque;
3076
                if !switched_to_opaque && e.exec != expected.mode.exec {
3077
                    if let Some(old) = mode_atom {
3078
                        self.tree_ops.push(TreeOp::SetMode {
3079
                            node,
3080
                            old,
3081
                            mode: Mode {
3082
                                exec: e.exec,
3083
                                opaque: false,
3084
                            },
3085
                        });
3086
                        status.get_or_insert(ChangeStatus::ModeChanged);
3087
                    }
3088
                }
3089
            }
3090
            FileContent::Opaque { blobs } => {
3091
                let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content));
3092
                if e.kind == NodeKind::File && wants_opaque {
3093
                    // Unchanged or replaced, the file stays opaque: remember its blob.
3094
                    self.blobs.insert(path.to_string(), new_ref);
3095
                }
3096
                if blobs.len() > 1 && self.resolve.contains(path) && e.kind == expected.kind {
3097
                    // Settle the replace conflict (spec §9.3): replace every
3098
                    // live candidate with the chosen blob, depending on each
3099
                    // change that made one live, so all are superseded.
3100
                    let chosen = self.blob_of(repo, path, e, nocompress)?;
3101
                    for b in blobs {
3102
                        self.edits.push(Edit::Replace {
3103
                            node,
3104
                            old: Some(*b),
3105
                            new: Some(chosen),
3106
                        });
3107
                    }
3108
                    self.contributors
3109
                        .extend(self.base.blob_contributors(&node, blobs));
3110
                    self.resolves = true;
3111
                    self.statuses.push(PathStatus {
3112
                        path: path.to_string(),
3113
                        status: ChangeStatus::Resolved,
3114
                    });
3115
                    return Ok(());
3116
                }
3117
                if blobs.len() > 1 {
3118
                    if !blobs.contains(&new_ref) {
3119
                        self.statuses.push(PathStatus {
3120
                            path: path.to_string(),
3121
                            status: ChangeStatus::ConflictEdited,
3122
                        });
3123
                    }
3124
                    return Ok(());
3125
                }
3126
                let current = blobs.first().copied();
3127
                if current != Some(new_ref) {
3128
                    if e.kind == NodeKind::File && !wants_opaque {
3129
                        // Opaque → text (spec §7.5).
3130
                        if let Some(old) = mode_atom {
3131
                            self.tree_ops.push(TreeOp::SetMode {
3132
                                node,
3133
                                old,
3134
                                mode: Mode {
3135
                                    exec: e.exec,
3136
                                    opaque: false,
3137
                                },
3138
                            });
3139
                        }
3140
                        self.edits.push(Edit::Replace {
3141
                            node,
3142
                            old: current,
3143
                            new: None,
3144
                        });
3145
                        if !e.content.is_empty() {
3146
                            self.edits.push(Edit::Insert {
3147
                                node,
3148
                                after: crate::objects::AtomRef::Start,
3149
                                before: crate::objects::AtomRef::End,
3150
                                lines: crate::lines::Lines::split(&e.content),
3151
                            });
3152
                        }
3153
                    } else {
3154
                        let blob = self.blob_of(repo, path, e, nocompress)?;
3155
                        self.edits.push(Edit::Replace {
3156
                            node,
3157
                            old: current,
3158
                            new: Some(blob),
3159
                        });
3160
                        // New contents and a new executable bit at once: the
3161
                        // bit was dropped (VS Code's inno_updater.exe,
3162
                        // 2018-02-28, lost +x for 49 commits; oxygen#87).
3163
                        if e.kind == NodeKind::File && e.exec != expected.mode.exec {
3164
                            if let Some(old) = mode_atom {
3165
                                self.tree_ops.push(TreeOp::SetMode {
3166
                                    node,
3167
                                    old,
3168
                                    mode: Mode {
3169
                                        exec: e.exec,
3170
                                        opaque: true,
3171
                                    },
3172
                                });
3173
                            }
3174
                        }
3175
                    }
3176
                    status = Some(ChangeStatus::Modified);
3177
                } else if e.kind == NodeKind::File && e.exec != expected.mode.exec {
3178
                    if let Some(old) = mode_atom {
3179
                        self.tree_ops.push(TreeOp::SetMode {
3180
                            node,
3181
                            old,
3182
                            mode: Mode {
3183
                                exec: e.exec,
3184
                                opaque: true,
3185
                            },
3186
                        });
3187
                        status = Some(ChangeStatus::ModeChanged);
3188
                    }
3189
                }
3190
            }
3191
        }
3192
        if let Some(s) = status {
3193
            self.statuses.push(PathStatus {
3194
                path: path.to_string(),
3195
                status: s,
3196
            });
3197
        }
3198
        Ok(())
3199
    }
3200
}
3201
3202
fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> {
3203
    base.live_lines(node).into_iter().map(|(k, _)| k).collect()
3204
}
3205
3206
/// The changes a change's scope references (spec §8.1): introducers of every
3207
/// atom, placement, and mode it touches, and of every node it operates on.
3208
pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> {
3209
    let mut out = BTreeSet::new();
3210
    let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| {
3211
        if !r.change.is_zero() {
3212
            out.insert(r.change);
3213
        }
3214
    };
3215
    let mut nodes: Vec<NodeId> = Vec::new();
3216
    for op in &change.tree_ops {
3217
        match op {
3218
            TreeOp::AddNode { parent, .. } => nodes.push(*parent),
3219
            TreeOp::RemoveNode { node, placement } => {
3220
                nodes.push(*node);
3221
                opref(placement, &mut out);
3222
            }
3223
            TreeOp::Move {
3224
                node, old, parent, ..
3225
            } => {
3226
                nodes.push(*node);
3227
                nodes.push(*parent);
3228
                opref(old, &mut out);
3229
            }
3230
            TreeOp::SetMode { node, old, .. } => {
3231
                nodes.push(*node);
3232
                opref(old, &mut out);
3233
            }
3234
        }
3235
    }
3236
    for e in &change.edits {
3237
        nodes.push(e.node());
3238
        match e {
3239
            Edit::Insert { after, before, .. } => {
3240
                for r in [after, before] {
3241
                    if let crate::objects::AtomRef::Atom { change, .. } = r {
3242
                        if !change.is_zero() {
3243
                            out.insert(*change);
3244
                        }
3245
                    }
3246
                }
3247
            }
3248
            Edit::Delete { atoms, .. } => {
3249
                // Deleting an atom also depends on whoever inserted the live
3250
                // atoms right after it: the author saw those insertions, and
3251
                // without the dependency spec §9.1 would call the deletion
3252
                // concurrent with them (a delete/insert conflict).
3253
                let graph = base.file(&e.node());
3254
                for r in atoms {
3255
                    if !r.change.is_zero() {
3256
                        out.insert(r.change);
3257
                    }
3258
                    if let Some(g) = graph {
3259
                        for i in r.from..r.to {
3260
                            out.extend(
3261
                                g.live_successor_changes(&(r.change, i))
3262
                                    .into_iter()
3263
                                    .filter(|c| !c.is_zero()),
3264
                            );
3265
                        }
3266
                    }
3267
                }
3268
            }
3269
            // Replacing a blob depends on the change that made it current,
3270
            // as deleting a line depends on the line's introducer.
3271
            Edit::Replace {
3272
                node,
3273
                old: Some(old),
3274
                ..
3275
            } => {
3276
                out.extend(
3277
                    base.live_replaces(node)
3278
                        .into_iter()
3279
                        .filter(|(c, b)| b == old && !c.is_zero())
3280
                        .map(|(c, _)| c),
3281
                );
3282
            }
3283
            Edit::Replace { .. } => {}
3284
        }
3285
    }
3286
    for n in nodes {
3287
        if let Some(h) = base.tree().introducer(&n) {
3288
            out.insert(h);
3289
        }
3290
    }
3291
    out
3292
}
3293
3294
/// Paths of the nodes a change adds on top of `base_snapshot`.
3295
pub fn added_node_paths(
3296
    change: &Change,
3297
    base_snapshot: &Snapshot,
3298
) -> HashMap<String, (NodeId, NodeKind)> {
3299
    let by_node = base_snapshot.tree.by_node();
3300
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
3301
    for op in &change.tree_ops {
3302
        if let TreeOp::AddNode {
3303
            node,
3304
            parent,
3305
            name,
3306
            kind,
3307
            ..
3308
        } = op
3309
        {
3310
            new_names.insert(*node, (*parent, name.clone(), *kind));
3311
        }
3312
    }
3313
    let mut out = HashMap::new();
3314
    for (node, (_, _, kind)) in &new_names {
3315
        if let Some(path) = path_of_node(node, &by_node, &new_names) {
3316
            out.insert(path, (*node, *kind));
3317
        }
3318
    }
3319
    out
3320
}
3321
3322
/// Whether `op` retires a node `base_snapshot` shows as one with another
3323
/// (spec §9.2): part of touching that node, never a removal the user made.
3324
fn is_unified_retirement(op: &TreeOp, base_snapshot: &Snapshot) -> bool {
3325
    matches!(op, TreeOp::RemoveNode { node, .. } if base_snapshot.tree.unified.contains_key(node))
3326
}
3327
3328
fn path_of_node(
3329
    n: &NodeId,
3330
    by_node: &HashMap<NodeId, &crate::tree::TreeEntry>,
3331
    new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>,
3332
) -> Option<String> {
3333
    if let Some(e) = by_node.get(n) {
3334
        return Some(e.path.clone());
3335
    }
3336
    let mut parts = Vec::new();
3337
    let mut cur = *n;
3338
    loop {
3339
        if cur.is_root() {
3340
            break;
3341
        }
3342
        if let Some(e) = by_node.get(&cur) {
3343
            if !e.path.is_empty() {
3344
                parts.push(e.path.clone());
3345
            }
3346
            break;
3347
        }
3348
        let (parent, name, _) = new_names.get(&cur)?;
3349
        parts.push(name.clone());
3350
        cur = *parent;
3351
    }
3352
    parts.reverse();
3353
    Some(parts.join("/"))
3354
}
3355
3356
/// Render a change's operations with line content, using `base` for deleted lines.
3357
pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String {
3358
    let by_node = base_snapshot.tree.by_node();
3359
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
3360
    for op in &change.tree_ops {
3361
        if let TreeOp::AddNode {
3362
            node,
3363
            parent,
3364
            name,
3365
            kind,
3366
            ..
3367
        } = op
3368
        {
3369
            new_names.insert(*node, (*parent, name.clone(), *kind));
3370
        }
3371
    }
3372
    let path_of = |n: &NodeId| -> String {
3373
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
3374
    };
3375
3376
    let mut out = String::new();
3377
    for op in change
3378
        .tree_ops
3379
        .iter()
3380
        .filter(|o| !is_unified_retirement(o, base_snapshot))
3381
    {
3382
        match op {
3383
            TreeOp::AddNode {
3384
                node, kind, mode, ..
3385
            } => {
3386
                out.push_str(&format!(
3387
                    "A {} ({:?}{}{})\n",
3388
                    path_of(node),
3389
                    kind,
3390
                    if mode.exec { ", exec" } else { "" },
3391
                    if mode.opaque { ", opaque" } else { "" }
3392
                ));
3393
            }
3394
            TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))),
3395
            TreeOp::Move {
3396
                node, parent, name, ..
3397
            } => {
3398
                let to = if parent.is_root() {
3399
                    name.clone()
3400
                } else {
3401
                    format!("{}/{name}", path_of(parent))
3402
                };
3403
                out.push_str(&format!("R {} -> {to}\n", path_of(node)))
3404
            }
3405
            TreeOp::SetMode { node, mode, .. } => {
3406
                out.push_str(&format!(
3407
                    "M {} mode: exec={} opaque={}\n",
3408
                    path_of(node),
3409
                    mode.exec,
3410
                    mode.opaque
3411
                ));
3412
            }
3413
        }
3414
    }
3415
    // Every other file the change edits is modified: the summary names
3416
    // every path it touches, not only those with a tree operation.
3417
    let with_op: HashSet<NodeId> = change.tree_ops.iter().map(|o| o.node()).collect();
3418
    let mut listed: HashSet<NodeId> = HashSet::new();
3419
    for e in &change.edits {
3420
        let n = e.node();
3421
        if !with_op.contains(&n) && listed.insert(n) {
3422
            out.push_str(&format!("M {}\n", path_of(&n)));
3423
        }
3424
    }
3425
    let mut last_node: Option<NodeId> = None;
3426
    for e in &change.edits {
3427
        let n = e.node();
3428
        if last_node != Some(n) {
3429
            out.push_str(&format!("--- {}\n", path_of(&n)));
3430
            last_node = Some(n);
3431
        }
3432
        match e {
3433
            Edit::Insert { lines, .. } => {
3434
                for l in lines.iter() {
3435
                    out.push('+');
3436
                    out.push_str(&String::from_utf8_lossy(l));
3437
                    if !l.ends_with(b"\n") {
3438
                        out.push('\n');
3439
                    }
3440
                }
3441
            }
3442
            Edit::Delete { atoms, .. } => {
3443
                for r in atoms {
3444
                    for i in r.from..r.to {
3445
                        let content = base
3446
                            .file(&n)
3447
                            .and_then(|g| g.content(&(r.change, i)))
3448
                            .unwrap_or(b"<unknown>\n");
3449
                        out.push('-');
3450
                        out.push_str(&String::from_utf8_lossy(content));
3451
                        if !content.ends_with(b"\n") {
3452
                            out.push('\n');
3453
                        }
3454
                    }
3455
                }
3456
            }
3457
            Edit::Replace { old, new, .. } => {
3458
                out.push_str(&format!(
3459
                    "  opaque content {} -> {}\n",
3460
                    old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()),
3461
                    new.map(|o| o.short(12)).unwrap_or_else(|| "none".into())
3462
                ));
3463
            }
3464
        }
3465
    }
3466
    out
3467
}
3468
3469
/// Pair removed files with added files of the same kind whose contents are
3470
/// at least `RENAME_THRESHOLD` percent alike, best matches first. Beyond
3471
/// `RENAME_LIMIT` candidate pairs only identical contents are paired.
3472
const RENAME_THRESHOLD: u32 = 50;
3473
const RENAME_LIMIT: usize = 10_000;
3474
3475
/// How a change is built beyond its entries: for a Git import, the seed
3476
/// its new nodes' identifiers derive from; and, when it is built from part
3477
/// of the tree, whether the new tree has anything under a directory.
3478
#[derive(Default)]
3479
struct Scope<'a> {
3480
    seed: Option<[u8; 32]>,
3481
    dir_needed: Option<&'a dyn Fn(&str) -> bool>,
3482
}
3483
3484
fn detect_renames(
3485
    base_snapshot: &Snapshot,
3486
    fs_entries: &BTreeMap<String, FsEntry>,
3487
    in_scope: impl Fn(&str) -> bool,
3488
) -> Vec<(String, String)> {
3489
    let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot
3490
        .files
3491
        .iter()
3492
        .filter(|(p, f)| {
3493
            f.kind != NodeKind::Dir
3494
                && !fs_entries.contains_key(*p)
3495
                && in_scope(p)
3496
                && !f.is_conflicted()
3497
        })
3498
        .collect();
3499
    let added: Vec<(&String, &FsEntry)> = fs_entries
3500
        .iter()
3501
        .filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p))
3502
        .collect();
3503
    if removed.is_empty() || added.is_empty() {
3504
        return vec![];
3505
    }
3506
    let exact_only = removed.len() * added.len() > RENAME_LIMIT;
3507
    let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> {
3508
        match &f.content {
3509
            FileContent::Text { bytes, .. } => Some(bytes.clone()),
3510
            _ => None,
3511
        }
3512
    };
3513
    let mut scored: Vec<(u32, usize, usize)> = Vec::new();
3514
    for (ri, (_, f)) in removed.iter().enumerate() {
3515
        let old = old_content(f);
3516
        let old_blob = match &f.content {
3517
            FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]),
3518
            _ => None,
3519
        };
3520
        for (ai, (_, e)) in added.iter().enumerate() {
3521
            if e.kind != f.kind {
3522
                continue;
3523
            }
3524
            let score = match (&old, old_blob) {
3525
                (Some(o), _) => {
3526
                    if exact_only {
3527
                        if *o == *e.content {
3528
                            100
3529
                        } else {
3530
                            0
3531
                        }
3532
                    } else {
3533
                        similarity(o, &e.content)
3534
                    }
3535
                }
3536
                (None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100,
3537
                _ => 0,
3538
            };
3539
            if score >= RENAME_THRESHOLD {
3540
                scored.push((score, ri, ai));
3541
            }
3542
        }
3543
    }
3544
    scored.sort_by(|a, b| {
3545
        b.0.cmp(&a.0)
3546
            .then(removed[a.1].0.cmp(removed[b.1].0))
3547
            .then(added[a.2].0.cmp(added[b.2].0))
3548
    });
3549
    let mut used_r = vec![false; removed.len()];
3550
    let mut used_a = vec![false; added.len()];
3551
    let mut out = Vec::new();
3552
    for (_, ri, ai) in scored {
3553
        if used_r[ri] || used_a[ai] {
3554
            continue;
3555
        }
3556
        used_r[ri] = true;
3557
        used_a[ai] = true;
3558
        out.push((removed[ri].0.clone(), added[ai].0.clone()));
3559
    }
3560
    out
3561
}
3562
3563
/// Percentage of the larger file's bytes found in lines the two share
3564
/// (multiset intersection of lines), like Git's similarity index.
3565
pub fn similarity(a: &[u8], b: &[u8]) -> u32 {
3566
    if a == b {
3567
        return 100;
3568
    }
3569
    if a.is_empty() || b.is_empty() {
3570
        return 0;
3571
    }
3572
    let mut counts: HashMap<&[u8], usize> = HashMap::new();
3573
    for l in split_lines(a) {
3574
        *counts.entry(l).or_default() += 1;
3575
    }
3576
    let mut shared = 0usize;
3577
    for l in split_lines(b) {
3578
        if let Some(n) = counts.get_mut(l) {
3579
            if *n > 0 {
3580
                *n -= 1;
3581
                shared += l.len();
3582
            }
3583
        }
3584
    }
3585
    (shared * 100 / a.len().max(b.len())) as u32
3586
}
3587
3588
#[cfg(test)]
3589
mod join_tests {
3590
    use super::*;
3591
3592
    #[test]
3593
    fn missing_from_finds_what_the_other_lacks() {
3594
        let a: BTreeMap<String, u8> = [("a", 1), ("b", 2), ("c/d", 3), ("e", 4)]
3595
            .into_iter()
3596
            .map(|(k, v)| (k.to_string(), v))
3597
            .collect();
3598
        let b: BTreeMap<String, ()> = [("0", ()), ("b", ()), ("c", ()), ("e", ())]
3599
            .into_iter()
3600
            .map(|(k, v)| (k.to_string(), v))
3601
            .collect();
3602
        let got: Vec<&str> = missing_from(&a, &b)
3603
            .iter()
3604
            .map(|(k, _)| k.as_str())
3605
            .collect();
3606
        assert_eq!(got, vec!["a", "c/d"]);
3607
    }
3608
3609
    #[test]
3610
    fn join_pairs_every_key_in_order() {
3611
        let a: BTreeMap<String, u8> = [("a", 1), ("b/c", 2), ("d", 3)]
3612
            .into_iter()
3613
            .map(|(k, v)| (k.to_string(), v))
3614
            .collect();
3615
        let b: BTreeMap<String, char> = [("b", 'x'), ("b/c", 'y'), ("e", 'z')]
3616
            .into_iter()
3617
            .map(|(k, v)| (k.to_string(), v))
3618
            .collect();
3619
        let got: Vec<(String, Option<u8>, Option<char>)> = join(&a, &b)
3620
            .map(|(k, x, y)| (k.clone(), x.copied(), y.copied()))
3621
            .collect();
3622
        let want = vec![
3623
            ("a".to_string(), Some(1), None),
3624
            ("b".to_string(), None, Some('x')),
3625
            ("b/c".to_string(), Some(2), Some('y')),
3626
            ("d".to_string(), Some(3), None),
3627
            ("e".to_string(), None, Some('z')),
3628
        ];
3629
        assert_eq!(got, want);
3630
    }
3631
}