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