OXYGENHUB

Coverage / crates/oxy-core/src/repo/snapshot.rs 91.4%

2596 of 2841 lines run, 354 of 473 functions · run #393 on identical-sides · run missed

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).
213×
pub fn has_conflict_markers(content: &[u8]) -> bool {
226×
    split_lines(content).iter().any(|l| {
236×
        l.starts_with(b"<<<<<<< oxy conflict")
243×
            || l.starts_with(b"======= oxy conflict")
253×
            || l.starts_with(b">>>>>>> oxy conflict")
266×
    })
273×
}
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.
3450×
pub fn has_own_conflict_markers(content: &[u8], materialized: &[u8], plain: &[&[u8]]) -> bool {
35648×
    let line = |l: &[u8]| -> Vec<u8> {
36648×
        let l = l.strip_suffix(b"\n").unwrap_or(l);
37648×
        l.strip_suffix(b"\r").unwrap_or(l).to_vec()
38648×
    };
39362×
    let marker = |l: &[u8]| {
40362×
        l.starts_with(b"<<<<<<< oxy conflict")
41308×
            || l.starts_with(b"======= oxy conflict")
42254×
            || l.starts_with(b">>>>>>> oxy conflict")
43362×
    };
44110×
    let unconflicted: HashSet<Vec<u8>> = plain.iter().map(|l| line(l)).collect();
4550×
    let own: HashSet<Vec<u8>> = split_lines(materialized)
4650×
        .into_iter()
4750×
        .map(line)
48362×
        .filter(|l| marker(l) && !unconflicted.contains(l))
4950×
        .collect();
5050×
    !own.is_empty()
5150×
        && split_lines(content)
5250×
            .into_iter()
53176×
            .any(|l| own.contains(&line(l)))
5450×
}
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).
1108×
fn has_replace_conflict(base: &Snapshot, path: &str) -> bool {
1118×
    matches!(base.files.get(path).map(|f| &f.content), Some(FileContent::Opaque { blobs }) if blobs.len() > 1)
1128×
}
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.
1305×
pub(crate) fn keep_both_text(
1315×
    text: &[u8],
1325×
    sides: &[Option<Vec<Vec<u8>>>],
1335×
    reverse: bool,
1345×
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) {
1355×
    let plans: Vec<Option<crate::state::Settle>> = sides
1365×
        .iter()
1375×
        .map(|s| {
1385×
            s.as_ref().map(|w| crate::state::Settle {
1394×
                sides: w.clone(),
1404×
                kept: w.clone(),
141
                identical: false,
1424×
            })
1435×
        })
1445×
        .collect();
1455×
    settle_text(text, &plans, reverse)
1465×
}
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.
15323×
pub(crate) fn settle_text(
15423×
    text: &[u8],
15523×
    plans: &[Option<crate::state::Settle>],
15623×
    reverse: bool,
15723×
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) {
158
    // A side as the markers hold it: the renderer ends each with a newline.
15967×
    let with_newline = |s: &[u8]| {
16067×
        let mut s = s.to_vec();
16167×
        if !s.is_empty() && !s.ends_with(b"\n") {
1620×
            s.push(b'\n');
16367×
        }
16467×
        s
16567×
    };
1666656×
    let lines: Vec<&[u8]> = text.split_inclusive(|&b| b == b'\n').collect();
16723×
    let (mut out, mut settled, mut left) = (Vec::new(), Vec::new(), Vec::new());
16823×
    let mut i = 0;
169102×
    while i < lines.len() {
17079×
        let Some((n, total)) = conflict_header(lines[i]) else {
17146×
            out.extend_from_slice(lines[i]);
17246×
            i += 1;
17346×
            continue;
174
        };
17533×
        let end = format!(">>>>>>> oxy conflict {n} of {total} end");
17633×
        let next_side = format!("======= oxy conflict {n} of {total}, side ");
17733×
        let Some(stop) =
178144×
            (i + 1..lines.len()).find(|&j| lines[j].trim_ascii_end() == end.as_bytes())
179
        else {
1800×
            left.push((n, "its end marker is missing; edit it by hand"));
1810×
            out.extend_from_slice(lines[i]);
1820×
            i += 1;
1830×
            continue;
184
        };
18533×
        let mut found: Vec<Vec<u8>> = vec![Vec::new()];
186111×
        for line in &lines[i + 1..stop] {
187111×
            if line.starts_with(next_side.as_bytes()) {
18835×
                found.push(Vec::new());
18976×
            } else {
19076×
                found.last_mut().unwrap().extend_from_slice(line);
19176×
            }
192
        }
19333×
        let wanted = match plans.get(n.wrapping_sub(1)) {
19433×
            _ if total != plans.len() => {
1950×
                Err("the markers are not the file's conflicts now; see `oxy status`")
196
            }
19721×
            Some(Some(w)) => Ok(w),
19812×
            _ => Err("both sides changed these lines; edit the markers"),
199
        };
20033×
        let keep = wanted.and_then(|w| {
20142×
            let mut a: Vec<Vec<u8>> = w.sides.iter().map(|s| with_newline(s)).collect();
20221×
            let mut b = found.clone();
20321×
            a.sort();
20421×
            b.sort();
20521×
            if a == b {
20620×
                Ok(&w.kept)
207
            } else {
2081×
                Err("the lines between its markers were edited; finish it by hand")
209
            }
21021×
        });
21133×
        match keep {
21220×
            Ok(w) => {
21320×
                let order: Vec<&Vec<u8>> = if reverse {
2141×
                    w.iter().rev().collect()
215
                } else {
21619×
                    w.iter().collect()
217
                };
21825×
                for s in order {
21925×
                    out.extend_from_slice(&with_newline(s));
22025×
                }
22120×
                settled.push(n);
222
            }
22313×
            Err(why) => {
22413×
                left.push((n, why));
22577×
                for line in &lines[i..=stop] {
22677×
                    out.extend_from_slice(line);
22777×
                }
228
            }
229
        }
23033×
        i = stop + 1;
231
    }
23223×
    (out, settled, left)
23323×
}
234
235
/// `(n, total)` from a conflict's opening marker (spec §9.5).
23679×
fn conflict_header(line: &[u8]) -> Option<(usize, usize)> {
23779×
    let rest = line.strip_prefix(b"<<<<<<< oxy conflict ")?;
23833×
    let rest = std::str::from_utf8(rest).ok()?;
23933×
    let (n, rest) = rest.split_once(" of ")?;
24066×
    let total = rest.split(|c: char| !c.is_ascii_digit()).next()?;
24133×
    Some((n.parse().ok()?, total.parse().ok()?))
24279×
}
243
24424228×
pub fn is_binary(content: &[u8]) -> bool {
24524228×
    content[..content.len().min(BINARY_SNIFF)].contains(&0)
24624228×
}
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.
2523×
    pub(crate) fn build_against(
2533×
        &self,
2543×
        base: &State,
2553×
        base_snapshot: &Snapshot,
2563×
        reuse: HashMap<String, (NodeId, NodeKind)>,
2573×
    ) -> Result<BuiltChange> {
2583×
        let mut fs_entries =
2593×
            self.walk_working_copy_with(None, &Self::tracked_kinds(base_snapshot), None)?;
2603×
        self.platform_normalize(&mut fs_entries, base_snapshot);
2613×
        self.build_from_entries(base, base_snapshot, reuse, &fs_entries)
2623×
    }
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.
2681597×
    fn platform_normalize(&self, entries: &mut BTreeMap<String, FsEntry>, base: &Snapshot) {
2691597×
        let p = self.platform;
2701597×
        if p.exec_bits && p.symlinks {
2711595×
            return;
2722×
        }
2734×
        for (path, e) in entries.iter_mut() {
2744×
            let Some(f) = base.files.get(path) else {
2750×
                continue;
276
            };
2774×
            if !p.symlinks && f.kind == NodeKind::Symlink && e.kind == NodeKind::File {
2782×
                e.kind = NodeKind::Symlink;
2792×
                e.exec = false;
2802×
            }
2814×
            if !p.exec_bits && f.kind == NodeKind::File && e.kind == NodeKind::File {
2822×
                e.exec = f.mode.exec;
2832×
            }
284
        }
2851597×
    }
286
287
    /// Like `build_against`, for files given in memory (imports).
2881314×
    pub(crate) fn build_from_entries(
2891314×
        &self,
2901314×
        base: &State,
2911314×
        base_snapshot: &Snapshot,
2921314×
        reuse: HashMap<String, (NodeId, NodeKind)>,
2931314×
        fs_entries: &BTreeMap<String, FsEntry>,
2941314×
    ) -> Result<BuiltChange> {
2951314×
        self.build_from_entries_resolving(base, base_snapshot, reuse, fs_entries, &HashSet::new())
2961314×
    }
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.
3031332×
    pub(crate) fn build_from_entries_resolving(
3041332×
        &self,
3051332×
        base: &State,
3061332×
        base_snapshot: &Snapshot,
3071332×
        reuse: HashMap<String, (NodeId, NodeKind)>,
3081332×
        fs_entries: &BTreeMap<String, FsEntry>,
3091332×
        resolve: &HashSet<String>,
3101332×
    ) -> Result<BuiltChange> {
3111332×
        self.build_from_entries_with(
3121332×
            base,
3131332×
            base_snapshot,
3141332×
            reuse,
3151332×
            fs_entries,
3161332×
            resolve,
3171332×
            Scope::default(),
318
        )
3191332×
    }
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.
32988×
    pub(crate) fn build_from_entries_seeded(
33088×
        &self,
33188×
        base: &State,
33288×
        base_snapshot: &Snapshot,
33388×
        fs_entries: &BTreeMap<String, FsEntry>,
33488×
        seed: [u8; 32],
33588×
        dir_needed: Option<&dyn Fn(&str) -> bool>,
33688×
    ) -> Result<BuiltChange> {
33788×
        self.build_from_entries_with(
33888×
            base,
33988×
            base_snapshot,
34088×
            HashMap::new(),
34188×
            fs_entries,
34288×
            &HashSet::new(),
34388×
            Scope {
34488×
                seed: Some(seed),
34588×
                dir_needed,
34688×
            },
347
        )
34888×
    }
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.
3551017×
    fn build_changed_only(
3561017×
        &self,
3571017×
        base: &State,
3581017×
        base_snapshot: &Snapshot,
3591017×
        reuse: HashMap<String, (NodeId, NodeKind)>,
3601017×
        fs_entries: &BTreeMap<String, FsEntry>,
3611017×
    ) -> Result<BuiltChange> {
3621017×
        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.
3651017×
        let changed: BTreeMap<String, FsEntry> = join(fs_entries, &base_snapshot.files)
36611232×
            .filter_map(|(p, e, f)| {
36711232×
                let e = e?;
36811104×
                (!e.unchanged || f.is_none_or(|f| f.kind != e.kind || f.mode.exec != e.exec))
36911104×
                    .then(|| (p.clone(), e.clone()))
37011232×
            })
3711017×
            .collect();
3721017×
        let mut files: BTreeMap<String, crate::state::FileState> = BTreeMap::new();
3736873×
        for p in changed.keys() {
3746873×
            if let Some(f) = base_snapshot.files.get(p) {
3753791×
                files.insert(p.clone(), f.clone());
3763791×
            }
377
        }
378
        // Paths the base has and the disk does not.
3791017×
        let gone = join(&base_snapshot.files, fs_entries)
38011232×
            .filter_map(|(p, f, e)| (e.is_none()).then_some((p, f?)));
3811017×
        for (p, f) in gone {
382128×
            if f.kind == NodeKind::Dir {
383100×
                continue;
38428×
            }
38528×
            files.insert(p.clone(), f.clone());
38628×
            let mut end = p.len();
38733×
            while let Some(i) = p[..end].rfind('/') {
3885×
                let dir = &p[..i];
3895×
                if files.contains_key(dir) {
3900×
                    break;
3915×
                }
3925×
                if let Some(d) = base_snapshot.files.get(dir) {
3935×
                    files.insert(dir.to_string(), d.clone());
3945×
                }
3955×
                end = i;
396
            }
397
        }
3981017×
        let part = Snapshot {
3991017×
            tree: base_snapshot.tree.clone(),
4001017×
            files,
4011017×
            unrendered: BTreeSet::new(),
4021017×
        };
4031017×
        drop(pick);
4041017×
        let _build = crate::timing::phase("snapshot:   of which building the change");
405
        // Whether anything on disk is under `dir`.
4061017×
        let dir_needed = |dir: &str| {
4075×
            let prefix = format!("{dir}/");
4085×
            fs_entries
4095×
                .range(prefix.clone()..)
4105×
                .next()
4115×
                .is_some_and(|(k, _)| k.starts_with(&prefix))
4125×
        };
4131017×
        self.build_from_entries_with(
4141017×
            base,
4151017×
            &part,
4161017×
            reuse,
4171017×
            &changed,
4181017×
            &HashSet::new(),
4191017×
            Scope {
4201017×
                seed: None,
4211017×
                dir_needed: Some(&dir_needed),
4221017×
            },
423
        )
4241017×
    }
425
4262437×
    fn build_from_entries_with(
4272437×
        &self,
4282437×
        base: &State,
4292437×
        base_snapshot: &Snapshot,
4302437×
        reuse: HashMap<String, (NodeId, NodeKind)>,
4312437×
        fs_entries: &BTreeMap<String, FsEntry>,
4322437×
        resolve: &HashSet<String>,
4332437×
        scope: Scope,
4342437×
    ) -> Result<BuiltChange> {
4352437×
        let Scope { seed, dir_needed } = scope;
4362437×
        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.
4392437×
        let attrs = crate::attributes::Attributes::from_files(
4402437×
            fs_entries
4412437×
                .iter()
44219549×
                .filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes"))
4432437×
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
444
        );
4452437×
        let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs);
4462437×
        b.resolve = resolve.clone();
4472437×
        b.node_seed = seed;
4482437×
        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.
4522437×
        let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p));
4532437×
        let renamed_from: HashMap<&str, &str> = renames
4542437×
            .iter()
4552437×
            .map(|(old, new)| (new.as_str(), old.as_str()))
4562437×
            .collect();
4572437×
        let renamed_to: HashMap<&str, &str> = renames
4582437×
            .iter()
4592437×
            .map(|(old, new)| (old.as_str(), new.as_str()))
4602437×
            .collect();
4612437×
        laps.lap("build: renames");
4622437×
        b.diff_in_parallel(fs_entries);
4632437×
        laps.lap("build: diffs");
46419549×
        for (path, fs_entry) in fs_entries {
46519549×
            match base_snapshot.files.get(path) {
4666231×
                None => match renamed_from.get(path.as_str()) {
46737×
                    Some(old) => {
46837×
                        b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])?
469
                    }
4706194×
                    None => b.add_path(self, path, fs_entry)?,
471
                },
47213318×
                Some(expected) => b.update_path(self, path, fs_entry, expected)?,
473
            }
474
        }
4752437×
        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.
4782437×
        let mut needed_dirs: HashSet<&str> = HashSet::new();
47919562×
        fn parents_of<'a>(p: &'a str, set: &mut HashSet<&'a str>) {
48019562×
            let mut end = p.len();
48120288×
            while let Some(i) = p[..end].rfind('/') {
48215510×
                if !set.insert(&p[..i]) {
48314784×
                    break;
484726×
                }
485726×
                end = i;
486
            }
48719562×
        }
48819549×
        for p in fs_entries.keys() {
48919549×
            parents_of(p, &mut needed_dirs);
49019549×
        }
49113946×
        for p in base_snapshot.files.keys() {
49213946×
            if !self.in_scope(p) {
49313×
                parents_of(p, &mut needed_dirs);
49413933×
            }
495
        }
49613946×
        for (path, expected) in &base_snapshot.files {
49713946×
            if fs_entries.contains_key(path)
498628×
                || !self.in_scope(path)
499615×
                || renamed_to.contains_key(path.as_str())
500
            {
50113368×
                continue;
502578×
            }
503578×
            let still_needed = expected.kind == NodeKind::Dir
504481×
                && match dir_needed {
50521×
                    Some(f) => f(path),
506460×
                    None => needed_dirs.contains(path.as_str()),
507
                };
508578×
            if !still_needed {
509123×
                b.remove_path(path, expected);
510455×
            }
511
        }
5122437×
        laps.lap("build: removals");
5132437×
        if seed.is_some() {
51488×
            b.retire_stray_placements();
5152349×
        }
5162437×
        b.retire_unified();
5172437×
        laps.lap("build: stray placements");
5182437×
        let mut statuses = b.statuses;
5195748×
        statuses.sort_by(|a, b| a.path.cmp(&b.path));
5202437×
        Ok(BuiltChange {
5212437×
            tree_ops: b.tree_ops,
5222437×
            edits: b.edits,
5232437×
            contributors: b.contributors.into_iter().collect(),
5242437×
            resolves: b.resolves,
5252437×
            statuses,
5262437×
            blobs: b.blobs,
5272437×
        })
5282437×
    }
529
530
    /// Store a change and, when `core.sign = change` and we are its author,
531
    /// sign it. A rewritten change by another author is stored unsigned: only
532
    /// its author (or a delegate) can vouch for it.
533819×
    pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> {
534819×
        let ours = change.author == self.author();
535819×
        if self.config.sign_changes && ours && self.secret_key()?.is_none() {
5361×
            return fail("core.sign is \"change\" but no secret key is available for author.key");
537818×
        }
538818×
        let storing = crate::timing::phase("store: the change");
539818×
        let hash = self.store.put_change(change)?;
540818×
        drop(storing);
541818×
        if self.config.sign_changes && ours {
54243×
            let _t = crate::timing::phase("store: sign it");
54343×
            self.sign_change(&hash)?;
544775×
        }
545818×
        Ok(hash)
546819×
    }
547
5481601×
    fn resolutions_path(&self) -> PathBuf {
5491601×
        self.oxy
5501601×
            .join("workspaces")
5511601×
            .join(self.workspace.to_hex())
5521601×
            .join("resolve")
5531601×
    }
554
555
    /// The paths whose replace conflicts the user chose to settle with what
556
    /// the working copy holds there (`oxy resolve PATH`, spec §9.3).
5571597×
    pub fn resolutions(&self) -> Result<BTreeSet<String>> {
5581597×
        match fs::read_to_string(self.resolutions_path()) {
5595×
            Ok(text) => Ok(text
5605×
                .lines()
5615×
                .filter(|l| !l.is_empty())
5625×
                .map(String::from)
5635×
                .collect()),
5641592×
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()),
5650×
            Err(e) => Err(e.into()),
566
        }
5671597×
    }
568
569
    /// Settle the replace conflicts at `paths` (a file's or a subrepo pin's,
570
    /// spec §9.3) with what the working copy holds there: the next snapshot
571
    /// replaces every live candidate with it. Refused for a path whose base
572
    /// has no replace conflict.
5733×
    pub fn choose_resolutions(&self, base: &Snapshot, paths: &[String]) -> Result<()> {
5743×
        for p in paths {
5753×
            if !has_replace_conflict(base, p) {
5761×
                return Err(RepoError::Msg(format!(
5771×
                    "{p} has no replace conflict to settle (a text file's conflict is settled by editing out its markers, or `oxy resolve --both {p}`)"
5781×
                )));
5792×
            }
580
        }
5812×
        let mut all = self.resolutions()?;
5822×
        all.extend(paths.iter().cloned());
5832×
        let text: String = all.iter().map(|p| format!("{p}\n")).collect();
5842×
        crate::durable::write_atomic(&self.resolutions_path(), text.as_bytes())?;
5852×
        Ok(())
5863×
    }
587
588
    /// Settle the adjacent-insertion conflicts of the text files at `paths`
589
    /// (oxygen#121) by keeping every side, earliest change first (latest
590
    /// first with `reverse`): each one's markers in the working copy give
591
    /// way to its sides' lines, and the next snapshot records the result.
592
    /// A conflict whose sides changed the same lines, or whose text between
593
    /// the markers was edited, keeps its markers. With no paths, every
594
    /// conflicted text file.
5953×
    pub fn keep_both(
5963×
        &self,
5973×
        snap: &SnapshotResult,
5983×
        paths: &[String],
5993×
        reverse: bool,
6003×
    ) -> Result<Vec<KeptBoth>> {
6013×
        let paths: Vec<String> = if paths.is_empty() {
6022×
            snap.base_snapshot
6032×
                .files
6042×
                .iter()
6052×
                .filter(|(_, f)| matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted()))
6062×
                .map(|(p, _)| p.clone())
6072×
                .collect()
608
        } else {
6091×
            paths.to_vec()
610
        };
6113×
        let mut out = Vec::new();
6123×
        for p in paths {
6133×
            let conflicted = match snap.base_snapshot.files.get(&p).map(|f| (f, &f.content)) {
6143×
                Some((f, FileContent::Text { materialized, .. }))
6153×
                    if materialized.is_conflicted() =>
616
                {
6173×
                    f
618
                }
6190×
                Some((_, FileContent::Opaque { blobs })) if blobs.len() > 1 => {
6200×
                    return Err(RepoError::Msg(format!(
6210×
                        "{p} has a replace conflict: keep one version with `oxy resolve {p}`"
6220×
                    )))
623
                }
624
                _ => {
6250×
                    return Err(RepoError::Msg(format!(
6260×
                        "{p} has no text conflict to settle"
6270×
                    )))
628
                }
629
            };
6303×
            let sides = snap.base.both_sides(&conflicted.node);
6313×
            let disk = self.root().join(&p);
6323×
            let text = fs::read(&disk)?;
6333×
            let (bytes, settled, left) = keep_both_text(&text, &sides, reverse);
6343×
            if !settled.is_empty() {
6352×
                fs::write(&disk, bytes)?;
6361×
            }
6373×
            out.push(KeptBoth {
6383×
                path: p,
6393×
                settled,
6403×
                identical: Vec::new(),
6413×
                left,
6423×
            });
643
        }
6443×
        Ok(out)
6453×
    }
646
647
    /// Settle what a checkout may settle by itself in each conflicted text
648
    /// file ([`State::settle_plans`]): conflicts whose sides are identical,
649
    /// keeping one (oxygen#146), and in files whose `.oxyattributes` say
650
    /// `merge=union`, adjacent insertions, keeping both sides earliest
651
    /// change first (oxygen#145). The next commit records the resolution.
652
    /// The files it settled something in.
65379×
    pub(crate) fn settle_conflicts(&self, command: &str) -> Result<Vec<KeptBoth>> {
65479×
        let snap = self.snapshot(command)?;
65579×
        let mut out = Vec::new();
656316×
        for (p, f) in &snap.base_snapshot.files {
657259×
            if !matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted())
658
            {
659229×
                continue;
66087×
            }
66187×
            let plans = snap
66287×
                .base
66387×
                .settle_plans(&f.node, snap.attributes.lookup(p).union);
66487×
            if plans.iter().all(Option::is_none) {
66572×
                continue;
66615×
            }
66715×
            let disk = self.root().join(p);
66815×
            let Ok(text) = fs::read(&disk) else { continue };
66915×
            let (bytes, settled, left) = settle_text(&text, &plans, false);
67015×
            if settled.is_empty() {
6710×
                continue;
67215×
            }
67315×
            fs::write(&disk, bytes)?;
67415×
            let identical = settled
67515×
                .iter()
67615×
                .copied()
67715×
                .filter(|n| plans[n - 1].as_ref().is_some_and(|w| w.identical))
67815×
                .collect();
67915×
            out.push(KeptBoth {
68015×
                path: p.clone(),
68115×
                settled,
68215×
                identical,
68315×
                left,
68415×
            });
685
        }
68679×
        Ok(out)
68779×
    }
688
689
    /// Every conflict of `snap`, `state`'s materialization, settled as a
690
    /// checkout settles them ([`State::settle_plans`]): the new text of
691
    /// each conflicted file, when each is a text file whose every conflict
692
    /// has identical sides (one kept, oxygen#146) or, in a file its
693
    /// `.oxyattributes` mark `merge=union`, is an adjacent insertion (both
694
    /// kept, oxygen#145); `None` when any conflict needs a person, or there
695
    /// is none. A host lands a proposal with these as a resolution, as a
696
    /// checkout settles them in a working copy.
6973×
    pub fn union_resolution(state: &State, snap: &Snapshot) -> Option<Vec<(String, Vec<u8>)>> {
6983×
        if snap.tree.is_conflicted() {
6990×
            return None;
7003×
        }
7013×
        let attrs = Repo::state_attributes(snap);
7023×
        let mut out = Vec::new();
7037×
        for (p, f) in &snap.files {
7047×
            if !f.is_conflicted() {
7054×
                continue;
7063×
            }
7073×
            let FileContent::Text { bytes, .. } = &f.content else {
7080×
                return None;
709
            };
7103×
            let plans = state.settle_plans(&f.node, attrs.lookup(p).union);
7113×
            let (text, _, left) = settle_text(bytes, &plans, false);
7123×
            if !left.is_empty() || plans.iter().any(Option::is_none) {
7131×
                return None;
7142×
            }
7152×
            out.push((p.clone(), text));
716
        }
7172×
        (!out.is_empty()).then_some(out)
7183×
    }
719
720
    /// The chosen resolutions `base` still needs; the rest are forgotten.
7211594×
    fn resolutions_for(&self, base: &Snapshot) -> Result<HashSet<String>> {
7221594×
        let all = self.resolutions()?;
7231594×
        if all.is_empty() {
7241589×
            return Ok(HashSet::new());
7255×
        }
7265×
        let live: HashSet<String> = all
7275×
            .iter()
7285×
            .filter(|p| has_replace_conflict(base, p))
7295×
            .cloned()
7305×
            .collect();
7315×
        if live.is_empty() {
7322×
            match fs::remove_file(self.resolutions_path()) {
7332×
                Ok(()) => {}
7340×
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
7350×
                Err(e) => return Err(e.into()),
736
            }
7373×
        }
7385×
        Ok(live)
7391594×
    }
740
741
    /// Snapshot the working copy into the working-copy change (spec §12.3).
742
    /// The result carries the full base state and its whole rendering.
743575×
    pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> {
744575×
        self.snapshot_as(command, false, false)
745575×
    }
746
747
    /// Snapshot the working copy as [`Repo::snapshot`] does, for commands
748
    /// that need its statuses, conflicts and base history, and the graphs
749
    /// of the files it changes: status, log, show, commit, diff, hunks and
750
    /// discard. Text files known unchanged since the last manifest are
751
    /// neither read nor rendered: the result's `base` is a history-only
752
    /// state without their graphs, and its `base_snapshot` lists them in
753
    /// `unrendered` with empty bytes. They are never conflicted, and no
754
    /// change the working copy makes touches them.
755467×
    pub fn snapshot_status(&self, command: &str) -> Result<SnapshotResult> {
756467×
        self.snapshot_as(command, true, false)
757467×
    }
758
759
    /// `snapshot_as`; `for_commit` writes the manifest even where a status
760
    /// would not bother (an edited file is read next time whatever it
761
    /// says), since after the commit that file is unchanged, and records no
762
    /// operation: the commit's own, recorded straight after, replaces the
763
    /// working copy, so the snapshot's would cost a durable write for
764
    /// nothing. Should the commit fail, the files are still on disk and the
765
    /// next snapshot finds them.
7661597×
    fn snapshot_as(
7671597×
        &self,
7681597×
        command: &str,
7691597×
        partial: bool,
7701597×
        for_commit: bool,
7711597×
    ) -> Result<SnapshotResult> {
7721597×
        let _lock = self.lock_snapshot()?;
7731597×
        let batch = self.store.begin_batch()?;
7741597×
        let result = self.snapshot_locked(command, partial, for_commit);
7751597×
        batch.finish()?;
7761597×
        result
7771597×
    }
778
7791597×
    fn snapshot_locked(
7801597×
        &self,
7811597×
        command: &str,
7821597×
        partial: bool,
7831597×
        for_commit: bool,
7841597×
    ) -> Result<SnapshotResult> {
7851597×
        let _all = crate::timing::phase("snapshot");
7861597×
        let (op_ref, op) = self.head_op()?;
7871597×
        let (old_hash, old_wc) = self.working_copy()?;
788
        // The base's history first: its graph keys say which files the
789
        // manifest already knows equal to it.
7901595×
        let mut base = {
7911595×
            let _t = crate::timing::phase("snapshot: load base history");
7921595×
            self.load_state_indexed(&old_wc.deps)?
793
        };
7941595×
        let view = {
7951595×
            let _t = crate::timing::phase("snapshot: tree view");
7961595×
            self.tree_view(&mut base)?
797
        };
7981595×
        let manifest = {
7991595×
            let _t = crate::timing::phase("snapshot: read manifest");
8001595×
            self.read_manifest()?.map(|(_, _, m)| m)
801
        };
8021595×
        let tracked: BTreeMap<String, (NodeKind, Option<ObjectRef>)> = {
8031595×
            let _t = crate::timing::phase("snapshot: tracked paths");
804
            // In the view's path order, so the map is built without searching.
8051595×
            view.entries
8061595×
                .iter()
80710060×
                .filter(|(_, e)| e.kind != NodeKind::Dir)
8089605×
                .map(|(p, e)| {
8099605×
                    let blob = if e.kind != NodeKind::File || e.mode.opaque {
810185×
                        base.live_blobs(&e.node).first().copied()
811
                    } else {
8129420×
                        None
813
                    };
8149605×
                    (p.clone(), (e.kind, blob))
8159605×
                })
8161595×
                .collect()
817
        };
818
        // A text file's graph key, worked out when the walk asks for it.
81912695×
        let key_of = |p: &str| -> Option<[u8; 32]> {
82012695×
            let e = view.entries.get(p)?;
8219485×
            if e.kind != NodeKind::File || e.mode.opaque {
822143×
                return None;
8239342×
            }
8249342×
            base.graph_key(&e.node)
82512695×
        };
8261594×
        let mut fs_entries = {
8271595×
            let _t = crate::timing::phase("snapshot: read working copy");
8281595×
            self.walk_working_copy_with(manifest.as_deref(), &tracked, Some(&key_of))?
829
        };
830
        // The keys of the files read, before building graphs drops them: a
831
        // file found equal to its base rendering keeps its key.
8321594×
        let keys: HashMap<String, [u8; 32]> = fs_entries
8331594×
            .iter()
83412743×
            .filter(|(_, e)| !e.unchanged && e.kind == NodeKind::File)
8358452×
            .filter_map(|(p, _)| Some((p.clone(), key_of(p)?)))
8361594×
            .collect();
8371594×
        let base_snapshot = {
8381594×
            let _t = crate::timing::phase("snapshot: load and render base");
8391594×
            if partial {
8401022×
                let sets = crate::timing::phase("snapshot:   of which choosing graphs");
841
                // Every file but those on disk known unchanged.
8421022×
                let need: Vec<(&str, NodeId)> = join(&view.entries, &fs_entries)
84311254×
                    .filter_map(|(p, v, f)| {
84411254×
                        let v = v?;
8458170×
                        (v.kind == NodeKind::File && !f.is_some_and(|f| f.unchanged))
8468170×
                            .then_some((p.as_str(), v.node))
84711254×
                    })
8481022×
                    .collect();
8491022×
                let render: std::collections::HashSet<&str> =
8501022×
                    need.iter().map(|(p, _)| *p).collect();
8511022×
                let need: Vec<NodeId> = need.iter().map(|(_, n)| *n).collect();
8521022×
                drop(sets);
853
                {
8541022×
                    let _t = crate::timing::phase("snapshot:   of which building graphs");
8551022×
                    self.load_graphs_of(&mut base, &need)?;
856
                }
8571022×
                let _t = crate::timing::phase("snapshot:   of which rendering");
8581022×
                base.materialize_only(&render)
859
            } else {
860572×
                self.load_all_graphs(&mut base)?;
861572×
                base.materialize()
862
            }
863
        };
864
865
        // Reuse the NodeIds the previous working-copy version assigned to new
866
        // paths, so an unchanged working copy hashes identically (spec §12.3).
8671594×
        let reuse = added_node_paths(&old_wc, &base_snapshot);
8681594×
        self.platform_normalize(&mut fs_entries, &base_snapshot);
869
        // Replace conflicts the user settled with `oxy resolve PATH`
870
        // (oxygen#140): those still in the base; a choice the base no longer
871
        // needs (once committed) is forgotten.
8721594×
        let resolve = self.resolutions_for(&base_snapshot)?;
8731594×
        let built = {
8741594×
            let _t = crate::timing::phase("snapshot: diff against base");
8751594×
            if !resolve.is_empty() {
8763×
                self.build_from_entries_resolving(
8773×
                    &base,
8783×
                    &base_snapshot,
8793×
                    reuse,
8803×
                    &fs_entries,
8813×
                    &resolve,
8820×
                )?
8831591×
            } else if partial && self.config.sparse_paths.is_empty() {
884
                #[cfg(test)]
885733×
                let full =
886733×
                    self.build_from_entries(&base, &base_snapshot, reuse.clone(), &fs_entries)?;
8871017×
                let built = self.build_changed_only(&base, &base_snapshot, reuse, &fs_entries)?;
888
                #[cfg(test)]
889
                {
890
                    // New nodes take random identifiers: compare them by
891
                    // order of appearance.
8921466×
                    let norm = |b: &BuiltChange| {
8931466×
                        let text = format!("{:?} {:?} {:?}", b.tree_ops, b.edits, b.statuses);
8941466×
                        let mut seen: Vec<String> = Vec::new();
8951466×
                        let mut out = String::new();
8961466×
                        let mut rest = text.as_str();
89720796×
                        while let Some(i) = rest.find("NodeId(") {
89819330×
                            out.push_str(&rest[..i]);
89919330×
                            let end = i + rest[i..].find(')').unwrap() + 1;
90019330×
                            let id = rest[i..end].to_string();
9019046576×
                            let n = seen.iter().position(|x| *x == id).unwrap_or_else(|| {
9027420×
                                seen.push(id);
9037420×
                                seen.len() - 1
9047420×
                            });
90519330×
                            out.push_str(&format!("N{n}"));
90619330×
                            rest = &rest[end..];
907
                        }
9081466×
                        out.push_str(rest);
9091466×
                        out
9101466×
                    };
911733×
                    assert_eq!(
912733×
                        norm(&built),
913733×
                        norm(&full),
914
                        "a changed-only build is the full one"
915
                    );
916
                }
9171017×
                built
918
            } else {
919574×
                self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)?
920
            }
921
        };
922
        // Text files found equal to an unconflicted base rendering keep its
923
        // graph key, so the next snapshot need not read or render them.
924
        {
9253944×
            let changed: HashSet<&str> = built.statuses.iter().map(|s| s.path.as_str()).collect();
92612743×
            for (p, e) in fs_entries.iter_mut() {
92712743×
                if e.unchanged || e.kind != NodeKind::File || changed.contains(p.as_str()) {
9288130×
                    continue;
9294613×
                }
9304613×
                e.graph_key = match base_snapshot.files.get(p).map(|f| &f.content) {
931
                    Some(FileContent::Text {
9324489×
                        bytes,
9334489×
                        materialized,
9344489×
                    }) if !materialized.is_conflicted() && *bytes == *e.content => {
9354392×
                        keys.get(p).copied()
936
                    }
937221×
                    _ => None,
938
                };
939
            }
940
        }
941
9421594×
        let mut wc = Change {
9431594×
            version: crate::objects::CHANGE_VERSION,
9441594×
            change_id: old_wc.change_id,
9451594×
            deps: vec![],
9461594×
            author: self.author(),
9471594×
            hlc: old_wc.hlc,
9481594×
            message: old_wc.message.clone(),
9491594×
            tree_ops: built.tree_ops,
9501594×
            edits: built.edits,
9511594×
            kind: ChangeKind::WorkingCopy,
9521594×
        };
9531594×
        let base_heads = base.maximal(&old_wc.deps);
9541594×
        let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect();
9551594×
        deps.extend(referenced_changes(&wc, &base));
9561594×
        deps.extend(built.contributors.iter().copied());
9571594×
        wc.deps = deps.into_iter().collect();
9581594×
        wc.normalize();
9591594×
        let resolves = built.resolves;
9601594×
        let contributors = built.contributors;
9611594×
        let statuses = built.statuses;
962
        // Empty snapshot: hash the candidate before storing; only record an op on change.
9631594×
        let record = crate::timing::phase("snapshot: hash and record");
964
        // Encoded once, its texts stored a batch at a time as they are
965
        // made (a stored blob is not stored again); the change itself only
966
        // when it changed.
967
        // For a commit the change is neither encoded, stored nor recorded:
968
        // the commit stores its own, built from this one's operations, and
969
        // its texts with it. The hash it would have is not needed.
9701594×
        let (new_hash, changed, encoded) = if for_commit {
971555×
            (old_hash, true, Vec::new())
972
        } else {
9731039×
            let encoded = wc.encode_streaming(|batch| self.store.put_texts(&batch))?;
9741039×
            let new_hash = crate::hash::hash_object(crate::hash::ObjectType::Change, &encoded);
9751039×
            (new_hash, new_hash != old_hash, encoded)
976
        };
9771594×
        if changed && !for_commit {
978322×
            self.store.put(crate::hash::ObjectType::Change, &encoded)?;
979322×
            let mut view = op.view.clone();
980322×
            view.workspaces.insert(self.workspace, new_hash);
981322×
            self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?;
9821272×
        }
9831594×
        drop(record);
984
        // The prompt compares against this without recording anything.
9851594×
        let _t = crate::timing::phase("snapshot: write manifest");
9861594×
        let conflicted = base_snapshot.is_conflicted();
9871594×
        let uncommitted = !wc.tree_ops.is_empty() || !wc.edits.is_empty();
988
        // Rewriting the manifest pays only when it would teach the next
989
        // snapshot something: other flags, a file found equal to the base
990
        // (it need not be read again), a new blob, a path added or gone.
991
        // An edited file is read next time whatever it says.
9921594×
        let worth = match self.read_manifest()? {
993555×
            _ if for_commit => true,
994362×
            None => true,
995677×
            Some((c, u, m)) => {
996677×
                c != conflicted
997677×
                    || u != uncommitted
998421×
                    || m.entries.len() != fs_entries.len()
999555×
                    || fs_entries.iter().any(|(p, e)| {
1000555×
                        !e.unchanged && (e.graph_key.is_some() || !m.entries.contains_key(p))
1001555×
                    })
100275×
                    || built
100375×
                        .blobs
100475×
                        .iter()
100575×
                        .any(|(p, b)| m.entries.get(p).and_then(|e| e.blob) != Some(*b))
1006
                    // With uncommitted work, the status cache below holds
1007
                    // only while the manifest describes the edited files as
1008
                    // they are now.
100974×
                    || (uncommitted && statuses.iter().any(|st| {
101074×
                        let Some(e) = fs_entries.get(&st.path) else {
10114×
                            return m.entries.contains_key(&st.path);
1012
                        };
101370×
                        let Some(me) = m.entries.get(&st.path) else {
10140×
                            return true;
1015
                        };
101670×
                        let hash = e
101770×
                            .content_hash
101870×
                            .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes());
101970×
                        let stat = e.stat.or_else(|| {
10200×
                            fs::symlink_metadata(self.root.join(&st.path))
10210×
                                .ok()
10220×
                                .map(|md| (md.len(), super::prompt::mtime_ns(&md)))
10230×
                        });
1024
                        // The executable bit too: a mode change moves
1025
                        // neither size nor time.
102670×
                        me.hash != hash || me.exec != e.exec || stat != Some((me.size, me.mtime_ns))
102774×
                    }))
1028
            }
1029
        };
10301594×
        let attributes = crate::attributes::Attributes::from_files(
10311594×
            fs_entries
10321594×
                .iter()
103312743×
                .filter(|(p, _)| is_attributes(p))
10341594×
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
1035
        );
10361594×
        let mut manifest = None;
10371594×
        if for_commit {
1038555×
            manifest = Some(PendingManifest {
1039555×
                entries: fs_entries,
1040555×
                blobs: built.blobs.clone(),
1041555×
                dirs: self.walked_dirs.take(),
1042555×
                written: super::prompt::now_ns(),
1043555×
            });
1044555×
        } else {
10451039×
            if worth {
1046968×
                self.write_manifest_entries(conflicted, uncommitted, &fs_entries, &built.blobs)?;
104771×
            }
10481039×
            self.write_status_cache((uncommitted && !conflicted).then_some(&new_hash), &statuses);
1049
        }
10501594×
        Ok(SnapshotResult {
10511594×
            wc_hash: new_hash,
10521594×
            wc,
10531594×
            base,
10541594×
            base_snapshot,
10551594×
            statuses,
10561594×
            changed,
10571594×
            resolves,
10581594×
            contributors,
10591594×
            attributes,
10601594×
            untracked_nested: self.untracked_nested.borrow().clone(),
10611594×
            manifest,
10621594×
        })
10631597×
    }
1064
1065
    /// `oxy status --json` (spec §9.5): the changed paths and every conflict
1066
    /// of the working copy's base with its kind, path, and contributing
1067
    /// ChangeIds.
10688×
    pub fn status_json(&self, snap: &SnapshotResult) -> Result<String> {
10698×
        let heads = self.bookmark_heads()?;
10708×
        let base_snapshot = &snap.base_snapshot;
10718×
        let by_node = base_snapshot.tree.by_node();
10728×
        let path_of = |n: &NodeId| by_node.get(n).map(|e| e.path.clone());
10738×
        let id_of = |h: &ChangeHash| {
10744×
            snap.base
10754×
                .get(h)
10764×
                .map(|m| m.change_id.to_hex())
10774×
                .unwrap_or_else(|| h.to_hex())
10784×
        };
10798×
        let paths = json_array(snap.statuses.iter().map(|s| {
10804×
            format!(
1081
                "{{\"path\":{},\"status\":{}}}",
10824×
                json_str(&s.path),
10834×
                json_str(&format!("{:?}", s.status))
1084
            )
10854×
        }));
10868×
        let mut conflicts: Vec<String> = Vec::new();
10879×
        for (p, f) in &base_snapshot.files {
10889×
            if !f.is_conflicted() {
10897×
                continue;
10902×
            }
10912×
            let (kind, changes) = match &f.content {
1092
                FileContent::Text { .. } => {
10932×
                    ("text", snap.base.conflict_regions(&f.node).contributors)
1094
                }
10950×
                FileContent::Opaque { blobs } => {
10960×
                    ("replace", snap.base.blob_contributors(&f.node, blobs))
1097
                }
10980×
                FileContent::Dir => continue,
1099
            };
11002×
            let resolved = match snap
11012×
                .statuses
11022×
                .iter()
11032×
                .find(|s| s.path == *p)
11042×
                .map(|s| s.status)
1105
            {
11061×
                Some(ChangeStatus::Resolved) => "true",
11070×
                Some(ChangeStatus::ConflictEdited) => "\"markers\"",
11081×
                _ => "false",
1109
            };
11102×
            conflicts.push(format!(
1111
                "{{\"kind\":{},\"path\":{},\"node\":{},\"changes\":{},\"resolved\":{resolved}}}",
11122×
                json_str(kind),
11132×
                json_str(p),
11142×
                json_str(&f.node.to_hex()),
11154×
                json_array(changes.iter().map(|h| json_str(&id_of(h))))
1116
            ));
1117
        }
11188×
        for c in &base_snapshot.tree.conflicts {
11190×
            let (kind, nodes): (&str, Vec<NodeId>) = match c {
11200×
                crate::tree::TreeConflict::Name { nodes, .. } => ("name", nodes.clone()),
11210×
                crate::tree::TreeConflict::MultiPlacement { node, .. } => {
11220×
                    ("multi-placement", vec![*node])
1123
                }
11240×
                crate::tree::TreeConflict::Orphan { node, .. } => ("orphan", vec![*node]),
11250×
                crate::tree::TreeConflict::Cycle { node } => ("cycle", vec![*node]),
11260×
                crate::tree::TreeConflict::Mode { node, .. } => ("mode", vec![*node]),
1127
            };
11280×
            let detail = match c {
11290×
                crate::tree::TreeConflict::Name { name, parent, .. } => {
11300×
                    let dir = if *parent == NodeId::ROOT {
11310×
                        String::new()
1132
                    } else {
11330×
                        path_of(parent).unwrap_or_default()
1134
                    };
11350×
                    format!(
1136
                        ",\"path\":{}",
11370×
                        json_str(&if dir.is_empty() {
11380×
                            name.clone()
1139
                        } else {
11400×
                            format!("{dir}/{name}")
1141
                        })
1142
                    )
1143
                }
11440×
                _ => nodes
11450×
                    .first()
11460×
                    .and_then(path_of)
11470×
                    .map(|p| format!(",\"path\":{}", json_str(&p)))
11480×
                    .unwrap_or_default(),
1149
            };
11500×
            conflicts.push(format!(
1151
                "{{\"kind\":{},\"nodes\":{}{detail}}}",
11520×
                json_str(kind),
11530×
                json_array(nodes.iter().map(|n| json_str(&n.to_hex())))
1154
            ));
1155
        }
11568×
        Ok(format!(
1157
            "{{\"bookmark\":{},\"working_copy\":{},\"heads\":{},\"clean\":{},\"conflicted\":{},\"paths\":{paths},\"conflicts\":{},\"untracked_nested\":{},\"skipped_large\":{}}}",
11588×
            json_str(&self.config.bookmark),
11598×
            json_str(&snap.wc.change_id.to_hex()),
11608×
            heads.len(),
11618×
            snap.statuses.is_empty(),
11628×
            base_snapshot.is_conflicted(),
11638×
            json_array(conflicts),
11648×
            json_array(snap.untracked_nested.iter().map(|p| json_str(p))),
11658×
            json_array(
11668×
                self.skipped_large
11678×
                    .borrow()
11688×
                    .iter()
11698×
                    .map(|(p, n)| format!("{{\"path\":{},\"size\":{n}}}", json_str(p)))
1170
            )
1171
        ))
11728×
    }
1173
1174
    /// Turn the working-copy change into a normal change on the bookmark (spec §12.3).
1175754×
    pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> {
1176754×
        self.commit_selection(message, extra_deps, &Selection::All)
1177754×
    }
1178
1179
    /// The hunks of the working copy, for `oxy commit --interactive`: each
1180
    /// tree operation and each edit, in the order `select_hunks` numbers them.
11818×
    pub fn hunks(&self) -> Result<(SnapshotResult, Vec<Hunk>)> {
11828×
        let snap = self.snapshot_status("oxy commit")?;
11838×
        let hunks = render_hunks(&snap.wc, &snap.base, &snap.base_snapshot);
11848×
        Ok((snap, hunks))
11858×
    }
1186
1187
    /// Commit part of the working copy (spec §12.3). What is not selected
1188
    /// stays on disk as uncommitted work: the next snapshot rebuilds it
1189
    /// against the new base, so the working copy always describes the
1190
    /// directory.
1191
    /// The graph key in `st` of each text file at `paths`, the paths a
1192
    /// commit changed: only their graphs changed, so the keys the manifest
1193
    /// holds for the others still stand (and any that does not is simply
1194
    /// not trusted by the next snapshot, which compares keys). Files with
1195
    /// an `eol` attribute are left out: their bytes on disk are not the
1196
    /// rendering.
1197772×
    fn committed_graph_keys(
1198772×
        &self,
1199772×
        st: &State,
1200772×
        paths: &[String],
1201772×
        attrs: &crate::attributes::Attributes,
1202772×
    ) -> HashMap<String, [u8; 32]> {
1203772×
        let _t = crate::timing::phase("commit: graph keys");
1204772×
        let view = st.view();
1205772×
        paths
1206772×
            .iter()
12073648×
            .filter_map(|p| {
12083648×
                let e = view.entries.get(p)?;
12093596×
                (e.kind == NodeKind::File && !e.mode.opaque && attrs.lookup(p).eol.is_none())
12103596×
                    .then_some(())?;
12113526×
                Some((p.clone(), st.graph_key(&e.node)?))
12123648×
            })
1213772×
            .collect()
1214772×
    }
1215
1216761×
    pub fn commit_selection(
1217761×
        &self,
1218761×
        message: &str,
1219761×
        extra_deps: &[ChangeHash],
1220761×
        selection: &Selection,
1221761×
    ) -> Result<ChangeHash> {
1222761×
        Ok(self
1223761×
            .commit_selection_partial(message, extra_deps, selection)?
1224
            .0)
1225761×
    }
1226
1227
    /// As [`Repo::commit_selection`], and whether the commit left part of
1228
    /// the working copy uncommitted: when it did not, nothing is left, and
1229
    /// no snapshot is needed to say so.
1230790×
    pub fn commit_selection_partial(
1231790×
        &self,
1232790×
        message: &str,
1233790×
        extra_deps: &[ChangeHash],
1234790×
        selection: &Selection,
1235790×
    ) -> Result<(ChangeHash, bool)> {
1236
        // The commit's objects are written together, made durable at once
1237
        // before the operation that names them (spec §13.6).
1238790×
        let batch = self.store.begin_batch()?;
1239790×
        let out = self.commit_selection_batched(message, extra_deps, selection);
1240790×
        batch.finish()?;
1241790×
        out
1242790×
    }
1243
1244
    /// Write the manifest a commit's snapshot left to the commit, with the
1245
    /// graph keys `keys` sets.
1246543×
    fn write_pending_manifest(
1247543×
        &self,
1248543×
        mut m: PendingManifest,
1249543×
        conflicted: bool,
1250543×
        keys: HashMap<String, [u8; 32]>,
1251543×
    ) -> Result<()> {
12522895×
        for (path, k) in keys {
12532895×
            if let Some(e) = m.entries.get_mut(&path) {
12542895×
                e.graph_key = Some(k);
12552895×
            }
1256
        }
1257543×
        self.write_manifest_entries_as_of(
1258543×
            conflicted,
1259
            false,
1260543×
            &m.entries,
1261543×
            &m.blobs,
1262543×
            m.written,
1263543×
            m.dirs.unwrap_or_default(),
1264
        )
1265543×
    }
1266
1267790×
    fn commit_selection_batched(
1268790×
        &self,
1269790×
        message: &str,
1270790×
        extra_deps: &[ChangeHash],
1271790×
        selection: &Selection,
1272790×
    ) -> Result<(ChangeHash, bool)> {
1273790×
        let _wc = self.lock_working_copy()?;
1274790×
        let mut snap = match self.snapshot_from_cache()? {
1275235×
            Some(snap) => snap,
1276555×
            None => self.snapshot_as("oxy commit", true, true)?,
1277
        };
1278790×
        let pending = snap.manifest.take();
1279790×
        if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() {
12804×
            if let Some(m) = pending {
12814×
                let conflicted = snap.base_snapshot.is_conflicted();
12824×
                self.write_pending_manifest(m, conflicted, HashMap::new())?;
12830×
            }
12844×
            return fail("nothing to commit: the working copy matches its base");
1285786×
        }
1286786×
        if let Some(s) = snap
1287786×
            .statuses
1288786×
            .iter()
12893677×
            .find(|s| s.status == ChangeStatus::ConflictEdited)
1290
        {
12911×
            return fail(format!("{} still contains conflict markers", s.path));
1292785×
        }
1293785×
        if message.trim().is_empty() {
12940×
            return fail("a commit message is required");
1295785×
        }
1296
        // What the whole working copy references, before a whole commit
1297
        // takes its operations over (they are moved, not copied: for a
1298
        // large tree they are most of the memory).
1299785×
        let wc_refs = referenced_changes(&snap.wc, &snap.base);
13003675×
        let changed_paths: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
1301785×
        let (tree_ops, edits, partial) = match selection {
1302772×
            Selection::All => (
1303772×
                std::mem::take(&mut snap.wc.tree_ops),
1304772×
                std::mem::take(&mut snap.wc.edits),
1305772×
                false,
1306772×
            ),
13078×
            Selection::Paths(paths) => {
13088×
                let (o, e) = select_paths(&snap.wc, &snap.base_snapshot, paths);
13098×
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
13108×
                (o, e, partial)
1311
            }
13125×
            Selection::Hunks(indices) => {
13135×
                let (o, e) = select_hunks(&snap.wc, indices, &snap.base_snapshot.tree.unified);
13145×
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
13155×
                (o, e, partial)
1316
            }
1317
        };
1318785×
        if tree_ops.is_empty() && edits.is_empty() {
13190×
            return fail("nothing selected to commit");
1320785×
        }
1321
        // A resolution may leave other work uncommitted (oxygen#118), but
1322
        // takes every file it resolves, whole.
1323785×
        if partial && snap.resolves {
13242×
            let resolved = snap
13252×
                .statuses
13262×
                .iter()
13274×
                .filter(|s| s.status == ChangeStatus::Resolved)
13282×
                .map(|s| s.path.as_str());
13292×
            let missing: Vec<&str> = match selection {
13302×
                Selection::Paths(paths) => resolved
13312×
                    .filter(|p| {
13322×
                        !paths.iter().any(|x| {
13332×
                            p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))
13342×
                        })
13352×
                    })
13362×
                    .collect(),
13370×
                _ => resolved.collect(),
1338
            };
13392×
            if !missing.is_empty() {
13401×
                return fail(format!(
1341
                    "a conflict resolution takes every file it resolves, whole: commit by paths, including {}",
13421×
                    missing.join(", ")
1343
                ));
13441×
            }
1345783×
        }
1346
        // pre-commit hook: may veto.
1347784×
        self.run_hook(
1348784×
            "pre-commit",
1349784×
            &format!(
1350
                "{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}",
1351784×
                json_str(message),
13523673×
                json_array(snap.statuses.iter().map(|s| format!(
1353
                    "{{\"path\":{},\"status\":{}}}",
13543673×
                    json_str(&s.path),
13553673×
                    json_str(&format!("{:?}", s.status))
1356
                )))
1357
            ),
13581×
        )?;
1359783×
        let (op_ref, op) = self.head_op()?;
1360783×
        let mut deps: BTreeSet<ChangeHash> = wc_refs;
1361
        // semantic-deps hook: may add explicit dependencies (spec §8.1).
1362783×
        if self.config.semantic_deps {
13631×
            let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
13641×
            let by_node = snap.base_snapshot.tree.by_node();
13651×
            let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new();
13661×
            let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
13672×
            for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) {
13682×
                if !seen.insert(*h) {
13690×
                    continue;
13702×
                }
13712×
                let c = self.store.get_change(h)?;
13722×
                let mut paths: BTreeSet<String> = BTreeSet::new();
13736×
                for n in c
13742×
                    .tree_ops
13752×
                    .iter()
13763×
                    .map(|o| o.node())
13773×
                    .chain(c.edits.iter().map(|e| e.node()))
1378
                {
13796×
                    if let Some(e) = by_node.get(&n) {
13806×
                        paths.insert(e.path.clone());
13816×
                    }
1382
                }
13832×
                candidates.push((c.change_id, *h, paths.into_iter().collect()));
1384
            }
13851×
            for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? {
13861×
                if snap.base.contains(&d) {
13871×
                    deps.insert(d);
13881×
                }
1389
            }
1390782×
        }
1391
        // A resolution depends on every change that contributed to the
1392
        // conflict (spec §8.1). Tree and opaque conflicts are resolved through
1393
        // ordinary operations whose references already cover the contributors.
1394783×
        deps.extend(snap.contributors.iter().copied());
1395783×
        for d in extra_deps {
13964×
            if !snap.base.contains(d) {
13970×
                return fail(format!(
1398
                    "--depends-on {d}: not in the working copy's base state"
1399
                ));
14004×
            }
14014×
            deps.insert(*d);
1402
        }
1403783×
        let mut change = Change {
1404
            version: crate::objects::CHANGE_VERSION,
1405783×
            change_id: if partial {
140610×
                ChangeId::random()
1407
            } else {
1408773×
                snap.wc.change_id
1409
            },
1410783×
            deps: vec![],
1411783×
            author: self.author(),
1412783×
            hlc: Hlc::now(),
1413783×
            message: message.trim_end().to_string(),
1414783×
            tree_ops,
1415783×
            edits,
1416783×
            kind: if snap.resolves {
141716×
                ChangeKind::Resolution
1418
            } else {
1419767×
                ChangeKind::Normal
1420
            },
1421
        };
1422783×
        if partial {
1423
            // Dependencies come from what the selected part references, not
1424
            // from the whole working copy.
142510×
            deps = referenced_changes(&change, &snap.base);
142610×
            for d in extra_deps {
14271×
                deps.insert(*d);
14281×
            }
1429773×
        }
1430783×
        change.deps = deps.into_iter().collect();
1431783×
        change.normalize();
1432783×
        let signing = crate::timing::phase("commit: sign and store");
1433783×
        let hash = self.store_signed(&change)?;
1434782×
        drop(signing);
1435782×
        let extending = crate::timing::phase("commit: extend the state");
1436
1437
        // New bookmark heads: maximal elements of old heads plus the new change.
1438
        // The working copy continues on its own base plus the new change, which
1439
        // is exactly what the files on disk contain; the bookmark may hold
1440
        // other heads (and conflicts) the working copy has not checked out.
1441782×
        let old_heads = self.heads_of(&op.view, &self.config.bookmark)?;
1442782×
        let old_base = snap.base.maximal(&snap.wc.deps);
1443782×
        let mut all: Vec<ChangeHash> = old_heads.clone();
1444782×
        all.extend(old_base.iter().copied());
1445782×
        all.push(hash);
1446
        // The snapshot already loaded the working copy's base; when that also
1447
        // holds every bookmark head, it is the state to extend.
1448782×
        let mut st = if old_heads.iter().all(|h| snap.base.contains(h)) {
1449732×
            snap.base
1450
        } else {
145150×
            self.load_state(&all)?
1452
        };
1453782×
        st.add_hashed(hash, change)?;
1454782×
        let mut with_new = old_heads.clone();
1455782×
        with_new.push(hash);
1456782×
        let new_heads = st.maximal(&with_new);
1457
        // Automatic checkpoint once enough changes accumulated (spec §11).
1458782×
        let since = self
1459782×
            .latest_checkpoint(&self.config.bookmark)?
1460782×
            .map(|(_, cp)| cp.count)
1461782×
            .unwrap_or(0);
1462782×
        if self.config.checkpoint_interval > 0
1463782×
            && (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval
1464
        {
14652×
            let cp_state = self.load_state_full(&new_heads)?;
14662×
            self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?;
1467780×
        }
1468782×
        let mut wc_base = old_base.clone();
1469782×
        wc_base.push(hash);
1470782×
        let wc_base = st.maximal(&wc_base);
1471782×
        drop(extending);
1472
1473
        // The graph keys of the files as they now are, from the state the
1474
        // commit just extended: a file whose graph it built has none, and
1475
        // is read by the next snapshot; a key the next base does not share
1476
        // (the bookmark held other heads) is simply not trusted then.
1477782×
        let keying = crate::timing::phase("commit: graph keys of the files");
1478782×
        let committed_keys =
1479782×
            (!partial).then(|| self.committed_graph_keys(&st, &changed_paths, &snap.attributes));
1480782×
        drop(keying);
1481782×
        let recording = crate::timing::phase("commit: record");
1482782×
        let wc = self.new_working_copy(wc_base, "");
1483782×
        let wc_hash = self.store.put_change(&wc)?;
1484782×
        let mut view = op.view.clone();
1485782×
        view.bookmarks.insert(
1486782×
            self.config.bookmark.clone(),
1487782×
            self.store.put_state(&StateObject::new(new_heads))?,
1488
        );
1489782×
        view.workspaces.insert(self.workspace, wc_hash);
1490782×
        self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?;
1491777×
        drop(recording);
1492777×
        let rewriting = crate::timing::phase("commit: manifest");
1493777×
        if partial {
149410×
            // The rest is rebuilt from disk by the next snapshot.
149510×
            self.drop_manifest();
149610×
        } else {
1497767×
            let conflicted = snap.base_snapshot.is_conflicted() && !snap.resolves;
1498
            // The files on disk are now exactly the new base's renderings:
1499
            // record their graph keys, so the next snapshot neither reads nor
1500
            // renders them (oxygen#35).
1501767×
            let keys = if conflicted {
15020×
                HashMap::new()
1503
            } else {
1504767×
                committed_keys.unwrap_or_default()
1505
            };
1506767×
            match pending {
1507539×
                Some(m) => self.write_pending_manifest(m, conflicted, keys)?,
1508228×
                None => self.rewrite_manifest(conflicted, false, &keys)?,
1509
            }
1510
        }
1511777×
        drop(rewriting);
1512777×
        let packing = crate::timing::phase("commit: autopack");
1513777×
        self.maybe_autopack()?;
1514777×
        drop(packing);
1515777×
        let _hook = crate::timing::phase("commit: hook");
1516777×
        let _ = self.run_hook(
1517777×
            "post-commit",
1518777×
            &format!(
1519777×
                "{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}",
1520777×
                json_str(&snap.wc.change_id.to_hex()),
1521777×
                json_str(&hash.to_hex()),
1522777×
                json_str(message)
1523777×
            ),
1524777×
        );
1525777×
        Ok((hash, partial))
1526790×
    }
1527
}
1528
1529
/// What part of the working copy a commit takes (spec §12.3).
1530
#[derive(Clone, Debug)]
1531
pub enum Selection {
1532
    All,
1533
    /// Every operation and edit on nodes under these paths.
1534
    Paths(Vec<String>),
1535
    /// Hunk indices as `Repo::hunks` numbers them; dependent hunks are added.
1536
    Hunks(Vec<usize>),
1537
}
1538
1539
/// One selectable piece of the working copy: a tree operation or an edit.
1540
#[derive(Clone, Debug)]
1541
pub struct Hunk {
1542
    pub index: usize,
1543
    pub path: String,
1544
    /// Rendered like `oxy diff`.
1545
    pub text: String,
1546
}
1547
1548
/// The tree operations and edits on nodes under `paths`, plus the
1549
/// additions of any new parent directories they need.
15508×
fn select_paths(
15518×
    wc: &Change,
15528×
    base_snapshot: &Snapshot,
15538×
    paths: &[String],
15548×
) -> (Vec<TreeOp>, Vec<Edit>) {
155527×
    let selected = |p: &str| {
155627×
        paths
155727×
            .iter()
155827×
            .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
155927×
    };
15608×
    let by_node = base_snapshot.tree.by_node();
15618×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
15628×
    for op in &wc.tree_ops {
1563
        if let TreeOp::AddNode {
15645×
            node,
15655×
            parent,
15665×
            name,
15675×
            kind,
1568
            ..
15696×
        } = op
15705×
        {
15715×
            new_names.insert(*node, (*parent, name.clone(), *kind));
15725×
        }
1573
    }
15748×
    let mut nodes: BTreeSet<NodeId> = BTreeSet::new();
157528×
    for n in wc
15768×
        .tree_ops
15778×
        .iter()
15788×
        .map(|o| o.node())
157922×
        .chain(wc.edits.iter().map(|e| e.node()))
1580
    {
158128×
        if path_of_node(&n, &by_node, &new_names).is_some_and(|p| selected(&p)) {
158214×
            nodes.insert(n);
158314×
        }
1584
    }
1585
    // The nodes shown as one with a selected node are retired with it.
15868×
    for (dup, shown) in &base_snapshot.tree.unified {
15871×
        if nodes.contains(shown) {
15881×
            nodes.insert(*dup);
15891×
        }
1590
    }
1591
    // New parents of selected new nodes come along.
15928×
    let mut changed = true;
159316×
    while changed {
15948×
        changed = false;
15958×
        for (n, (parent, _, _)) in &new_names {
15965×
            if nodes.contains(n) && new_names.contains_key(parent) && nodes.insert(*parent) {
15970×
                changed = true;
15985×
            }
1599
        }
1600
    }
16018×
    let ops = wc
16028×
        .tree_ops
16038×
        .iter()
16048×
        .filter(|o| nodes.contains(&o.node()))
16058×
        .cloned()
16068×
        .collect();
16078×
    let edits = wc
16088×
        .edits
16098×
        .iter()
161022×
        .filter(|e| nodes.contains(&e.node()))
16118×
        .cloned()
16128×
        .collect();
16138×
    (ops, edits)
16148×
}
1615
1616
/// The hunks `indices` name plus whatever they refer to within the working
1617
/// copy (the parent directory of an added node, the insert that introduced
1618
/// an anchor line), renumbered so the result is a well-formed change.
1619
/// The nodes `unified` shows as one with another (a base's
1620
/// [`TreeView::unified`](crate::tree::TreeView::unified)) are retired with
1621
/// any hunk of the node shown.
16226×
fn select_hunks(
16236×
    wc: &Change,
16246×
    indices: &[usize],
16256×
    unified: &BTreeMap<NodeId, NodeId>,
16266×
) -> (Vec<TreeOp>, Vec<Edit>) {
16276×
    let n_ops = wc.tree_ops.len();
16286×
    let mut ops_sel = vec![false; n_ops];
16296×
    let mut edits_sel = vec![false; wc.edits.len()];
16306×
    for &i in indices {
16315×
        if i < n_ops {
16321×
            ops_sel[i] = true;
16334×
        } else if i - n_ops < wc.edits.len() {
16344×
            edits_sel[i - n_ops] = true;
16354×
        }
1636
    }
1637
    // Atom ranges each insert introduces, per node, in the working copy's numbering.
16386×
    let mut per_node: HashMap<NodeId, u32> = HashMap::new();
16396×
    let mut insert_ranges: Vec<Option<(NodeId, u32, u32)>> = Vec::new();
164012×
    for e in &wc.edits {
164112×
        insert_ranges.push(match e {
164212×
            Edit::Insert { node, lines, .. } => {
164312×
                let start = per_node.entry(*node).or_default();
164412×
                let r = (*node, *start, *start + lines.len() as u32);
164512×
                *start += lines.len() as u32;
164612×
                Some(r)
1647
            }
16480×
            _ => None,
1649
        });
1650
    }
16516×
    let add_index: HashMap<NodeId, usize> = wc
16526×
        .tree_ops
16536×
        .iter()
16546×
        .enumerate()
165510×
        .filter_map(|(i, o)| {
165610×
            if let TreeOp::AddNode { node, .. } = o {
16579×
                Some((*node, i))
1658
            } else {
16591×
                None
1660
            }
166110×
        })
16626×
        .collect();
16636×
    let insert_for = |node: &NodeId, index: u32| -> Option<usize> {
16640×
        insert_ranges
16650×
            .iter()
16660×
            .position(|r| matches!(r, Some((n, a, b)) if n == node && *a <= index && index < *b))
16670×
    };
1668
    // Closure.
16696×
    let mut changed = true;
167015×
    while changed {
16719×
        changed = false;
167219×
        for i in 0..n_ops {
167319×
            if !ops_sel[i] {
167413×
                continue;
16756×
            }
16766×
            if let TreeOp::AddNode { node, parent, .. } = &wc.tree_ops[i] {
16776×
                if let Some(&p) = add_index.get(parent) {
16784×
                    if !ops_sel[p] {
16792×
                        ops_sel[p] = true;
16802×
                        changed = true;
16812×
                    }
16822×
                }
1683
                // A new node's content belongs with its addition: nobody
1684
                // wants an empty file committed apart from its lines.
168512×
                for (j, e) in wc.edits.iter().enumerate() {
168612×
                    if e.node() == *node && !edits_sel[j] {
16871×
                        edits_sel[j] = true;
16881×
                        changed = true;
168911×
                    }
1690
                }
16910×
            }
1692
        }
169318×
        for j in 0..wc.edits.len() {
169418×
            if !edits_sel[j] {
169510×
                continue;
16968×
            }
16978×
            let e = &wc.edits[j];
16988×
            if let Some(&a) = add_index.get(&e.node()) {
16995×
                if !ops_sel[a] {
17001×
                    ops_sel[a] = true;
17011×
                    changed = true;
17024×
                }
17033×
            }
1704
            if let Edit::Insert {
17058×
                node,
17068×
                after,
17078×
                before,
1708
                ..
17098×
            } = e
1710
            {
171116×
                for r in [after, before] {
171216×
                    if let AtomRef::Atom { change, index } = r {
17133×
                        if change.is_zero() {
17140×
                            if let Some(k) = insert_for(node, *index) {
17150×
                                if !edits_sel[k] {
17160×
                                    edits_sel[k] = true;
17170×
                                    changed = true;
17180×
                                }
17190×
                            }
17203×
                        }
172113×
                    }
1722
                }
17230×
            }
1724
        }
1725
    }
1726
    // The retirement of a node shown as one with a selected node.
17276×
    if !unified.is_empty() {
17281×
        let chosen: HashSet<NodeId> = (0..n_ops)
17292×
            .filter(|i| ops_sel[*i])
17301×
            .map(|i| wc.tree_ops[i].node())
17311×
            .chain(
17321×
                (0..wc.edits.len())
17332×
                    .filter(|j| edits_sel[*j])
17341×
                    .map(|j| wc.edits[j].node()),
1735
            )
17361×
            .collect();
17372×
        for (i, o) in wc.tree_ops.iter().enumerate() {
17382×
            if let TreeOp::RemoveNode { node, .. } = o {
17391×
                if unified.get(node).is_some_and(|s| chosen.contains(s)) {
17401×
                    ops_sel[i] = true;
17411×
                }
17421×
            }
1743
        }
17445×
    }
1745
    // Renumber self-references: old atom index → new index among selected inserts.
17466×
    let mut remap: HashMap<(NodeId, u32), u32> = HashMap::new();
17476×
    let mut new_counts: HashMap<NodeId, u32> = HashMap::new();
174812×
    for (j, r) in insert_ranges.iter().enumerate() {
174912×
        if let (true, Some((node, a, b))) = (edits_sel[j], r) {
17505×
            let next = new_counts.entry(*node).or_default();
17515×
            for old in *a..*b {
17525×
                remap.insert((*node, old), *next);
17535×
                *next += 1;
17545×
            }
17557×
        }
1756
    }
175710×
    let map_ref = |r: AtomRef, node: &NodeId| -> AtomRef {
17583×
        match r {
17593×
            AtomRef::Atom { change, index } if change.is_zero() => AtomRef::Atom {
17600×
                change,
17610×
                index: remap.get(&(*node, index)).copied().unwrap_or(index),
17620×
            },
176310×
            other => other,
1764
        }
176510×
    };
17666×
    let ops = wc
17676×
        .tree_ops
17686×
        .iter()
17696×
        .enumerate()
177010×
        .filter(|(i, _)| ops_sel[*i])
17716×
        .map(|(_, o)| o.clone())
17726×
        .collect();
17736×
    let edits = wc
17746×
        .edits
17756×
        .iter()
17766×
        .enumerate()
177712×
        .filter(|(j, _)| edits_sel[*j])
17786×
        .map(|(_, e)| match e {
1779
            Edit::Insert {
17805×
                node,
17815×
                after,
17825×
                before,
17835×
                lines,
17845×
            } => Edit::Insert {
17855×
                node: *node,
17865×
                after: map_ref(*after, node),
17875×
                before: map_ref(*before, node),
17885×
                lines: lines.clone(),
17895×
            },
17900×
            other => other.clone(),
17915×
        })
17926×
        .collect();
17936×
    (ops, edits)
17946×
}
1795
1796
/// Render each tree operation and edit of `change` on its own, numbered.
17978×
pub fn render_hunks(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<Hunk> {
17988×
    let by_node = base_snapshot.tree.by_node();
17998×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
180010×
    for op in &change.tree_ops {
1801
        if let TreeOp::AddNode {
18029×
            node,
18039×
            parent,
18049×
            name,
18059×
            kind,
1806
            ..
180710×
        } = op
18089×
        {
18099×
            new_names.insert(*node, (*parent, name.clone(), *kind));
18109×
        }
1811
    }
18128×
    let mut out = Vec::new();
181310×
    for (i, op) in change.tree_ops.iter().enumerate() {
1814
        // A retired duplicate goes with the node shown: no hunk of its own.
181510×
        if is_unified_retirement(op, base_snapshot) {
18161×
            continue;
18179×
        }
18189×
        let one = Change {
18199×
            tree_ops: vec![op.clone()],
18209×
            edits: vec![],
18219×
            ..change.clone()
18229×
        };
18239×
        let path = path_of_node(&op.node(), &by_node, &new_names).unwrap_or_default();
18249×
        out.push(Hunk {
18259×
            index: i,
18269×
            path,
18279×
            text: render_change(&one, base, base_snapshot),
18289×
        });
1829
    }
183015×
    for (j, e) in change.edits.iter().enumerate() {
183115×
        let one = Change {
183215×
            tree_ops: vec![],
183315×
            edits: vec![e.clone()],
183415×
            ..change.clone()
183515×
        };
183615×
        let path = path_of_node(&e.node(), &by_node, &new_names).unwrap_or_default();
183715×
        out.push(Hunk {
183815×
            index: change.tree_ops.len() + j,
183915×
            path,
184015×
            text: render_change(&one, base, base_snapshot),
184115×
        });
184215×
    }
18438×
    out
18448×
}
1845
1846
/// One file's part of a diff, for `oxy diff --json`.
1847
#[derive(Clone, Debug)]
1848
pub struct FileDiff {
1849
    pub path: String,
1850
    /// `added`, `removed`, `modified`, `renamed`, `mode`, `replaced`.
1851
    pub kind: String,
1852
    pub hunks: Vec<DiffHunk>,
1853
    /// For renames, the previous path.
1854
    pub from: Option<String>,
1855
}
1856
1857
/// Lines inserted after `base_line` (0 = before the first line) and lines
1858
/// removed, with their base line numbers.
1859
#[derive(Clone, Debug, Default)]
1860
pub struct DiffHunk {
1861
    pub base_line: u32,
1862
    pub added: Vec<Vec<u8>>,
1863
    pub removed: Vec<(u32, Vec<u8>)>,
1864
}
1865
1866
impl Repo {
1867
    /// The content of `path` in a state: `heads`, or the working copy's base.
186827×
    pub fn file_at(&self, path: &str, heads: Option<&[ChangeHash]>) -> Result<Vec<u8>> {
1869
        // The state's history and tree, then the one file's graph: nothing
1870
        // else is built or rendered (oxygen#92).
1871
        // The working copy's base, and a state named by its heads (`-r
1872
        // main` on main), come from the workspace's state index.
187327×
        let base = if self.is_bare() {
18745×
            None
1875
        } else {
187622×
            Some(self.working_copy()?.1.deps)
1877
        };
187827×
        let same = |a: &[ChangeHash], b: &[ChangeHash]| {
18791×
            let (mut a, mut b) = (a.to_vec(), b.to_vec());
18801×
            a.sort();
18811×
            b.sort();
18821×
            a == b
18831×
        };
188427×
        let mut state = match (heads, base) {
18851×
            (Some(h), Some(b)) if same(h, &b) => self.load_state_indexed(&b)?,
18865×
            (Some(h), _) => self.load_state_history(h)?,
188721×
            (None, Some(b)) => self.load_state_indexed(&b)?,
18880×
            (None, None) => return fail("this is a bare repository: it has no working copy"),
1889
        };
189027×
        let view = self.tree_view(&mut state)?;
189127×
        let Some(node) = view.entries.get(path).map(|e| e.node) else {
18920×
            return fail(format!("{path} is not in that state"));
1893
        };
189427×
        self.load_graphs_of(&mut state, &[node])?;
189526×
        let snapshot = state.materialize_paths(view, [path]);
189626×
        let Some(f) = snapshot.files.get(path) else {
18970×
            return fail(format!("{path} is not in that state"));
1898
        };
189926×
        match &f.content {
19000×
            FileContent::Dir => fail(format!("{path} is a directory")),
190126×
            FileContent::Text { bytes, .. } => Ok(bytes.clone()),
19020×
            FileContent::Opaque { blobs } => match blobs.first() {
19030×
                Some(b) => self.read_blob(b),
19040×
                None => Ok(vec![]),
1905
            },
1906
        }
190727×
    }
1908
1909
    /// Restore `paths` (files or directories) to the working copy's base:
1910
    /// edits are undone, additions removed, removals put back. Returns the
1911
    /// number of paths touched.
19122×
    pub fn discard(&self, paths: &[String]) -> Result<usize> {
19132×
        let _wc = self.lock_working_copy()?;
19142×
        let snap = self.snapshot_status("oxy discard")?;
19157×
        let under = |p: &str| {
19167×
            paths
19177×
                .iter()
19187×
                .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
19197×
        };
19202×
        let attrs = Repo::state_attributes(&snap.base_snapshot);
19212×
        let mut n = 0;
19223×
        for s in &snap.statuses {
19233×
            if !under(&s.path) {
19241×
                continue;
19252×
            }
19262×
            match snap.base_snapshot.files.get(&s.path) {
1927
                None => {
1928
                    // Added (or the new name of a rename): remove it.
19291×
                    let full = self.root.join(&s.path);
19301×
                    if full.is_dir() {
19310×
                        let _ = fs::remove_dir_all(&full);
19321×
                    } else {
19331×
                        let _ = fs::remove_file(&full);
19341×
                    }
19351×
                    n += 1;
1936
                }
19371×
                Some(f) => {
19381×
                    match &f.content {
19390×
                        FileContent::Dir => fs::create_dir_all(self.root.join(&s.path))?,
19401×
                        FileContent::Text { bytes, .. } => {
19411×
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&s.path).eol
19421×
                                == Some(crate::attributes::Eol::Crlf)
1943
                            {
19440×
                                crate::attributes::to_crlf(bytes).into()
1945
                            } else {
19461×
                                bytes.as_slice().into()
1947
                            };
19481×
                            self.write_file(&s.path, &on_disk, f.mode.exec)?;
1949
                        }
19500×
                        FileContent::Opaque { blobs } => {
19510×
                            let bytes = match blobs.first() {
19520×
                                Some(b) => self.read_blob(b)?,
19530×
                                None => vec![],
1954
                            };
19550×
                            if f.kind == NodeKind::Symlink {
19560×
                                self.write_symlink(&s.path, &bytes)?;
19570×
                            } else if f.kind == NodeKind::File {
19580×
                                self.write_file(&s.path, &bytes, f.mode.exec)?;
19590×
                            }
1960
                        }
1961
                    }
19621×
                    n += 1;
1963
                }
1964
            }
1965
        }
1966
        // A rename's old name is not in the statuses; put it back too.
19674×
        for (p, f) in &snap.base_snapshot.files {
19684×
            if under(p)
19691×
                && !self.root.join(p).exists()
19700×
                && f.kind != NodeKind::Dir
19710×
                && f.kind != NodeKind::Subrepo
19720×
                && !snap.statuses.iter().any(|s| s.path == *p)
1973
            {
19740×
                let bytes = match &f.content {
19750×
                    FileContent::Text { bytes, .. } => bytes.clone(),
19760×
                    FileContent::Opaque { blobs } => blobs
19770×
                        .first()
19780×
                        .map(|b| self.read_blob(b))
19790×
                        .transpose()?
19800×
                        .unwrap_or_default(),
19810×
                    FileContent::Dir => continue,
1982
                };
19830×
                if f.kind == NodeKind::Symlink {
19840×
                    self.write_symlink(p, &bytes)?
1985
                } else {
19860×
                    self.write_file(p, &bytes, f.mode.exec)?
1987
                }
19880×
                n += 1;
19894×
            }
1990
        }
19912×
        self.drop_manifest();
19922×
        Ok(n)
19932×
    }
1994
1995
    /// Undo the working-copy hunks `indices` (as `hunks` numbers them) on
1996
    /// disk, keeping the others: the files are rewritten from the base plus
1997
    /// the remaining hunks.
19981×
    pub fn discard_hunks(&self, indices: &[usize]) -> Result<usize> {
19991×
        let _wc = self.lock_working_copy()?;
20001×
        let (snap, hunks) = self.hunks()?;
20011×
        let keep: Vec<usize> = hunks
20021×
            .iter()
20031×
            .map(|h| h.index)
20041×
            .filter(|i| !indices.contains(i))
20051×
            .collect();
20061×
        let (ops, edits) = select_hunks(&snap.wc, &keep, &snap.base_snapshot.tree.unified);
20071×
        let dropped_nodes: BTreeSet<NodeId> = hunks
20081×
            .iter()
20091×
            .filter(|h| indices.contains(&h.index))
20101×
            .map(|h| {
20111×
                if h.index < snap.wc.tree_ops.len() {
20120×
                    snap.wc.tree_ops[h.index].node()
2013
                } else {
20141×
                    snap.wc.edits[h.index - snap.wc.tree_ops.len()].node()
2015
                }
20161×
            })
20171×
            .collect();
20181×
        let mut partial = snap.wc.clone();
20191×
        partial.tree_ops = ops;
20201×
        partial.edits = edits;
20211×
        partial.normalize();
20221×
        let mut st = snap.base.clone();
20231×
        st.add_unchecked(partial);
2024
        // Every file of the target is rendered: build the graphs the
2025
        // partial snapshot left out.
20261×
        self.load_all_graphs(&mut st)?;
20271×
        let target = st.materialize();
20281×
        let by_node_target = target.tree.by_node();
20291×
        let by_node_base = snap.base_snapshot.tree.by_node();
20301×
        let attrs = Repo::state_attributes(&target);
20311×
        let mut n = 0;
20321×
        for node in dropped_nodes {
20331×
            match by_node_target.get(&node) {
20341×
                Some(e) => {
20351×
                    let f = &target.files[&e.path];
20361×
                    match &f.content {
20370×
                        FileContent::Dir => {}
20381×
                        FileContent::Text { bytes, .. } => {
20391×
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&e.path).eol
20401×
                                == Some(crate::attributes::Eol::Crlf)
2041
                            {
20420×
                                crate::attributes::to_crlf(bytes).into()
2043
                            } else {
20441×
                                bytes.as_slice().into()
2045
                            };
20461×
                            self.write_file(&e.path, &on_disk, f.mode.exec)?;
2047
                        }
20480×
                        FileContent::Opaque { blobs } => {
20490×
                            let bytes = blobs
20500×
                                .first()
20510×
                                .map(|b| self.read_blob(b))
20520×
                                .transpose()?
20530×
                                .unwrap_or_default();
20540×
                            if f.kind == NodeKind::Symlink {
20550×
                                self.write_symlink(&e.path, &bytes)?
2056
                            } else {
20570×
                                self.write_file(&e.path, &bytes, f.mode.exec)?
2058
                            }
2059
                        }
2060
                    }
2061
                    // A discarded rename: the new name goes.
20621×
                    if let Some(old_path) = snap
20631×
                        .statuses
20641×
                        .iter()
20651×
                        .find(|s| {
20661×
                            s.status == ChangeStatus::Renamed && !target.files.contains_key(&s.path)
20671×
                        })
20681×
                        .map(|s| s.path.clone())
20690×
                    {
20700×
                        let _ = fs::remove_file(self.root.join(old_path));
20711×
                    }
20721×
                    n += 1;
2073
                }
2074
                None => {
2075
                    // The node exists in neither target nor base with a live path: an addition to remove.
20760×
                    if let Some(added) = snap.statuses.iter().find(|s| {
20770×
                        s.status == ChangeStatus::Added
20780×
                            && !target.files.contains_key(&s.path)
20790×
                            && !by_node_base.contains_key(&node)
20800×
                    }) {
20810×
                        let full = self.root.join(&added.path);
20820×
                        if full.is_dir() {
20830×
                            let _ = fs::remove_dir_all(&full);
20840×
                        } else {
20850×
                            let _ = fs::remove_file(&full);
20860×
                        }
20870×
                        n += 1;
20880×
                    }
2089
                }
2090
            }
2091
        }
20921×
        self.drop_manifest();
20931×
        Ok(n)
20941×
    }
2095
2096
    /// The diff of the working copy (`None`) or of a change, per file with
2097
    /// line-numbered hunks (spec: `oxy diff --json`).
20987×
    pub fn diff_files(&self, change: Option<&ChangeHash>) -> Result<Vec<FileDiff>> {
20997×
        let (c, base, base_snapshot) = match change {
21003×
            Some(h) => {
21013×
                let c = self.store.get_change(h)?;
21023×
                let (base, snapshot) = self.change_base(&c)?;
21033×
                (c, base, snapshot)
2104
            }
21054×
            None => match self.working_copy_with_base_unless_unchanged()? {
21063×
                Some(all) => all,
21071×
                None => return Ok(Vec::new()),
2108
            },
2109
        };
21106×
        Ok(diff_files_of(&c, &base, &base_snapshot))
21117×
    }
2112
2113
    /// The snapshot a command would take, without taking it, when the
2114
    /// status cache holds: every file is as the last snapshot left it, so
2115
    /// that snapshot's working-copy change is the answer. Its base comes
2116
    /// from the state index, the `.oxyattributes` files from disk. `None`
2117
    /// when a snapshot must be taken, and when there is nothing to commit
2118
    /// (the snapshot then says so). A conflicted base never gets here: the
2119
    /// cache does not hold for one.
2120791×
    pub(crate) fn snapshot_from_cache(&self) -> Result<Option<SnapshotResult>> {
2121791×
        let Some(statuses) = self.status_by_manifest()? else {
2122552×
            return Ok(None);
2123
        };
2124239×
        if statuses.is_empty() {
21253×
            return Ok(None);
2126236×
        }
2127236×
        let _t = crate::timing::phase("snapshot: from the status cache");
2128236×
        let (wc_hash, wc) = self.working_copy()?;
2129236×
        let base = self.load_state_indexed(&wc.deps)?;
2130236×
        let (base, base_snapshot) = self.render_base_for(&wc, base)?;
2131236×
        let attribute_files: Vec<(String, Vec<u8>)> = match self.read_manifest()? {
2132236×
            Some((_, _, m)) => m
2133236×
                .entries
2134236×
                .keys()
2135720×
                .filter(|p| is_attributes(p))
2136236×
                .filter_map(|p| fs::read(self.root.join(p)).ok().map(|b| (p.clone(), b)))
2137236×
                .collect(),
21380×
            None => Vec::new(),
2139
        };
2140236×
        let attributes = crate::attributes::Attributes::from_files(
2141236×
            attribute_files
2142236×
                .iter()
2143236×
                .map(|(p, b)| (p.as_str(), b.as_slice())),
2144
        );
2145236×
        Ok(Some(SnapshotResult {
2146236×
            wc_hash,
2147236×
            wc,
2148236×
            base,
2149236×
            base_snapshot,
2150236×
            statuses,
2151236×
            changed: false,
2152236×
            resolves: false,
2153236×
            contributors: Vec::new(),
2154236×
            attributes,
2155236×
            untracked_nested: self.untracked_nested.borrow().clone(),
2156236×
            manifest: None,
2157236×
        }))
2158791×
    }
2159
2160
    /// The working-copy change with its base state and a snapshot of the
2161
    /// base, for showing what the working copy changes ([`render_change`],
2162
    /// [`diff_files_of`]). While the status cache holds (every file as the
2163
    /// last snapshot left it), the stored working-copy change is the
2164
    /// answer, no snapshot is taken, and the base's snapshot holds its tree
2165
    /// but no file; otherwise a snapshot makes all three.
21662×
    pub fn working_copy_with_base(&self) -> Result<(Change, State, Snapshot)> {
21672×
        match self.working_copy_with_base_unless_unchanged()? {
21681×
            Some(all) => Ok(all),
2169
            None => {
21701×
                let (_, wc) = self.working_copy()?;
21711×
                let base = self.load_state_indexed(&wc.deps)?;
21721×
                let (base, snapshot) = self.tree_base_for(&wc, base)?;
21731×
                Ok((wc, base, snapshot))
2174
            }
2175
        }
21762×
    }
2177
2178
    /// [`Repo::working_copy_with_base`], or `None` when the status cache
2179
    /// holds and the working-copy change changes nothing: its diff is
2180
    /// empty, and the base is not loaded for it (oxygen#94).
21817×
    pub fn working_copy_with_base_unless_unchanged(
21827×
        &self,
21837×
    ) -> Result<Option<(Change, State, Snapshot)>> {
21847×
        if self.status_by_manifest()?.is_some() {
21855×
            let (_, wc) = self.working_copy()?;
21865×
            if wc.tree_ops.is_empty() && wc.edits.is_empty() {
21873×
                return Ok(None);
21882×
            }
21892×
            let base = self.load_state_indexed(&wc.deps)?;
21902×
            let (base, snapshot) = self.tree_base_for(&wc, base)?;
21912×
            return Ok(Some((wc, base, snapshot)));
21922×
        }
21932×
        let snap = self.snapshot_status("oxy diff")?;
21942×
        Ok(Some((snap.wc, snap.base, snap.base_snapshot)))
21957×
    }
2196
2197
    /// The log restricted to changes touching `path` (its node, so renames
2198
    /// are followed), on the current bookmark.
21996×
    pub fn log_for_path(&self, path: &str) -> Result<Vec<LogEntry>> {
22006×
        let heads = self.bookmark_heads()?;
22016×
        if self.is_bare() {
22021×
            return self.log_for_path_in(path, &heads);
22035×
        }
22045×
        let entries = self.log()?;
22055×
        let (_, wc) = self.working_copy()?;
2206
        // The path's node from the trees alone: the base's (from the
2207
        // workspace's state index), else the bookmark's. Nothing is
2208
        // rendered.
22095×
        let node_in = |mut state: State| -> Result<Option<HashSet<NodeId>>> {
22105×
            Ok(nodes_at(&self.tree_view(&mut state)?, path))
22115×
        };
22125×
        let nodes = match node_in(self.load_state_indexed(&wc.deps)?)? {
22135×
            Some(n) => Some(n),
22140×
            None => node_in(self.load_state_history(&heads)?)?,
2215
        };
22165×
        let Some(nodes) = nodes else {
22170×
            return fail(format!("{path} is not tracked"));
2218
        };
22195×
        Ok(self.entries_touching_up_to(entries, nodes, None))
22206×
    }
2221
2222
    /// The log of the state with these heads restricted to changes touching
2223
    /// `path` (its node, so renames are followed).
22242×
    pub fn log_for_path_in(&self, path: &str, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> {
22252×
        self.log_for_path_of(&self.load_state_history(heads)?, path, None)
22262×
    }
2227
2228
    /// The changes of a loaded state touching `path`, newest first, at most
2229
    /// `limit` of them. The path is found in the state's tree alone, so no
2230
    /// file is rendered, and the walk stops once `limit` are found.
22315×
    pub fn log_for_path_of(
22325×
        &self,
22335×
        state: &State,
22345×
        path: &str,
22355×
        limit: Option<usize>,
22365×
    ) -> Result<Vec<LogEntry>> {
22375×
        let Some(nodes) = nodes_at(&state.view(), path) else {
22381×
            return fail(format!("{path} is not in that state"));
2239
        };
22404×
        Ok(self.entries_touching_up_to(self.log_of(state), nodes, limit))
22415×
    }
2242
22439×
    fn entries_touching_up_to(
22449×
        &self,
22459×
        entries: Vec<LogEntry>,
22469×
        wanted: HashSet<NodeId>,
22479×
        limit: Option<usize>,
22489×
    ) -> Vec<LogEntry> {
22499×
        let mut touched = self.touched();
22509×
        if limit.is_none() {
22518×
            let hashes: Vec<ChangeHash> = entries.iter().map(|e| e.hash).collect();
22528×
            touched.fill(self, &hashes);
22531×
        }
22549×
        let mut out = Vec::new();
225532×
        for e in entries {
225632×
            if limit.is_some_and(|n| out.len() >= n) {
22571×
                break;
225831×
            }
225931×
            if touched.touches(self, &e.hash, &wanted) {
226020×
                out.push(e);
226120×
            }
2262
        }
22639×
        touched.save();
22649×
        out
22659×
    }
2266
}
2267
2268
/// The node at `path` in `view`, with the nodes it shows as one with it
2269
/// (identical additions, spec §9.2): the file's history is all of theirs.
227010×
fn nodes_at(view: &crate::tree::TreeView, path: &str) -> Option<HashSet<NodeId>> {
227110×
    let node = view.entries.get(path)?.node;
22729×
    let mut out = HashSet::from([node]);
22739×
    out.extend(
22749×
        view.unified
22759×
            .iter()
22769×
            .filter(|(_, shown)| **shown == node)
22779×
            .map(|(dup, _)| *dup),
2278
    );
22799×
    Some(out)
228010×
}
2281
2282
/// Per-file hunks of `change` against `base`.
22836×
pub fn diff_files_of(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<FileDiff> {
22846×
    let by_node = base_snapshot.tree.by_node();
22856×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
22866×
    for op in &change.tree_ops {
2287
        if let TreeOp::AddNode {
22882×
            node,
22892×
            parent,
22902×
            name,
22912×
            kind,
2292
            ..
22933×
        } = op
22942×
        {
22952×
            new_names.insert(*node, (*parent, name.clone(), *kind));
22962×
        }
2297
    }
229810×
    let path_of = |n: &NodeId| -> String {
229910×
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
230010×
    };
23016×
    let mut files: BTreeMap<NodeId, FileDiff> = BTreeMap::new();
23026×
    let mut order: Vec<NodeId> = Vec::new();
230315×
    let mut entry = |files: &mut BTreeMap<NodeId, FileDiff>, n: NodeId, kind: &str| {
230415×
        if let std::collections::btree_map::Entry::Vacant(v) = files.entry(n) {
230510×
            order.push(n);
230610×
            v.insert(FileDiff {
230710×
                path: path_of(&n),
230810×
                kind: kind.to_string(),
230910×
                hunks: vec![],
231010×
                from: None,
231110×
            });
231210×
        }
231315×
    };
23146×
    for op in change
23156×
        .tree_ops
23166×
        .iter()
23176×
        .filter(|o| !is_unified_retirement(o, base_snapshot))
2318
    {
23193×
        match op {
23202×
            TreeOp::AddNode { node, kind, .. } => {
23212×
                if *kind != NodeKind::Dir {
23222×
                    entry(&mut files, *node, "added");
23232×
                }
2324
            }
23251×
            TreeOp::RemoveNode { node, .. } => {
23261×
                entry(&mut files, *node, "removed");
23271×
                if let Some(e) = by_node.get(node) {
23281×
                    files.get_mut(node).unwrap().path = e.path.clone();
23291×
                }
2330
            }
2331
            TreeOp::Move {
23320×
                node, parent, name, ..
2333
            } => {
23340×
                entry(&mut files, *node, "renamed");
23350×
                let f = files.get_mut(node).unwrap();
23360×
                f.from = by_node.get(node).map(|e| e.path.clone());
23370×
                let dir = if parent.is_root() {
23380×
                    String::new()
2339
                } else {
23400×
                    path_of_node(parent, &by_node, &new_names).unwrap_or_default()
2341
                };
23420×
                f.path = if dir.is_empty() {
23430×
                    name.clone()
2344
                } else {
23450×
                    format!("{dir}/{name}")
2346
                };
2347
            }
23480×
            TreeOp::SetMode { node, .. } => entry(&mut files, *node, "mode"),
2349
        }
2350
    }
2351
    // Base line numbers per node, 1-based.
23526×
    let mut line_of: HashMap<NodeId, HashMap<(ChangeHash, u32), u32>> = HashMap::new();
235312×
    let mut lines_for = |n: &NodeId| -> HashMap<(ChangeHash, u32), u32> {
235412×
        line_of
235512×
            .entry(*n)
235612×
            .or_insert_with(|| {
23579×
                base.live_lines(n)
23589×
                    .into_iter()
23599×
                    .enumerate()
236012×
                    .map(|(i, (k, _))| (k, i as u32 + 1))
23619×
                    .collect()
23629×
            })
236312×
            .clone()
236412×
    };
236512×
    for e in &change.edits {
236612×
        let n = e.node();
236712×
        let is_new = new_names.contains_key(&n);
236812×
        entry(&mut files, n, if is_new { "added" } else { "modified" });
236912×
        let f = files.get_mut(&n).unwrap();
237012×
        match e {
237110×
            Edit::Insert { after, lines, .. } => {
237210×
                let map = lines_for(&n);
237310×
                let base_line = match after {
23743×
                    AtomRef::Start => 0,
23750×
                    AtomRef::End => map.len() as u32,
23767×
                    AtomRef::Atom { change, index } if change.is_zero() => {
23770×
                        f.hunks.last().map(|h| h.base_line).unwrap_or(0)
2378
                    }
23797×
                    AtomRef::Atom { change, index } => {
23807×
                        map.get(&(*change, *index)).copied().unwrap_or(0)
2381
                    }
2382
                };
238310×
                f.hunks.push(DiffHunk {
238410×
                    base_line,
238510×
                    added: lines.to_vec(),
238610×
                    removed: vec![],
238710×
                });
2388
            }
23892×
            Edit::Delete { atoms, .. } => {
23902×
                let map = lines_for(&n);
23912×
                let mut removed = Vec::new();
23922×
                for r in atoms {
23932×
                    for i in r.from..r.to {
23942×
                        let line = map.get(&(r.change, i)).copied().unwrap_or(0);
23952×
                        let text = base
23962×
                            .file(&n)
23972×
                            .and_then(|g| g.content(&(r.change, i)))
23982×
                            .unwrap_or(b"")
23992×
                            .to_vec();
24002×
                        removed.push((line, text));
2401
                    }
2402
                }
24032×
                let base_line = removed
24042×
                    .first()
24052×
                    .map(|(l, _)| l.saturating_sub(1))
24062×
                    .unwrap_or(0);
24072×
                f.hunks.push(DiffHunk {
24082×
                    base_line,
24092×
                    added: vec![],
24102×
                    removed,
24112×
                });
2412
            }
2413
            Edit::Replace { .. } => {
24140×
                if !is_new {
24150×
                    f.kind = "replaced".into();
24160×
                }
2417
            }
2418
        }
2419
    }
242010×
    order.into_iter().filter_map(|n| files.remove(&n)).collect()
24216×
}
2422
2423
/// Builds the tree ops and edits of a working-copy change against a base.
2424
/// `BLAKE3("oxy node 1" 0 seed kind path)`, truncated: a node identifier
2425
/// that the same seed and path always give (never the root's).
242659×
fn seeded_node(seed: &[u8; 32], path: &str, kind: NodeKind) -> NodeId {
242759×
    let mut h = blake3::Hasher::new();
242859×
    h.update(b"oxy node 1\0");
242959×
    h.update(seed);
243059×
    h.update(&[match kind {
243145×
        NodeKind::File => 1,
243213×
        NodeKind::Dir => 2,
24330×
        NodeKind::Symlink => 3,
24341×
        NodeKind::Subrepo => 4,
2435
    }]);
243659×
    h.update(path.as_bytes());
243759×
    let mut id = [0u8; 16];
243859×
    id.copy_from_slice(&h.finalize().as_bytes()[..16]);
243959×
    if id == [0; 16] {
24400×
        id[15] = 1;
244159×
    }
244259×
    NodeId(id)
244359×
}
2444
2445
struct ChangeBuilder<'a> {
2446
    base: &'a State,
2447
    base_snapshot: &'a Snapshot,
2448
    tree_ops: Vec<TreeOp>,
2449
    edits: Vec<Edit>,
2450
    statuses: Vec<PathStatus>,
2451
    dir_nodes: HashMap<String, NodeId>,
2452
    reuse: HashMap<String, (NodeId, NodeKind)>,
2453
    /// Changes that contributed to conflicts this working copy resolves.
2454
    contributors: BTreeSet<ChangeHash>,
2455
    resolves: bool,
2456
    attrs: crate::attributes::Attributes,
2457
    blobs: HashMap<String, ObjectRef>,
2458
    /// Opaque files whose replace conflict the content settles.
2459
    resolve: HashSet<String>,
2460
    /// When set, new nodes' identifiers derive from it and their paths.
2461
    node_seed: Option<[u8; 32]>,
2462
    /// For a Git import, the base's dead directories by where they were
2463
    /// (see `ensure_dir`), worked out when first needed.
2464
    dead_dirs: Option<HashMap<(NodeId, String), (NodeId, crate::tree::OpKey)>>,
2465
    /// Text edits of edited files, worked out in parallel beforehand
2466
    /// (`diff_in_parallel`); a file not here is diffed when it is updated.
2467
    diffs: HashMap<String, Vec<Edit>>,
2468
}
2469
2470
impl<'a> ChangeBuilder<'a> {
24712437×
    fn new(
24722437×
        base: &'a State,
24732437×
        base_snapshot: &'a Snapshot,
24742437×
        reuse: HashMap<String, (NodeId, NodeKind)>,
24752437×
    ) -> Self {
24762437×
        let mut dir_nodes: HashMap<String, NodeId> = HashMap::new();
24772437×
        dir_nodes.insert(String::new(), NodeId::ROOT);
247818411×
        for (p, e) in base_snapshot.tree.entries.iter() {
247918411×
            if e.kind == NodeKind::Dir {
2480620×
                dir_nodes.insert(p.clone(), e.node);
248117791×
            }
2482
        }
24832437×
        ChangeBuilder {
24842437×
            base,
24852437×
            base_snapshot,
24862437×
            tree_ops: vec![],
24872437×
            edits: vec![],
24882437×
            statuses: vec![],
24892437×
            dir_nodes,
24902437×
            reuse,
24912437×
            contributors: BTreeSet::new(),
24922437×
            resolves: false,
24932437×
            attrs: crate::attributes::Attributes::default(),
24942437×
            blobs: HashMap::new(),
24952437×
            resolve: HashSet::new(),
24962437×
            node_seed: None,
24972437×
            dead_dirs: None,
24982437×
            diffs: HashMap::new(),
24992437×
        }
25002437×
    }
2501
2502
    /// Diff the edited text files among `entries` against their base
2503
    /// renderings at once, on every core, for `update_path` to take: the
2504
    /// same diff it would make, only not one file after another.
25052437×
    fn diff_in_parallel(&mut self, entries: &BTreeMap<String, FsEntry>) {
2506
        use rayon::prelude::*;
25072437×
        let files = &self.base_snapshot.files;
25082437×
        let edited: Vec<(&String, &FsEntry, &crate::state::FileState)> = entries
25092437×
            .iter()
251019549×
            .filter_map(|(p, e)| {
251119549×
                let x = files.get(p)?;
251213318×
                (x.kind == e.kind && !e.unchanged && !self.opaque_for(p, e)).then_some((p, e, x))
251319549×
            })
25142437×
            .collect();
2515
        // Below this, starting the threads costs more than it saves.
25162437×
        if edited.len() < 16 {
25172427×
            return;
251810×
        }
251910×
        let this = &*self;
252010×
        let attrs = &this.attrs;
252110×
        let diffs: Vec<(String, Vec<Edit>)> = edited
252210×
            .par_iter()
25235560×
            .filter_map(|(p, e, x)| {
2524
                let FileContent::Text {
25255560×
                    bytes,
25265560×
                    materialized,
25275560×
                } = &x.content
2528
                else {
25290×
                    return None;
2530
                };
25315560×
                if materialized.is_conflicted() {
25320×
                    return None;
25335560×
                }
25345560×
                let disk_text = if attrs.lookup(p).eol.is_some() {
25350×
                    std::sync::Arc::new(crate::attributes::normalize_eol(&e.content))
2536
                } else {
25375560×
                    e.content.clone()
2538
                };
25395560×
                if *bytes == *disk_text {
25405480×
                    return None;
254180×
                }
254280×
                let keys = materialized.live_atoms();
254380×
                let lines = crate::diff::split_lines(bytes);
254480×
                (keys.len() == lines.len()).then(|| {
254580×
                    let edits = crate::diff::text_edits_of(x.node, &keys, &lines, &disk_text);
254680×
                    ((*p).clone(), edits)
254780×
                })
25485560×
            })
254910×
            .collect();
255010×
        self.diffs = diffs.into_iter().collect();
25512437×
    }
2552
25532437×
    fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self {
25542437×
        self.attrs = attrs;
25552437×
        self
25562437×
    }
2557
2558
    /// Whether a file is opaque: forced by `binary`, forced text by `text`
2559
    /// only when it has no NUL bytes, else sniffed.
2560
    /// The blob for an opaque entry: the cached one, else stored now.
2561176×
    fn blob_of(
2562176×
        &mut self,
2563176×
        repo: &Repo,
2564176×
        path: &str,
2565176×
        e: &FsEntry,
2566176×
        nocompress: bool,
2567176×
    ) -> Result<ObjectRef> {
2568176×
        let b = match e.blob {
25690×
            Some(b) => b,
2570176×
            None => repo.store_blob_for(&e.content, nocompress)?,
2571
        };
2572176×
        self.blobs.insert(path.to_string(), b);
2573176×
        Ok(b)
2574176×
    }
2575
257624376×
    fn opaque_for(&self, path: &str, e: &FsEntry) -> bool {
257724376×
        if e.blob.is_some() {
25784×
            return true;
257924372×
        }
258024372×
        if e.kind == NodeKind::Symlink || e.kind == NodeKind::Subrepo {
2581132×
            return true;
258224240×
        }
258324240×
        match self.attrs.lookup(path).binary {
258412×
            Some(true) => true,
25850×
            Some(false) => is_binary(&e.content),
258624228×
            None => is_binary(&e.content),
2587
        }
258824376×
    }
2589
2590
    /// Text content as stored: `\r\n` normalized for `eol=` files.
259115146×
    fn stored_text(
259215146×
        &self,
259315146×
        path: &str,
259415146×
        content: &std::sync::Arc<Vec<u8>>,
259515146×
    ) -> std::sync::Arc<Vec<u8>> {
259615146×
        if self.attrs.lookup(path).eol.is_some() {
25977×
            std::sync::Arc::new(crate::attributes::normalize_eol(content))
2598
        } else {
259915139×
            content.clone()
2600
        }
260115146×
    }
2602
2603
    /// The identifier of a node added at `path`: reused when asked, derived
2604
    /// from the seed when there is one, random otherwise.
26056351×
    fn new_node(&self, path: &str, kind: NodeKind) -> NodeId {
26066351×
        if let Some(n) = self.reused_node(path, kind) {
2607170×
            return n;
26086181×
        }
26096181×
        match &self.node_seed {
261059×
            Some(seed) => seeded_node(seed, path, kind),
26116122×
            None => NodeId::random(),
2612
        }
26136351×
    }
2614
26156351×
    fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> {
26166351×
        self.reuse
26176351×
            .get(path)
26186351×
            .filter(|(_, k)| *k == kind)
26196351×
            .map(|(n, _)| *n)
26206351×
    }
2621
2622
    /// A dead directory last placed at (`parent`, `name`) in the base.
262315×
    fn revivable_dir(
262415×
        &mut self,
262515×
        parent: NodeId,
262615×
        name: &str,
262715×
    ) -> Option<(NodeId, crate::tree::OpKey)> {
262815×
        let base = self.base;
262915×
        self.dead_dirs
263015×
            .get_or_insert_with(|| base.tree().dead_dirs())
263115×
            .get(&(parent, name.to_string()))
263215×
            .copied()
263315×
    }
2634
26356232×
    fn ensure_dir(&mut self, path: &str) -> NodeId {
26366232×
        if let Some(n) = self.dir_nodes.get(path) {
26376080×
            return *n;
2638152×
        }
2639152×
        let mut parent = NodeId::ROOT;
2640152×
        let mut prefix = String::new();
2641171×
        for comp in path.split('/') {
2642171×
            if !prefix.is_empty() {
264319×
                prefix.push('/');
2644152×
            }
2645171×
            prefix.push_str(comp);
2646171×
            let known = self.dir_nodes.get(&prefix).copied();
2647171×
            let revive = match known {
264813×
                Some(_) => None,
2649158×
                None if self.node_seed.is_some() => self.revivable_dir(parent, comp),
2650143×
                None => None,
2651
            };
2652171×
            parent = match (known, revive) {
265313×
                (Some(n), _) => n,
26542×
                (None, Some((node, old))) => {
2655
                    // Git has no identity for a directory: one that comes
2656
                    // back where it was is the same directory, brought back
2657
                    // with a move, so lines of history that placed files in
2658
                    // it before and after agree (oxygen#30).
26592×
                    self.tree_ops.push(TreeOp::Move {
26602×
                        node,
26612×
                        old: OpRef {
26622×
                            change: old.0,
26632×
                            index: old.1,
26642×
                        },
26652×
                        parent,
26662×
                        name: comp.to_string(),
26672×
                    });
26682×
                    self.dir_nodes.insert(prefix.clone(), node);
26692×
                    node
2670
                }
2671
                (None, None) => {
2672156×
                    let node = self.new_node(&prefix, NodeKind::Dir);
2673156×
                    self.tree_ops.push(TreeOp::AddNode {
2674156×
                        node,
2675156×
                        kind: NodeKind::Dir,
2676156×
                        mode: Mode::default(),
2677156×
                        parent,
2678156×
                        name: comp.to_string(),
2679156×
                    });
2680156×
                    self.dir_nodes.insert(prefix.clone(), node);
2681156×
                    node
2682
                }
2683
            };
2684
        }
2685152×
        parent
26866232×
    }
2687
26886195×
    fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> {
26896195×
        let (dir, name) = split_path(path);
26906195×
        let parent = self.ensure_dir(dir);
26916195×
        let node = self.new_node(path, e.kind);
26926195×
        let opaque = self.opaque_for(path, e);
26936195×
        let nocompress = self.attrs.lookup(path).nocompress;
26946195×
        self.tree_ops.push(TreeOp::AddNode {
26956195×
            node,
26966195×
            kind: e.kind,
26976195×
            mode: Mode {
26986195×
                exec: e.exec,
26996195×
                opaque,
27006195×
            },
27016195×
            parent,
27026195×
            name: name.to_string(),
27036195×
        });
27046195×
        if opaque {
2705127×
            let blob = self.blob_of(repo, path, e, nocompress)?;
2706127×
            self.edits.push(Edit::Replace {
2707127×
                node,
2708127×
                old: None,
2709127×
                new: Some(blob),
2710127×
            });
27116068×
        } else if !e.content.is_empty() {
27126037×
            let content = self.stored_text(path, &e.content);
27136037×
            self.edits.push(Edit::Insert {
27146037×
                node,
27156037×
                after: crate::objects::AtomRef::Start,
27166037×
                before: crate::objects::AtomRef::End,
27176037×
                lines: crate::lines::Lines::split_shared(content),
27186037×
            });
27196037×
        }
27206195×
        self.statuses.push(PathStatus {
27216195×
            path: path.to_string(),
27226195×
            status: ChangeStatus::Added,
27236195×
        });
27246195×
        Ok(())
27256195×
    }
2726
2727
    /// Move the node at `old` to `new` and apply `e`'s content to it.
272837×
    fn move_path(
272937×
        &mut self,
273037×
        repo: &Repo,
273137×
        old: &str,
273237×
        new: &str,
273337×
        e: &FsEntry,
273437×
        expected: &crate::state::FileState,
273537×
    ) -> Result<()> {
273637×
        let placement = self.base_snapshot.tree.entries[old].placement;
273737×
        let (dir, name) = split_path(new);
273837×
        let parent = self.ensure_dir(dir);
273937×
        self.tree_ops.push(TreeOp::Move {
274037×
            node: expected.node,
274137×
            old: op_ref(placement),
274237×
            parent,
274337×
            name: name.to_string(),
274437×
        });
274537×
        for k in self.same_place(&expected.node, placement) {
27460×
            self.tree_ops.push(TreeOp::RemoveNode {
27470×
                node: expected.node,
27480×
                placement: op_ref(k),
27490×
            });
27500×
        }
275137×
        self.update_path(repo, old, e, expected)?;
2752
        // update_path reports under the old path; the rename is what the user sees.
275337×
        self.statuses.retain(|s| s.path != old);
275437×
        self.statuses.push(PathStatus {
275537×
            path: new.to_string(),
275637×
            status: ChangeStatus::Renamed,
275737×
        });
275837×
        Ok(())
275937×
    }
2760
2761
    /// For a Git import: a node the commit keeps is in one place, the one
2762
    /// its tree names, so any other live placement it has in the base goes;
2763
    /// and a node the tree cannot show at all (an orphan) goes too. Such a
2764
    /// placement is out of sight, under a directory another line of history
2765
    /// removed, where no diff of the tree would ever reach it (oxygen#30).
276688×
    fn retire_stray_placements(&mut self) {
276788×
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
276888×
        let mut placed: BTreeSet<NodeId> = BTreeSet::new();
276999×
        for op in &self.tree_ops {
277099×
            match op {
277119×
                TreeOp::Move { node, old, .. } => {
277219×
                    retired.insert((old.change, old.index));
277319×
                    placed.insert(*node);
277419×
                }
277521×
                TreeOp::RemoveNode { placement, .. } => {
277621×
                    retired.insert((placement.change, placement.index));
277721×
                }
277859×
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
2779
            }
2780
        }
2781
        // Nodes kept where they were: what the base's tree view shows and
2782
        // this change neither moves nor removes.
278388×
        let mut kept: HashMap<NodeId, (ChangeHash, u32)> = HashMap::new();
2784241×
        for e in self.base_snapshot.tree.entries.values() {
2785241×
            if !retired.contains(&e.placement) {
2786204×
                kept.insert(e.node, e.placement);
2787204×
            }
2788
        }
2789
        // Only a node with several live placements can have a stray one,
2790
        // and the tree view names each as a conflict.
279188×
        let multi: BTreeSet<NodeId> = self
279288×
            .base_snapshot
279388×
            .tree
279488×
            .conflicts
279588×
            .iter()
279688×
            .filter_map(|c| match c {
27971×
                crate::tree::TreeConflict::MultiPlacement { node, .. } => Some(*node),
27986×
                _ => None,
27997×
            })
280088×
            .collect();
280188×
        let mut extra = Vec::new();
2802
        // A node whose only placements are under a directory another line
2803
        // removed (an orphan) is nowhere the tree can show, and Git's tree
2804
        // has no such path: it goes.
280588×
        for c in &self.base_snapshot.tree.conflicts {
28067×
            let crate::tree::TreeConflict::Orphan { node, .. } = c else {
28076×
                continue;
2808
            };
28091×
            if kept.contains_key(node) || placed.contains(node) {
28100×
                continue;
28111×
            }
28121×
            for (k, _) in self.base.tree().live_placements(node) {
28131×
                if retired.insert(k) {
28141×
                    extra.push(TreeOp::RemoveNode {
28151×
                        node: *node,
28161×
                        placement: op_ref(k),
28171×
                    });
28181×
                }
2819
            }
2820
        }
282188×
        for node in placed
282288×
            .iter()
282388×
            .chain(multi.iter().filter(|n| kept.contains_key(*n)))
2824
        {
282519×
            for (k, _) in self.base.tree().live_placements(node) {
282618×
                if retired.contains(&k) || kept.get(node) == Some(&k) {
282717×
                    continue;
28281×
                }
28291×
                retired.insert(k);
28301×
                extra.push(TreeOp::RemoveNode {
28311×
                    node: *node,
28321×
                    placement: op_ref(k),
28331×
                });
2834
            }
2835
        }
283688×
        if !extra.is_empty() {
28372×
            self.resolves = true;
28382×
            self.tree_ops.extend(extra);
283986×
        }
284088×
    }
2841
2842
    /// Nodes the base shows as one with another (identical additions at one
2843
    /// path, spec §9.2) are retired by the change that first touches the
2844
    /// node shown: edited, moved, removed or given a mode alone, it would
2845
    /// differ from them and the name conflict would come back. Nothing
2846
    /// else changes for the user, so no status reports it.
28472437×
    fn retire_unified(&mut self) {
28482437×
        let unified = &self.base_snapshot.tree.unified;
28492437×
        if unified.is_empty() {
28502408×
            return;
285129×
        }
285229×
        let touched: HashSet<NodeId> = self
285329×
            .tree_ops
285429×
            .iter()
285529×
            .map(|o| o.node())
285629×
            .chain(self.edits.iter().map(|e| e.node()))
285729×
            .collect();
285829×
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
285929×
        for op in &self.tree_ops {
286014×
            match op {
28613×
                TreeOp::Move { old, .. } => {
28623×
                    retired.insert((old.change, old.index));
28633×
                }
28643×
                TreeOp::RemoveNode { placement, .. } => {
28653×
                    retired.insert((placement.change, placement.index));
28663×
                }
28678×
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
2868
            }
2869
        }
287029×
        let mut extra = Vec::new();
287130×
        for (dup, shown) in unified {
287230×
            if !touched.contains(shown) {
287313×
                continue;
287417×
            }
287517×
            for (k, _) in self.base.tree().live_placements(dup) {
287617×
                if retired.insert(k) {
287717×
                    extra.push(TreeOp::RemoveNode {
287817×
                        node: *dup,
287917×
                        placement: op_ref(k),
288017×
                    });
288117×
                }
2882
            }
2883
        }
288429×
        self.tree_ops.extend(extra);
28852437×
    }
2886
2887
    /// The node's other live placements at the same place as `k`, the one
2888
    /// the tree shows: two lines of history that put a node back where it
2889
    /// was each leave a placement, and the tree shows them as one file. A
2890
    /// change that removes or moves it retires them together, or the copy
2891
    /// left behind outlives its directory (oxygen#88).
2892161×
    fn same_place(&self, node: &NodeId, k: (ChangeHash, u32)) -> Vec<(ChangeHash, u32)> {
2893161×
        let tree = self.base.tree();
2894161×
        let Some(shown) = tree.placement(&k) else {
28950×
            return Vec::new();
2896
        };
2897161×
        tree.live_placements(node)
2898161×
            .into_iter()
2899163×
            .filter(|(o, p)| *o != k && p.parent == shown.parent && p.name == shown.name)
2900161×
            .map(|(o, _)| o)
2901161×
            .collect()
2902161×
    }
2903
2904124×
    fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) {
2905124×
        let entry = &self.base_snapshot.tree.entries[path];
2906124×
        let placement = entry.placement;
2907124×
        self.tree_ops.push(TreeOp::RemoveNode {
2908124×
            node: expected.node,
2909124×
            placement: op_ref(placement),
2910124×
        });
2911124×
        for k in self.same_place(&expected.node, placement) {
29121×
            self.tree_ops.push(TreeOp::RemoveNode {
29131×
                node: expected.node,
29141×
                placement: op_ref(k),
29151×
            });
29161×
        }
2917124×
        if path.contains(".oxy-conflict-") || expected.is_conflicted() {
29187×
            self.resolves = true;
2919117×
        }
2920124×
        self.statuses.push(PathStatus {
2921124×
            path: path.to_string(),
2922124×
            status: ChangeStatus::Removed,
2923124×
        });
2924124×
    }
2925
292613355×
    fn update_path(
292713355×
        &mut self,
292813355×
        repo: &Repo,
292913355×
        path: &str,
293013355×
        e: &FsEntry,
293113355×
        expected: &crate::state::FileState,
293213355×
    ) -> Result<()> {
293313355×
        if expected.kind != e.kind {
29341×
            self.remove_path(path, expected);
29351×
            self.statuses.pop();
29361×
            self.add_path(repo, path, e)?;
29371×
            self.statuses.pop();
29381×
            self.statuses.push(PathStatus {
29391×
                path: path.to_string(),
29401×
                status: ChangeStatus::Modified,
29411×
            });
29421×
            return Ok(());
294313354×
        }
294413354×
        let entry = &self.base_snapshot.tree.entries[path];
294513354×
        let node = expected.node;
294613354×
        let mode_atom = entry.mode_atom.map(op_ref);
294713354×
        if e.unchanged {
2948
            // Known equal to the base's unconflicted rendering (spec
2949
            // §12.3); only the executable bit can differ.
29504245×
            if e.exec != expected.mode.exec {
29513×
                if let Some(old) = mode_atom {
29523×
                    self.tree_ops.push(TreeOp::SetMode {
29533×
                        node,
29543×
                        old,
29553×
                        mode: Mode {
29563×
                            exec: e.exec,
29573×
                            opaque: false,
29583×
                        },
29593×
                    });
29603×
                    self.statuses.push(PathStatus {
29613×
                        path: path.to_string(),
29623×
                        status: ChangeStatus::ModeChanged,
29633×
                    });
29643×
                }
29654242×
            }
29664245×
            return Ok(());
29679109×
        }
29689109×
        let mut status: Option<ChangeStatus> = None;
29699109×
        let wants_opaque = self.opaque_for(path, e);
29709109×
        let nocompress = self.attrs.lookup(path).nocompress;
29719109×
        let disk_text = self.stored_text(path, &e.content);
29729109×
        match &expected.content {
29730×
            FileContent::Dir => {}
2974
            FileContent::Text {
29758838×
                bytes,
29768838×
                materialized,
2977
            } => {
29788838×
                if materialized.is_conflicted() {
2979147×
                    if bytes == &*disk_text {
298097×
                        return Ok(());
298150×
                    }
298250×
                    let regions = self.base.conflict_regions(&node);
298350×
                    let plain: Vec<&[u8]> =
2984110×
                        regions.plain.iter().map(|(_, l)| l.as_slice()).collect();
298550×
                    if has_own_conflict_markers(&e.content, bytes, &plain) {
29865×
                        self.statuses.push(PathStatus {
29875×
                            path: path.to_string(),
29885×
                            status: ChangeStatus::ConflictEdited,
29895×
                        });
29905×
                        return Ok(());
299145×
                    }
2992
                    // Resolution (spec §9.6): diff against the unconflicted lines
2993
                    // with each region as a gap, then retire every region atom.
2994
                    // Whatever the author kept from a region is re-inserted as
2995
                    // new atoms, which leaves the graph totally ordered.
299645×
                    let mut edits = text_edits(node, &regions.plain, &disk_text);
299745×
                    if !regions.region_atoms.is_empty() {
299845×
                        edits.insert(
299945×
                            0,
300045×
                            Edit::Delete {
300145×
                                node,
300245×
                                atoms: ranges(&regions.region_atoms),
300345×
                            },
300445×
                        );
300545×
                    }
300645×
                    self.edits.extend(edits);
300745×
                    self.contributors.extend(regions.contributors);
300845×
                    self.resolves = true;
300945×
                    if e.exec != expected.mode.exec {
30100×
                        if let Some(old) = mode_atom {
30110×
                            self.tree_ops.push(TreeOp::SetMode {
30120×
                                node,
30130×
                                old,
30140×
                                mode: Mode {
30150×
                                    exec: e.exec,
30160×
                                    opaque: false,
30170×
                                },
30180×
                            });
30190×
                        }
302045×
                    }
302145×
                    self.statuses.push(PathStatus {
302245×
                        path: path.to_string(),
302345×
                        status: ChangeStatus::Resolved,
302445×
                    });
302545×
                    return Ok(());
30268691×
                }
30278691×
                if wants_opaque {
3028
                    // Text → opaque (spec §7.5), by content or by attribute.
30292×
                    if let Some(old) = mode_atom {
30302×
                        self.tree_ops.push(TreeOp::SetMode {
30312×
                            node,
30322×
                            old,
30332×
                            mode: Mode {
30342×
                                exec: e.exec,
30352×
                                opaque: true,
30362×
                            },
30372×
                        });
30382×
                    }
30392×
                    let live: Vec<_> = repo_live_keys(self.base, &node);
30402×
                    if !live.is_empty() {
30412×
                        self.edits.push(Edit::Delete {
30422×
                            node,
30432×
                            atoms: ranges(&live),
30442×
                        });
30452×
                    }
30462×
                    let blob = self.blob_of(repo, path, e, nocompress)?;
30472×
                    self.edits.push(Edit::Replace {
30482×
                        node,
30492×
                        old: None,
30502×
                        new: Some(blob),
30512×
                    });
30522×
                    status = Some(ChangeStatus::Modified);
30538689×
                } else if bytes != &*disk_text {
3054901×
                    let mut laps = crate::timing::Laps::start();
3055
                    // The base's rendering holds its live atoms in order,
3056
                    // one per line (it is not conflicted here).
3057901×
                    let edits = match self.diffs.remove(path) {
305880×
                        Some(edits) => edits,
3059
                        None => {
3060821×
                            let keys = materialized.live_atoms();
3061821×
                            let lines = crate::diff::split_lines(bytes);
3062821×
                            if keys.len() == lines.len() {
3063821×
                                crate::diff::text_edits_of(node, &keys, &lines, &disk_text)
3064
                            } else {
30650×
                                text_edits(node, &self.base.live_lines(&node), &disk_text)
3066
                            }
3067
                        }
3068
                    };
3069901×
                    laps.lap("build: text diff");
3070901×
                    if !edits.is_empty() {
3071901×
                        self.edits.extend(edits);
3072901×
                        status = Some(ChangeStatus::Modified);
3073901×
                    }
30747788×
                }
30758691×
                let switched_to_opaque = wants_opaque;
30768691×
                if !switched_to_opaque && e.exec != expected.mode.exec {
30774×
                    if let Some(old) = mode_atom {
30784×
                        self.tree_ops.push(TreeOp::SetMode {
30794×
                            node,
30804×
                            old,
30814×
                            mode: Mode {
30824×
                                exec: e.exec,
30834×
                                opaque: false,
30844×
                            },
30854×
                        });
30864×
                        status.get_or_insert(ChangeStatus::ModeChanged);
30874×
                    }
30888687×
                }
3089
            }
3090271×
            FileContent::Opaque { blobs } => {
3091271×
                let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content));
3092271×
                if e.kind == NodeKind::File && wants_opaque {
3093217×
                    // Unchanged or replaced, the file stays opaque: remember its blob.
3094217×
                    self.blobs.insert(path.to_string(), new_ref);
3095217×
                }
3096271×
                if blobs.len() > 1 && self.resolve.contains(path) && e.kind == expected.kind {
3097
                    // Settle the replace conflict (spec §9.3): replace every
3098
                    // live candidate with the chosen blob, depending on each
3099
                    // change that made one live, so all are superseded.
31005×
                    let chosen = self.blob_of(repo, path, e, nocompress)?;
310110×
                    for b in blobs {
310210×
                        self.edits.push(Edit::Replace {
310310×
                            node,
310410×
                            old: Some(*b),
310510×
                            new: Some(chosen),
310610×
                        });
310710×
                    }
31085×
                    self.contributors
31095×
                        .extend(self.base.blob_contributors(&node, blobs));
31105×
                    self.resolves = true;
31115×
                    self.statuses.push(PathStatus {
31125×
                        path: path.to_string(),
31135×
                        status: ChangeStatus::Resolved,
31145×
                    });
31155×
                    return Ok(());
3116266×
                }
3117266×
                if blobs.len() > 1 {
31189×
                    if !blobs.contains(&new_ref) {
31194×
                        self.statuses.push(PathStatus {
31204×
                            path: path.to_string(),
31214×
                            status: ChangeStatus::ConflictEdited,
31224×
                        });
31235×
                    }
31249×
                    return Ok(());
3125257×
                }
3126257×
                let current = blobs.first().copied();
3127257×
                if current != Some(new_ref) {
312844×
                    if e.kind == NodeKind::File && !wants_opaque {
3129
                        // Opaque → text (spec §7.5).
31302×
                        if let Some(old) = mode_atom {
31312×
                            self.tree_ops.push(TreeOp::SetMode {
31322×
                                node,
31332×
                                old,
31342×
                                mode: Mode {
31352×
                                    exec: e.exec,
31362×
                                    opaque: false,
31372×
                                },
31382×
                            });
31392×
                        }
31402×
                        self.edits.push(Edit::Replace {
31412×
                            node,
31422×
                            old: current,
31432×
                            new: None,
31442×
                        });
31452×
                        if !e.content.is_empty() {
31462×
                            self.edits.push(Edit::Insert {
31472×
                                node,
31482×
                                after: crate::objects::AtomRef::Start,
31492×
                                before: crate::objects::AtomRef::End,
31502×
                                lines: crate::lines::Lines::split(&e.content),
31512×
                            });
31522×
                        }
3153
                    } else {
315442×
                        let blob = self.blob_of(repo, path, e, nocompress)?;
315542×
                        self.edits.push(Edit::Replace {
315642×
                            node,
315742×
                            old: current,
315842×
                            new: Some(blob),
315942×
                        });
3160
                        // New contents and a new executable bit at once: the
3161
                        // bit was dropped (VS Code's inno_updater.exe,
3162
                        // 2018-02-28, lost +x for 49 commits; oxygen#87).
316342×
                        if e.kind == NodeKind::File && e.exec != expected.mode.exec {
31642×
                            if let Some(old) = mode_atom {
31652×
                                self.tree_ops.push(TreeOp::SetMode {
31662×
                                    node,
31672×
                                    old,
31682×
                                    mode: Mode {
31692×
                                        exec: e.exec,
31702×
                                        opaque: true,
31712×
                                    },
31722×
                                });
31732×
                            }
317440×
                        }
3175
                    }
317644×
                    status = Some(ChangeStatus::Modified);
3177213×
                } else if e.kind == NodeKind::File && e.exec != expected.mode.exec {
31780×
                    if let Some(old) = mode_atom {
31790×
                        self.tree_ops.push(TreeOp::SetMode {
31800×
                            node,
31810×
                            old,
31820×
                            mode: Mode {
31830×
                                exec: e.exec,
31840×
                                opaque: true,
31850×
                            },
31860×
                        });
31870×
                        status = Some(ChangeStatus::ModeChanged);
31880×
                    }
3189213×
                }
3190
            }
3191
        }
31928948×
        if let Some(s) = status {
3193951×
            self.statuses.push(PathStatus {
3194951×
                path: path.to_string(),
3195951×
                status: s,
3196951×
            });
31977997×
        }
31988948×
        Ok(())
319913355×
    }
3200
}
3201
32022×
fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> {
32032×
    base.live_lines(node).into_iter().map(|(k, _)| k).collect()
32042×
}
3205
3206
/// The changes a change's scope references (spec §8.1): introducers of every
3207
/// atom, placement, and mode it touches, and of every node it operates on.
32082501×
pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> {
32092501×
    let mut out = BTreeSet::new();
32102501×
    let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| {
3211223×
        if !r.change.is_zero() {
3212223×
            out.insert(r.change);
3213223×
        }
3214223×
    };
32152501×
    let mut nodes: Vec<NodeId> = Vec::new();
32166786×
    for op in &change.tree_ops {
32176786×
        match op {
32186563×
            TreeOp::AddNode { parent, .. } => nodes.push(*parent),
3219170×
            TreeOp::RemoveNode { node, placement } => {
3220170×
                nodes.push(*node);
3221170×
                opref(placement, &mut out);
3222170×
            }
3223
            TreeOp::Move {
322441×
                node, old, parent, ..
322541×
            } => {
322641×
                nodes.push(*node);
322741×
                nodes.push(*parent);
322841×
                opref(old, &mut out);
322941×
            }
323012×
            TreeOp::SetMode { node, old, .. } => {
323112×
                nodes.push(*node);
323212×
                opref(old, &mut out);
323312×
            }
3234
        }
3235
    }
32368552×
    for e in &change.edits {
32378552×
        nodes.push(e.node());
3238186×
        match e {
32397713×
            Edit::Insert { after, before, .. } => {
324015426×
                for r in [after, before] {
324115426×
                    if let crate::objects::AtomRef::Atom { change, .. } = r {
32422374×
                        if !change.is_zero() {
32432371×
                            out.insert(*change);
32442371×
                        }
324513052×
                    }
3246
                }
3247
            }
3248653×
            Edit::Delete { atoms, .. } => {
3249
                // Deleting an atom also depends on whoever inserted the live
3250
                // atoms right after it: the author saw those insertions, and
3251
                // without the dependency spec §9.1 would call the deletion
3252
                // concurrent with them (a delete/insert conflict).
3253653×
                let graph = base.file(&e.node());
32541231×
                for r in atoms {
32551231×
                    if !r.change.is_zero() {
32561230×
                        out.insert(r.change);
32571230×
                    }
32581231×
                    if let Some(g) = graph {
32591272×
                        for i in r.from..r.to {
32601272×
                            out.extend(
32611272×
                                g.live_successor_changes(&(r.change, i))
32621272×
                                    .into_iter()
32631272×
                                    .filter(|c| !c.is_zero()),
3264
                            );
3265
                        }
32660×
                    }
3267
                }
3268
            }
3269
            // Replacing a blob depends on the change that made it current,
3270
            // as deleting a line depends on the line's introducer.
3271
            Edit::Replace {
327259×
                node,
327359×
                old: Some(old),
3274
                ..
3275
            } => {
327659×
                out.extend(
327759×
                    base.live_replaces(node)
327859×
                        .into_iter()
327976×
                        .filter(|(c, b)| b == old && !c.is_zero())
328059×
                        .map(|(c, _)| c),
3281
                );
3282
            }
3283127×
            Edit::Replace { .. } => {}
3284
        }
3285
    }
328615379×
    for n in nodes {
328715379×
        if let Some(h) = base.tree().introducer(&n) {
32882479×
            out.insert(h);
328912900×
        }
3290
    }
32912501×
    out
32922501×
}
3293
3294
/// Paths of the nodes a change adds on top of `base_snapshot`.
32951597×
pub fn added_node_paths(
32961597×
    change: &Change,
32971597×
    base_snapshot: &Snapshot,
32981597×
) -> HashMap<String, (NodeId, NodeKind)> {
32991597×
    let by_node = base_snapshot.tree.by_node();
33001597×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
33011597×
    for op in &change.tree_ops {
3302
        if let TreeOp::AddNode {
330397×
            node,
330497×
            parent,
330597×
            name,
330697×
            kind,
3307
            ..
3308129×
        } = op
330997×
        {
331097×
            new_names.insert(*node, (*parent, name.clone(), *kind));
331197×
        }
3312
    }
33131597×
    let mut out = HashMap::new();
33141597×
    for (node, (_, _, kind)) in &new_names {
331597×
        if let Some(path) = path_of_node(node, &by_node, &new_names) {
331697×
            out.insert(path, (*node, *kind));
331797×
        }
3318
    }
33191597×
    out
33201597×
}
3321
3322
/// Whether `op` retires a node `base_snapshot` shows as one with another
3323
/// (spec §9.2): part of touching that node, never a removal the user made.
332437×
fn is_unified_retirement(op: &TreeOp, base_snapshot: &Snapshot) -> bool {
33258×
    matches!(op, TreeOp::RemoveNode { node, .. } if base_snapshot.tree.unified.contains_key(node))
332637×
}
3327
3328232×
fn path_of_node(
3329232×
    n: &NodeId,
3330232×
    by_node: &HashMap<NodeId, &crate::tree::TreeEntry>,
3331232×
    new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>,
3332232×
) -> Option<String> {
3333232×
    if let Some(e) = by_node.get(n) {
333469×
        return Some(e.path.clone());
3335163×
    }
3336163×
    let mut parts = Vec::new();
3337163×
    let mut cur = *n;
3338
    loop {
3339322×
        if cur.is_root() {
3340146×
            break;
3341176×
        }
3342176×
        if let Some(e) = by_node.get(&cur) {
33430×
            if !e.path.is_empty() {
33440×
                parts.push(e.path.clone());
33450×
            }
33460×
            break;
3347176×
        }
3348176×
        let (parent, name, _) = new_names.get(&cur)?;
3349159×
        parts.push(name.clone());
3350159×
        cur = *parent;
3351
    }
3352146×
    parts.reverse();
3353146×
    Some(parts.join("/"))
3354232×
}
3355
3356
/// Render a change's operations with line content, using `base` for deleted lines.
335733×
pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String {
335833×
    let by_node = base_snapshot.tree.by_node();
335933×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
336033×
    for op in &change.tree_ops {
3361
        if let TreeOp::AddNode {
336216×
            node,
336316×
            parent,
336416×
            name,
336516×
            kind,
3366
            ..
336724×
        } = op
336816×
        {
336916×
            new_names.insert(*node, (*parent, name.clone(), *kind));
337016×
        }
3371
    }
337273×
    let path_of = |n: &NodeId| -> String {
337373×
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
337473×
    };
3375
337633×
    let mut out = String::new();
337733×
    for op in change
337833×
        .tree_ops
337933×
        .iter()
338033×
        .filter(|o| !is_unified_retirement(o, base_snapshot))
3381
    {
338223×
        match op {
3383
            TreeOp::AddNode {
338416×
                node, kind, mode, ..
3385
            } => {
338616×
                out.push_str(&format!(
3387
                    "A {} ({:?}{}{})\n",
338816×
                    path_of(node),
3389
                    kind,
339016×
                    if mode.exec { ", exec" } else { "" },
339116×
                    if mode.opaque { ", opaque" } else { "" }
3392
                ));
3393
            }
33945×
            TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))),
3395
            TreeOp::Move {
33962×
                node, parent, name, ..
3397
            } => {
33982×
                let to = if parent.is_root() {
33990×
                    name.clone()
3400
                } else {
34012×
                    format!("{}/{name}", path_of(parent))
3402
                };
34032×
                out.push_str(&format!("R {} -> {to}\n", path_of(node)))
3404
            }
34050×
            TreeOp::SetMode { node, mode, .. } => {
34060×
                out.push_str(&format!(
34070×
                    "M {} mode: exec={} opaque={}\n",
34080×
                    path_of(node),
34090×
                    mode.exec,
34100×
                    mode.opaque
34110×
                ));
34120×
            }
3413
        }
3414
    }
3415
    // Every other file the change edits is modified: the summary names
3416
    // every path it touches, not only those with a tree operation.
341733×
    let with_op: HashSet<NodeId> = change.tree_ops.iter().map(|o| o.node()).collect();
341833×
    let mut listed: HashSet<NodeId> = HashSet::new();
341936×
    for e in &change.edits {
342036×
        let n = e.node();
342136×
        if !with_op.contains(&n) && listed.insert(n) {
342221×
            out.push_str(&format!("M {}\n", path_of(&n)));
342321×
        }
3424
    }
342533×
    let mut last_node: Option<NodeId> = None;
342636×
    for e in &change.edits {
342736×
        let n = e.node();
342836×
        if last_node != Some(n) {
342927×
            out.push_str(&format!("--- {}\n", path_of(&n)));
343027×
            last_node = Some(n);
343127×
        }
343236×
        match e {
343327×
            Edit::Insert { lines, .. } => {
343432×
                for l in lines.iter() {
343532×
                    out.push('+');
343632×
                    out.push_str(&String::from_utf8_lossy(l));
343732×
                    if !l.ends_with(b"\n") {
34380×
                        out.push('\n');
343932×
                    }
3440
                }
3441
            }
34428×
            Edit::Delete { atoms, .. } => {
34438×
                for r in atoms {
34448×
                    for i in r.from..r.to {
34458×
                        let content = base
34468×
                            .file(&n)
34478×
                            .and_then(|g| g.content(&(r.change, i)))
34488×
                            .unwrap_or(b"<unknown>\n");
34498×
                        out.push('-');
34508×
                        out.push_str(&String::from_utf8_lossy(content));
34518×
                        if !content.ends_with(b"\n") {
34520×
                            out.push('\n');
34538×
                        }
3454
                    }
3455
                }
3456
            }
34571×
            Edit::Replace { old, new, .. } => {
34581×
                out.push_str(&format!(
3459
                    "  opaque content {} -> {}\n",
34601×
                    old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()),
34611×
                    new.map(|o| o.short(12)).unwrap_or_else(|| "none".into())
3462
                ));
3463
            }
3464
        }
3465
    }
346633×
    out
346733×
}
3468
3469
/// Pair removed files with added files of the same kind whose contents are
3470
/// at least `RENAME_THRESHOLD` percent alike, best matches first. Beyond
3471
/// `RENAME_LIMIT` candidate pairs only identical contents are paired.
3472
const RENAME_THRESHOLD: u32 = 50;
3473
const RENAME_LIMIT: usize = 10_000;
3474
3475
/// How a change is built beyond its entries: for a Git import, the seed
3476
/// its new nodes' identifiers derive from; and, when it is built from part
3477
/// of the tree, whether the new tree has anything under a directory.
3478
#[derive(Default)]
3479
struct Scope<'a> {
3480
    seed: Option<[u8; 32]>,
3481
    dir_needed: Option<&'a dyn Fn(&str) -> bool>,
3482
}
3483
34842437×
fn detect_renames(
34852437×
    base_snapshot: &Snapshot,
34862437×
    fs_entries: &BTreeMap<String, FsEntry>,
34872437×
    in_scope: impl Fn(&str) -> bool,
34882437×
) -> Vec<(String, String)> {
34892437×
    let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot
34902437×
        .files
34912437×
        .iter()
349213946×
        .filter(|(p, f)| {
349313946×
            f.kind != NodeKind::Dir
349413462×
                && !fs_entries.contains_key(*p)
3495144×
                && in_scope(p)
3496134×
                && !f.is_conflicted()
349713946×
        })
34982437×
        .collect();
34992437×
    let added: Vec<(&String, &FsEntry)> = fs_entries
35002437×
        .iter()
350119549×
        .filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p))
35022437×
        .collect();
35032437×
    if removed.is_empty() || added.is_empty() {
35042373×
        return vec![];
350564×
    }
350664×
    let exact_only = removed.len() * added.len() > RENAME_LIMIT;
350778×
    let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> {
350878×
        match &f.content {
350974×
            FileContent::Text { bytes, .. } => Some(bytes.clone()),
35104×
            _ => None,
3511
        }
351278×
    };
351364×
    let mut scored: Vec<(u32, usize, usize)> = Vec::new();
351478×
    for (ri, (_, f)) in removed.iter().enumerate() {
351578×
        let old = old_content(f);
351678×
        let old_blob = match &f.content {
35174×
            FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]),
351874×
            _ => None,
3519
        };
352097×
        for (ai, (_, e)) in added.iter().enumerate() {
352197×
            if e.kind != f.kind {
35220×
                continue;
352397×
            }
352497×
            let score = match (&old, old_blob) {
352587×
                (Some(o), _) => {
352687×
                    if exact_only {
35270×
                        if *o == *e.content {
35280×
                            100
3529
                        } else {
35300×
                            0
3531
                        }
3532
                    } else {
353387×
                        similarity(o, &e.content)
3534
                    }
3535
                }
353610×
                (None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100,
35377×
                _ => 0,
3538
            };
353997×
            if score >= RENAME_THRESHOLD {
354043×
                scored.push((score, ri, ai));
354154×
            }
3542
        }
3543
    }
354464×
    scored.sort_by(|a, b| {
35459×
        b.0.cmp(&a.0)
35469×
            .then(removed[a.1].0.cmp(removed[b.1].0))
35479×
            .then(added[a.2].0.cmp(added[b.2].0))
35489×
    });
354964×
    let mut used_r = vec![false; removed.len()];
355064×
    let mut used_a = vec![false; added.len()];
355164×
    let mut out = Vec::new();
355264×
    for (_, ri, ai) in scored {
355343×
        if used_r[ri] || used_a[ai] {
35546×
            continue;
355537×
        }
355637×
        used_r[ri] = true;
355737×
        used_a[ai] = true;
355837×
        out.push((removed[ri].0.clone(), added[ai].0.clone()));
3559
    }
356064×
    out
35612437×
}
3562
3563
/// Percentage of the larger file's bytes found in lines the two share
3564
/// (multiset intersection of lines), like Git's similarity index.
356591×
pub fn similarity(a: &[u8], b: &[u8]) -> u32 {
356691×
    if a == b {
356725×
        return 100;
356866×
    }
356966×
    if a.is_empty() || b.is_empty() {
35705×
        return 0;
357161×
    }
357261×
    let mut counts: HashMap<&[u8], usize> = HashMap::new();
3573118×
    for l in split_lines(a) {
3574118×
        *counts.entry(l).or_default() += 1;
3575118×
    }
357661×
    let mut shared = 0usize;
3577113×
    for l in split_lines(b) {
3578113×
        if let Some(n) = counts.get_mut(l) {
357944×
            if *n > 0 {
358044×
                *n -= 1;
358144×
                shared += l.len();
358244×
            }
358369×
        }
3584
    }
358561×
    (shared * 100 / a.len().max(b.len())) as u32
358691×
}
3587
3588
#[cfg(test)]
3589
mod join_tests {
3590
    use super::*;
3591
3592
    #[test]
35931×
    fn missing_from_finds_what_the_other_lacks() {
35941×
        let a: BTreeMap<String, u8> = [("a", 1), ("b", 2), ("c/d", 3), ("e", 4)]
35951×
            .into_iter()
35964×
            .map(|(k, v)| (k.to_string(), v))
35971×
            .collect();
35981×
        let b: BTreeMap<String, ()> = [("0", ()), ("b", ()), ("c", ()), ("e", ())]
35991×
            .into_iter()
36004×
            .map(|(k, v)| (k.to_string(), v))
36011×
            .collect();
36021×
        let got: Vec<&str> = missing_from(&a, &b)
36031×
            .iter()
36042×
            .map(|(k, _)| k.as_str())
36051×
            .collect();
36061×
        assert_eq!(got, vec!["a", "c/d"]);
36071×
    }
3608
3609
    #[test]
36101×
    fn join_pairs_every_key_in_order() {
36111×
        let a: BTreeMap<String, u8> = [("a", 1), ("b/c", 2), ("d", 3)]
36121×
            .into_iter()
36133×
            .map(|(k, v)| (k.to_string(), v))
36141×
            .collect();
36151×
        let b: BTreeMap<String, char> = [("b", 'x'), ("b/c", 'y'), ("e", 'z')]
36161×
            .into_iter()
36173×
            .map(|(k, v)| (k.to_string(), v))
36181×
            .collect();
36191×
        let got: Vec<(String, Option<u8>, Option<char>)> = join(&a, &b)
36205×
            .map(|(k, x, y)| (k.clone(), x.copied(), y.copied()))
36211×
            .collect();
36221×
        let want = vec![
36231×
            ("a".to_string(), Some(1), None),
36241×
            ("b".to_string(), None, Some('x')),
36251×
            ("b/c".to_string(), Some(2), Some('y')),
36261×
            ("d".to_string(), Some(3), None),
36271×
            ("e".to_string(), None, Some('z')),
3628
        ];
36291×
        assert_eq!(got, want);
36301×
    }
3631
}