OXYGENHUB

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

2568 of 2813 lines run, 341 of 471 functions · run #398 on subrepo-pin-state · 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).
212×
pub fn has_conflict_markers(content: &[u8]) -> bool {
224×
    split_lines(content).iter().any(|l| {
234×
        l.starts_with(b"<<<<<<< oxy conflict")
242×
            || l.starts_with(b"======= oxy conflict")
252×
            || l.starts_with(b">>>>>>> oxy conflict")
264×
    })
272×
}
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.
3443×
pub fn has_own_conflict_markers(content: &[u8], materialized: &[u8], plain: &[&[u8]]) -> bool {
35564×
    let line = |l: &[u8]| -> Vec<u8> {
36564×
        let l = l.strip_suffix(b"\n").unwrap_or(l);
37564×
        l.strip_suffix(b"\r").unwrap_or(l).to_vec()
38564×
    };
39313×
    let marker = |l: &[u8]| {
40313×
        l.starts_with(b"<<<<<<< oxy conflict")
41266×
            || l.starts_with(b"======= oxy conflict")
42219×
            || l.starts_with(b">>>>>>> oxy conflict")
43313×
    };
4496×
    let unconflicted: HashSet<Vec<u8>> = plain.iter().map(|l| line(l)).collect();
4543×
    let own: HashSet<Vec<u8>> = split_lines(materialized)
4643×
        .into_iter()
4743×
        .map(line)
48313×
        .filter(|l| marker(l) && !unconflicted.contains(l))
4943×
        .collect();
5043×
    !own.is_empty()
5143×
        && split_lines(content)
5243×
            .into_iter()
53155×
            .any(|l| own.contains(&line(l)))
5443×
}
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
    /// The conflicts that keep their markers, by number, and why.
121
    pub left: Vec<(usize, &'static str)>,
122
}
123
124
/// `text` (a working-copy file) with the marker block of each conflict that
125
/// `sides` (from [`State::both_sides`]) says keeping both settles replaced
126
/// by its sides' lines; the conflicts settled, and those left and why.
1278×
pub(crate) fn keep_both_text(
1288×
    text: &[u8],
1298×
    sides: &[Option<Vec<Vec<u8>>>],
1308×
    reverse: bool,
1318×
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) {
132
    // A side as the markers hold it: the renderer ends each with a newline.
13322×
    let with_newline = |s: &[u8]| {
13422×
        let mut s = s.to_vec();
13522×
        if !s.is_empty() && !s.ends_with(b"\n") {
1360×
            s.push(b'\n');
13722×
        }
13822×
        s
13922×
    };
1401378×
    let lines: Vec<&[u8]> = text.split_inclusive(|&b| b == b'\n').collect();
1418×
    let (mut out, mut settled, mut left) = (Vec::new(), Vec::new(), Vec::new());
1428×
    let mut i = 0;
14332×
    while i < lines.len() {
14424×
        let Some((n, total)) = conflict_header(lines[i]) else {
14516×
            out.extend_from_slice(lines[i]);
14616×
            i += 1;
14716×
            continue;
148
        };
1498×
        let end = format!(">>>>>>> oxy conflict {n} of {total} end");
1508×
        let next_side = format!("======= oxy conflict {n} of {total}, side ");
1518×
        let Some(stop) =
15232×
            (i + 1..lines.len()).find(|&j| lines[j].trim_ascii_end() == end.as_bytes())
153
        else {
1540×
            left.push((n, "its end marker is missing; edit it by hand"));
1550×
            out.extend_from_slice(lines[i]);
1560×
            i += 1;
1570×
            continue;
158
        };
1598×
        let mut found: Vec<Vec<u8>> = vec![Vec::new()];
16024×
        for line in &lines[i + 1..stop] {
16124×
            if line.starts_with(next_side.as_bytes()) {
1628×
                found.push(Vec::new());
16316×
            } else {
16416×
                found.last_mut().unwrap().extend_from_slice(line);
16516×
            }
166
        }
1678×
        let wanted = match sides.get(n.wrapping_sub(1)) {
1688×
            _ if total != sides.len() => {
1690×
                Err("the markers are not the file's conflicts now; see `oxy status`")
170
            }
1716×
            Some(Some(w)) => Ok(w),
1722×
            _ => Err("both sides changed these lines; edit the markers"),
173
        };
1748×
        let keep = wanted.and_then(|w| {
17512×
            let mut a: Vec<Vec<u8>> = w.iter().map(|s| with_newline(s)).collect();
1766×
            let mut b = found.clone();
1776×
            a.sort();
1786×
            b.sort();
1796×
            if a == b {
1805×
                Ok(w)
181
            } else {
1821×
                Err("the lines between its markers were edited; finish it by hand")
183
            }
1846×
        });
1858×
        match keep {
1865×
            Ok(w) => {
1875×
                let order: Vec<&Vec<u8>> = if reverse {
1881×
                    w.iter().rev().collect()
189
                } else {
1904×
                    w.iter().collect()
191
                };
19210×
                for s in order {
19310×
                    out.extend_from_slice(&with_newline(s));
19410×
                }
1955×
                settled.push(n);
196
            }
1973×
            Err(why) => {
1983×
                left.push((n, why));
19915×
                for line in &lines[i..=stop] {
20015×
                    out.extend_from_slice(line);
20115×
                }
202
            }
203
        }
2048×
        i = stop + 1;
205
    }
2068×
    (out, settled, left)
2078×
}
208
209
/// `(n, total)` from a conflict's opening marker (spec §9.5).
21024×
fn conflict_header(line: &[u8]) -> Option<(usize, usize)> {
21124×
    let rest = line.strip_prefix(b"<<<<<<< oxy conflict ")?;
2128×
    let rest = std::str::from_utf8(rest).ok()?;
2138×
    let (n, rest) = rest.split_once(" of ")?;
21416×
    let total = rest.split(|c: char| !c.is_ascii_digit()).next()?;
2158×
    Some((n.parse().ok()?, total.parse().ok()?))
21624×
}
217
21824116×
pub fn is_binary(content: &[u8]) -> bool {
21924116×
    content[..content.len().min(BINARY_SNIFF)].contains(&0)
22024116×
}
221
222
impl Repo {
223
    /// Diff the files on disk against `base` and build the operations of a
224
    /// change from `base` to the working directory. `reuse` maps paths to the
225
    /// NodeIds an earlier version of the same change assigned to new paths.
2263×
    pub(crate) fn build_against(
2273×
        &self,
2283×
        base: &State,
2293×
        base_snapshot: &Snapshot,
2303×
        reuse: HashMap<String, (NodeId, NodeKind)>,
2313×
    ) -> Result<BuiltChange> {
2323×
        let mut fs_entries =
2333×
            self.walk_working_copy_with(None, &Self::tracked_kinds(base_snapshot), None)?;
2343×
        self.platform_normalize(&mut fs_entries, base_snapshot);
2353×
        self.build_from_entries(base, base_snapshot, reuse, &fs_entries)
2363×
    }
237
238
    /// Read the working copy as the platform left it (spec §7.7): without
239
    /// executable bits a file keeps the bit it has in the base, and without
240
    /// symbolic links a plain file where the base has a symlink is that
241
    /// symlink, written out as its target, not a new file.
2421565×
    fn platform_normalize(&self, entries: &mut BTreeMap<String, FsEntry>, base: &Snapshot) {
2431565×
        let p = self.platform;
2441565×
        if p.exec_bits && p.symlinks {
2451563×
            return;
2462×
        }
2474×
        for (path, e) in entries.iter_mut() {
2484×
            let Some(f) = base.files.get(path) else {
2490×
                continue;
250
            };
2514×
            if !p.symlinks && f.kind == NodeKind::Symlink && e.kind == NodeKind::File {
2522×
                e.kind = NodeKind::Symlink;
2532×
                e.exec = false;
2542×
            }
2554×
            if !p.exec_bits && f.kind == NodeKind::File && e.kind == NodeKind::File {
2562×
                e.exec = f.mode.exec;
2572×
            }
258
        }
2591565×
    }
260
261
    /// Like `build_against`, for files given in memory (imports).
2621285×
    pub(crate) fn build_from_entries(
2631285×
        &self,
2641285×
        base: &State,
2651285×
        base_snapshot: &Snapshot,
2661285×
        reuse: HashMap<String, (NodeId, NodeKind)>,
2671285×
        fs_entries: &BTreeMap<String, FsEntry>,
2681285×
    ) -> Result<BuiltChange> {
2691285×
        self.build_from_entries_resolving(base, base_snapshot, reuse, fs_entries, &HashSet::new())
2701285×
    }
271
272
    /// Like `build_from_entries`, settling the replace conflicts of the
273
    /// opaque files in `resolve` to their content (spec §9.3, §9.6): a
274
    /// caller that knows the user chose it, such as the hub's resolver. A
275
    /// snapshot never settles one on its own, since a checkout writes one
276
    /// candidate at the path.
2771303×
    pub(crate) fn build_from_entries_resolving(
2781303×
        &self,
2791303×
        base: &State,
2801303×
        base_snapshot: &Snapshot,
2811303×
        reuse: HashMap<String, (NodeId, NodeKind)>,
2821303×
        fs_entries: &BTreeMap<String, FsEntry>,
2831303×
        resolve: &HashSet<String>,
2841303×
    ) -> Result<BuiltChange> {
2851303×
        self.build_from_entries_with(
2861303×
            base,
2871303×
            base_snapshot,
2881303×
            reuse,
2891303×
            fs_entries,
2901303×
            resolve,
2911303×
            Scope::default(),
292
        )
2931303×
    }
294
295
    /// `build_from_entries` whose new nodes take identifiers derived from
296
    /// `seed` and their paths instead of random ones, so the same input
297
    /// always builds the same change (a Git import, spec §23).
298
    ///
299
    /// With `dir_needed`, the snapshot and entries hold only the paths the
300
    /// import touches (and the directories a removal may leave empty); a
301
    /// directory stays while `dir_needed` says the new tree has something
302
    /// under it.
30388×
    pub(crate) fn build_from_entries_seeded(
30488×
        &self,
30588×
        base: &State,
30688×
        base_snapshot: &Snapshot,
30788×
        fs_entries: &BTreeMap<String, FsEntry>,
30888×
        seed: [u8; 32],
30988×
        dir_needed: Option<&dyn Fn(&str) -> bool>,
31088×
    ) -> Result<BuiltChange> {
31188×
        self.build_from_entries_with(
31288×
            base,
31388×
            base_snapshot,
31488×
            HashMap::new(),
31588×
            fs_entries,
31688×
            &HashSet::new(),
31788×
            Scope {
31888×
                seed: Some(seed),
31988×
                dir_needed,
32088×
            },
321
        )
32288×
    }
323
324
    /// `build_from_entries` for a snapshot most of whose files are known
325
    /// unchanged (oxygen#41): only the paths that may differ are built
326
    /// (files read rather than known unchanged, base files gone from disk,
327
    /// and the directories those may leave empty), against a snapshot of
328
    /// just those, so a large tree with a few edits costs what they touch.
3291015×
    fn build_changed_only(
3301015×
        &self,
3311015×
        base: &State,
3321015×
        base_snapshot: &Snapshot,
3331015×
        reuse: HashMap<String, (NodeId, NodeKind)>,
3341015×
        fs_entries: &BTreeMap<String, FsEntry>,
3351015×
    ) -> Result<BuiltChange> {
3361015×
        let pick = crate::timing::phase("snapshot:   of which picking the changed");
337
        // A file known unchanged may still have changed mode: that alters
338
        // neither its size nor its time.
3391015×
        let changed: BTreeMap<String, FsEntry> = join(fs_entries, &base_snapshot.files)
34011235×
            .filter_map(|(p, e, f)| {
34111235×
                let e = e?;
34211106×
                (!e.unchanged || f.is_none_or(|f| f.kind != e.kind || f.mode.exec != e.exec))
34311106×
                    .then(|| (p.clone(), e.clone()))
34411235×
            })
3451015×
            .collect();
3461015×
        let mut files: BTreeMap<String, crate::state::FileState> = BTreeMap::new();
3476877×
        for p in changed.keys() {
3486877×
            if let Some(f) = base_snapshot.files.get(p) {
3493792×
                files.insert(p.clone(), f.clone());
3503792×
            }
351
        }
352
        // Paths the base has and the disk does not.
3531015×
        let gone = join(&base_snapshot.files, fs_entries)
35411235×
            .filter_map(|(p, f, e)| (e.is_none()).then_some((p, f?)));
3551015×
        for (p, f) in gone {
356129×
            if f.kind == NodeKind::Dir {
357101×
                continue;
35828×
            }
35928×
            files.insert(p.clone(), f.clone());
36028×
            let mut end = p.len();
36133×
            while let Some(i) = p[..end].rfind('/') {
3625×
                let dir = &p[..i];
3635×
                if files.contains_key(dir) {
3640×
                    break;
3655×
                }
3665×
                if let Some(d) = base_snapshot.files.get(dir) {
3675×
                    files.insert(dir.to_string(), d.clone());
3685×
                }
3695×
                end = i;
370
            }
371
        }
3721015×
        let part = Snapshot {
3731015×
            tree: base_snapshot.tree.clone(),
3741015×
            files,
3751015×
            unrendered: BTreeSet::new(),
3761015×
        };
3771015×
        drop(pick);
3781015×
        let _build = crate::timing::phase("snapshot:   of which building the change");
379
        // Whether anything on disk is under `dir`.
3801015×
        let dir_needed = |dir: &str| {
3815×
            let prefix = format!("{dir}/");
3825×
            fs_entries
3835×
                .range(prefix.clone()..)
3845×
                .next()
3855×
                .is_some_and(|(k, _)| k.starts_with(&prefix))
3865×
        };
3871015×
        self.build_from_entries_with(
3881015×
            base,
3891015×
            &part,
3901015×
            reuse,
3911015×
            &changed,
3921015×
            &HashSet::new(),
3931015×
            Scope {
3941015×
                seed: None,
3951015×
                dir_needed: Some(&dir_needed),
3961015×
            },
397
        )
3981015×
    }
399
4002406×
    fn build_from_entries_with(
4012406×
        &self,
4022406×
        base: &State,
4032406×
        base_snapshot: &Snapshot,
4042406×
        reuse: HashMap<String, (NodeId, NodeKind)>,
4052406×
        fs_entries: &BTreeMap<String, FsEntry>,
4062406×
        resolve: &HashSet<String>,
4072406×
        scope: Scope,
4082406×
    ) -> Result<BuiltChange> {
4092406×
        let Scope { seed, dir_needed } = scope;
4102406×
        let mut laps = crate::timing::Laps::start();
411
        // Attributes come from the files being committed, so a new
412
        // `.oxyattributes` applies to the same change that adds it.
4132406×
        let attrs = crate::attributes::Attributes::from_files(
4142406×
            fs_entries
4152406×
                .iter()
41619501×
                .filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes"))
4172406×
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
418
        );
4192406×
        let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs);
4202406×
        b.resolve = resolve.clone();
4212406×
        b.node_seed = seed;
4222406×
        laps.lap("build: builder");
423
        // Rename detection (spec §7.1, §23): a removed file and an added file
424
        // of the same kind whose contents are at least half alike become one
425
        // move of the same node, so identity survives across the rename.
4262406×
        let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p));
4272406×
        let renamed_from: HashMap<&str, &str> = renames
4282406×
            .iter()
4292406×
            .map(|(old, new)| (new.as_str(), old.as_str()))
4302406×
            .collect();
4312406×
        let renamed_to: HashMap<&str, &str> = renames
4322406×
            .iter()
4332406×
            .map(|(old, new)| (old.as_str(), new.as_str()))
4342406×
            .collect();
4352406×
        laps.lap("build: renames");
4362406×
        b.diff_in_parallel(fs_entries);
4372406×
        laps.lap("build: diffs");
43819501×
        for (path, fs_entry) in fs_entries {
43919501×
            match base_snapshot.files.get(path) {
4406241×
                None => match renamed_from.get(path.as_str()) {
44135×
                    Some(old) => {
44235×
                        b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])?
443
                    }
4446206×
                    None => b.add_path(self, path, fs_entry)?,
445
                },
44613260×
                Some(expected) => b.update_path(self, path, fs_entry, expected)?,
447
            }
448
        }
4492406×
        laps.lap("build: paths");
450
        // A directory stays while anything under it exists or is out of
451
        // scope: the directories above each such path, gathered once.
4522406×
        let mut needed_dirs: HashSet<&str> = HashSet::new();
45319514×
        fn parents_of<'a>(p: &'a str, set: &mut HashSet<&'a str>) {
45419514×
            let mut end = p.len();
45520229×
            while let Some(i) = p[..end].rfind('/') {
45615499×
                if !set.insert(&p[..i]) {
45714784×
                    break;
458715×
                }
459715×
                end = i;
460
            }
46119514×
        }
46219501×
        for p in fs_entries.keys() {
46319501×
            parents_of(p, &mut needed_dirs);
46419501×
        }
46513860×
        for p in base_snapshot.files.keys() {
46613860×
            if !self.in_scope(p) {
46713×
                parents_of(p, &mut needed_dirs);
46813847×
            }
469
        }
47013860×
        for (path, expected) in &base_snapshot.files {
47113860×
            if fs_entries.contains_key(path)
472600×
                || !self.in_scope(path)
473587×
                || renamed_to.contains_key(path.as_str())
474
            {
47513308×
                continue;
476552×
            }
477552×
            let still_needed = expected.kind == NodeKind::Dir
478465×
                && match dir_needed {
47921×
                    Some(f) => f(path),
480444×
                    None => needed_dirs.contains(path.as_str()),
481
                };
482552×
            if !still_needed {
483111×
                b.remove_path(path, expected);
484441×
            }
485
        }
4862406×
        laps.lap("build: removals");
4872406×
        if seed.is_some() {
48888×
            b.retire_stray_placements();
4892318×
        }
4902406×
        b.retire_unified();
4912406×
        laps.lap("build: stray placements");
4922406×
        let mut statuses = b.statuses;
4935712×
        statuses.sort_by(|a, b| a.path.cmp(&b.path));
4942406×
        Ok(BuiltChange {
4952406×
            tree_ops: b.tree_ops,
4962406×
            edits: b.edits,
4972406×
            contributors: b.contributors.into_iter().collect(),
4982406×
            resolves: b.resolves,
4992406×
            statuses,
5002406×
            blobs: b.blobs,
5012406×
        })
5022406×
    }
503
504
    /// Store a change and, when `core.sign = change` and we are its author,
505
    /// sign it. A rewritten change by another author is stored unsigned: only
506
    /// its author (or a delegate) can vouch for it.
507801×
    pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> {
508801×
        let ours = change.author == self.author();
509801×
        if self.config.sign_changes && ours && self.secret_key()?.is_none() {
5101×
            return fail("core.sign is \"change\" but no secret key is available for author.key");
511800×
        }
512800×
        let storing = crate::timing::phase("store: the change");
513800×
        let hash = self.store.put_change(change)?;
514800×
        drop(storing);
515800×
        if self.config.sign_changes && ours {
51643×
            let _t = crate::timing::phase("store: sign it");
51743×
            self.sign_change(&hash)?;
518757×
        }
519800×
        Ok(hash)
520801×
    }
521
5221569×
    fn resolutions_path(&self) -> PathBuf {
5231569×
        self.oxy
5241569×
            .join("workspaces")
5251569×
            .join(self.workspace.to_hex())
5261569×
            .join("resolve")
5271569×
    }
528
529
    /// The paths whose replace conflicts the user chose to settle with what
530
    /// the working copy holds there (`oxy resolve PATH`, spec §9.3).
5311565×
    pub fn resolutions(&self) -> Result<BTreeSet<String>> {
5321565×
        match fs::read_to_string(self.resolutions_path()) {
5335×
            Ok(text) => Ok(text
5345×
                .lines()
5355×
                .filter(|l| !l.is_empty())
5365×
                .map(String::from)
5375×
                .collect()),
5381560×
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()),
5390×
            Err(e) => Err(e.into()),
540
        }
5411565×
    }
542
543
    /// Settle the replace conflicts at `paths` (a file's or a subrepo pin's,
544
    /// spec §9.3) with what the working copy holds there: the next snapshot
545
    /// replaces every live candidate with it. Refused for a path whose base
546
    /// has no replace conflict.
5473×
    pub fn choose_resolutions(&self, base: &Snapshot, paths: &[String]) -> Result<()> {
5483×
        for p in paths {
5493×
            if !has_replace_conflict(base, p) {
5501×
                return Err(RepoError::Msg(format!(
5511×
                    "{p} has no replace conflict to settle (a text file's conflict is settled by editing out its markers, or `oxy resolve --both {p}`)"
5521×
                )));
5532×
            }
554
        }
5552×
        let mut all = self.resolutions()?;
5562×
        all.extend(paths.iter().cloned());
5572×
        let text: String = all.iter().map(|p| format!("{p}\n")).collect();
5582×
        crate::durable::write_atomic(&self.resolutions_path(), text.as_bytes())?;
5592×
        Ok(())
5603×
    }
561
562
    /// Settle the adjacent-insertion conflicts of the text files at `paths`
563
    /// (oxygen#121) by keeping every side, earliest change first (latest
564
    /// first with `reverse`): each one's markers in the working copy give
565
    /// way to its sides' lines, and the next snapshot records the result.
566
    /// A conflict whose sides changed the same lines, or whose text between
567
    /// the markers was edited, keeps its markers. With no paths, every
568
    /// conflicted text file.
5694×
    pub fn keep_both(
5704×
        &self,
5714×
        snap: &SnapshotResult,
5724×
        paths: &[String],
5734×
        reverse: bool,
5744×
    ) -> Result<Vec<KeptBoth>> {
5754×
        let paths: Vec<String> = if paths.is_empty() {
5762×
            snap.base_snapshot
5772×
                .files
5782×
                .iter()
5792×
                .filter(|(_, f)| matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted()))
5802×
                .map(|(p, _)| p.clone())
5812×
                .collect()
582
        } else {
5832×
            paths.to_vec()
584
        };
5854×
        let mut out = Vec::new();
5864×
        for p in paths {
5874×
            let conflicted = match snap.base_snapshot.files.get(&p).map(|f| (f, &f.content)) {
5884×
                Some((f, FileContent::Text { materialized, .. }))
5894×
                    if materialized.is_conflicted() =>
590
                {
5914×
                    f
592
                }
5930×
                Some((_, FileContent::Opaque { blobs })) if blobs.len() > 1 => {
5940×
                    return Err(RepoError::Msg(format!(
5950×
                        "{p} has a replace conflict: keep one version with `oxy resolve {p}`"
5960×
                    )))
597
                }
598
                _ => {
5990×
                    return Err(RepoError::Msg(format!(
6000×
                        "{p} has no text conflict to settle"
6010×
                    )))
602
                }
603
            };
6044×
            let sides = snap.base.both_sides(&conflicted.node);
6054×
            let disk = self.root().join(&p);
6064×
            let text = fs::read(&disk)?;
6074×
            let (bytes, settled, left) = keep_both_text(&text, &sides, reverse);
6084×
            if !settled.is_empty() {
6093×
                fs::write(&disk, bytes)?;
6101×
            }
6114×
            out.push(KeptBoth {
6124×
                path: p,
6134×
                settled,
6144×
                left,
6154×
            });
616
        }
6174×
        Ok(out)
6184×
    }
619
620
    /// Settle the adjacent insertions of each conflicted text file whose
621
    /// `.oxyattributes` say `merge=union` (oxygen#145) by keeping both
622
    /// sides, earliest change first, as [`Repo::keep_both`] does: a
623
    /// checkout calls it, and the next commit records the resolution. The
624
    /// files it settled something in.
62565×
    pub(crate) fn settle_unions(&self, command: &str) -> Result<Vec<KeptBoth>> {
62665×
        let snap = self.snapshot(command)?;
62765×
        let paths: Vec<String> = snap
62865×
            .base_snapshot
62965×
            .files
63065×
            .iter()
631282×
            .filter(|(p, f)| {
632232×
                matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted())
63356×
                    && snap.attributes.lookup(p).union
634282×
            })
63565×
            .map(|(p, _)| p.clone())
63665×
            .collect();
63765×
        if paths.is_empty() {
63864×
            return Ok(Vec::new());
6391×
        }
6401×
        let kept = self.keep_both(&snap, &paths, false)?;
6411×
        Ok(kept.into_iter().filter(|k| !k.settled.is_empty()).collect())
64265×
    }
643
644
    /// Every conflict of `snap`, `state`'s materialization, settled by
645
    /// keeping both sides (oxygen#145): the new text of each conflicted
646
    /// file, when each is a text file its `.oxyattributes` mark
647
    /// `merge=union` and each of its conflicts is an adjacent insertion;
648
    /// `None` when any conflict needs a person, or there is none. A host
649
    /// lands a proposal with these as a resolution, as a checkout settles
650
    /// them in a working copy.
6512×
    pub fn union_resolution(state: &State, snap: &Snapshot) -> Option<Vec<(String, Vec<u8>)>> {
6522×
        if snap.tree.is_conflicted() {
6530×
            return None;
6542×
        }
6552×
        let attrs = Repo::state_attributes(snap);
6562×
        let mut out = Vec::new();
6576×
        for (p, f) in &snap.files {
6586×
            if !f.is_conflicted() {
6594×
                continue;
6602×
            }
6612×
            let FileContent::Text { bytes, .. } = &f.content else {
6620×
                return None;
663
            };
6642×
            if !attrs.lookup(p).union {
6650×
                return None;
6662×
            }
6672×
            let sides = state.both_sides(&f.node);
6682×
            let (text, _, left) = keep_both_text(bytes, &sides, false);
6692×
            if !left.is_empty() || sides.iter().any(Option::is_none) {
6701×
                return None;
6711×
            }
6721×
            out.push((p.clone(), text));
673
        }
6741×
        (!out.is_empty()).then_some(out)
6752×
    }
676
677
    /// The chosen resolutions `base` still needs; the rest are forgotten.
6781562×
    fn resolutions_for(&self, base: &Snapshot) -> Result<HashSet<String>> {
6791562×
        let all = self.resolutions()?;
6801562×
        if all.is_empty() {
6811557×
            return Ok(HashSet::new());
6825×
        }
6835×
        let live: HashSet<String> = all
6845×
            .iter()
6855×
            .filter(|p| has_replace_conflict(base, p))
6865×
            .cloned()
6875×
            .collect();
6885×
        if live.is_empty() {
6892×
            match fs::remove_file(self.resolutions_path()) {
6902×
                Ok(()) => {}
6910×
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
6920×
                Err(e) => return Err(e.into()),
693
            }
6943×
        }
6955×
        Ok(live)
6961562×
    }
697
698
    /// Snapshot the working copy into the working-copy change (spec §12.3).
699
    /// The result carries the full base state and its whole rendering.
700545×
    pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> {
701545×
        self.snapshot_as(command, false, false)
702545×
    }
703
704
    /// Snapshot the working copy as [`Repo::snapshot`] does, for commands
705
    /// that need its statuses, conflicts and base history, and the graphs
706
    /// of the files it changes: status, log, show, commit, diff, hunks and
707
    /// discard. Text files known unchanged since the last manifest are
708
    /// neither read nor rendered: the result's `base` is a history-only
709
    /// state without their graphs, and its `base_snapshot` lists them in
710
    /// `unrendered` with empty bytes. They are never conflicted, and no
711
    /// change the working copy makes touches them.
712468×
    pub fn snapshot_status(&self, command: &str) -> Result<SnapshotResult> {
713468×
        self.snapshot_as(command, true, false)
714468×
    }
715
716
    /// `snapshot_as`; `for_commit` writes the manifest even where a status
717
    /// would not bother (an edited file is read next time whatever it
718
    /// says), since after the commit that file is unchanged, and records no
719
    /// operation: the commit's own, recorded straight after, replaces the
720
    /// working copy, so the snapshot's would cost a durable write for
721
    /// nothing. Should the commit fail, the files are still on disk and the
722
    /// next snapshot finds them.
7231565×
    fn snapshot_as(
7241565×
        &self,
7251565×
        command: &str,
7261565×
        partial: bool,
7271565×
        for_commit: bool,
7281565×
    ) -> Result<SnapshotResult> {
7291565×
        let _lock = self.lock_snapshot()?;
7301565×
        let batch = self.store.begin_batch()?;
7311565×
        let result = self.snapshot_locked(command, partial, for_commit);
7321565×
        batch.finish()?;
7331565×
        result
7341565×
    }
735
7361565×
    fn snapshot_locked(
7371565×
        &self,
7381565×
        command: &str,
7391565×
        partial: bool,
7401565×
        for_commit: bool,
7411565×
    ) -> Result<SnapshotResult> {
7421565×
        let _all = crate::timing::phase("snapshot");
7431565×
        let (op_ref, op) = self.head_op()?;
7441565×
        let (old_hash, old_wc) = self.working_copy()?;
745
        // The base's history first: its graph keys say which files the
746
        // manifest already knows equal to it.
7471563×
        let mut base = {
7481563×
            let _t = crate::timing::phase("snapshot: load base history");
7491563×
            self.load_state_indexed(&old_wc.deps)?
750
        };
7511563×
        let view = {
7521563×
            let _t = crate::timing::phase("snapshot: tree view");
7531563×
            self.tree_view(&mut base)?
754
        };
7551563×
        let manifest = {
7561563×
            let _t = crate::timing::phase("snapshot: read manifest");
7571563×
            self.read_manifest()?.map(|(_, _, m)| m)
758
        };
7591563×
        let tracked: BTreeMap<String, (NodeKind, Option<ObjectRef>)> = {
7601563×
            let _t = crate::timing::phase("snapshot: tracked paths");
761
            // In the view's path order, so the map is built without searching.
7621563×
            view.entries
7631563×
                .iter()
7649973×
                .filter(|(_, e)| e.kind != NodeKind::Dir)
7659534×
                .map(|(p, e)| {
7669534×
                    let blob = if e.kind != NodeKind::File || e.mode.opaque {
767188×
                        base.live_blobs(&e.node).first().copied()
768
                    } else {
7699346×
                        None
770
                    };
7719534×
                    (p.clone(), (e.kind, blob))
7729534×
                })
7731563×
                .collect()
774
        };
775
        // A text file's graph key, worked out when the walk asks for it.
77612639×
        let key_of = |p: &str| -> Option<[u8; 32]> {
77712639×
            let e = view.entries.get(p)?;
7789423×
            if e.kind != NodeKind::File || e.mode.opaque {
779143×
                return None;
7809280×
            }
7819280×
            base.graph_key(&e.node)
78212639×
        };
7831562×
        let mut fs_entries = {
7841563×
            let _t = crate::timing::phase("snapshot: read working copy");
7851563×
            self.walk_working_copy_with(manifest.as_deref(), &tracked, Some(&key_of))?
786
        };
787
        // The keys of the files read, before building graphs drops them: a
788
        // file found equal to its base rendering keeps its key.
7891562×
        let keys: HashMap<String, [u8; 32]> = fs_entries
7901562×
            .iter()
79112691×
            .filter(|(_, e)| !e.unchanged && e.kind == NodeKind::File)
7928398×
            .filter_map(|(p, _)| Some((p.clone(), key_of(p)?)))
7931562×
            .collect();
7941562×
        let base_snapshot = {
7951562×
            let _t = crate::timing::phase("snapshot: load and render base");
7961562×
            if partial {
7971020×
                let sets = crate::timing::phase("snapshot:   of which choosing graphs");
798
                // Every file but those on disk known unchanged.
7991020×
                let need: Vec<(&str, NodeId)> = join(&view.entries, &fs_entries)
80011257×
                    .filter_map(|(p, v, f)| {
80111257×
                        let v = v?;
8028170×
                        (v.kind == NodeKind::File && !f.is_some_and(|f| f.unchanged))
8038170×
                            .then_some((p.as_str(), v.node))
80411257×
                    })
8051020×
                    .collect();
8061020×
                let render: std::collections::HashSet<&str> =
8071020×
                    need.iter().map(|(p, _)| *p).collect();
8081020×
                let need: Vec<NodeId> = need.iter().map(|(_, n)| *n).collect();
8091020×
                drop(sets);
810
                {
8111020×
                    let _t = crate::timing::phase("snapshot:   of which building graphs");
8121020×
                    self.load_graphs_of(&mut base, &need)?;
813
                }
8141020×
                let _t = crate::timing::phase("snapshot:   of which rendering");
8151020×
                base.materialize_only(&render)
816
            } else {
817542×
                self.load_all_graphs(&mut base)?;
818542×
                base.materialize()
819
            }
820
        };
821
822
        // Reuse the NodeIds the previous working-copy version assigned to new
823
        // paths, so an unchanged working copy hashes identically (spec §12.3).
8241562×
        let reuse = added_node_paths(&old_wc, &base_snapshot);
8251562×
        self.platform_normalize(&mut fs_entries, &base_snapshot);
826
        // Replace conflicts the user settled with `oxy resolve PATH`
827
        // (oxygen#140): those still in the base; a choice the base no longer
828
        // needs (once committed) is forgotten.
8291562×
        let resolve = self.resolutions_for(&base_snapshot)?;
8301562×
        let built = {
8311562×
            let _t = crate::timing::phase("snapshot: diff against base");
8321562×
            if !resolve.is_empty() {
8333×
                self.build_from_entries_resolving(
8343×
                    &base,
8353×
                    &base_snapshot,
8363×
                    reuse,
8373×
                    &fs_entries,
8383×
                    &resolve,
8390×
                )?
8401559×
            } else if partial && self.config.sparse_paths.is_empty() {
841
                #[cfg(test)]
842734×
                let full =
843734×
                    self.build_from_entries(&base, &base_snapshot, reuse.clone(), &fs_entries)?;
8441015×
                let built = self.build_changed_only(&base, &base_snapshot, reuse, &fs_entries)?;
845
                #[cfg(test)]
846
                {
847
                    // New nodes take random identifiers: compare them by
848
                    // order of appearance.
8491468×
                    let norm = |b: &BuiltChange| {
8501468×
                        let text = format!("{:?} {:?} {:?}", b.tree_ops, b.edits, b.statuses);
8511468×
                        let mut seen: Vec<String> = Vec::new();
8521468×
                        let mut out = String::new();
8531468×
                        let mut rest = text.as_str();
85420804×
                        while let Some(i) = rest.find("NodeId(") {
85519336×
                            out.push_str(&rest[..i]);
85619336×
                            let end = i + rest[i..].find(')').unwrap() + 1;
85719336×
                            let id = rest[i..end].to_string();
8589046622×
                            let n = seen.iter().position(|x| *x == id).unwrap_or_else(|| {
8597422×
                                seen.push(id);
8607422×
                                seen.len() - 1
8617422×
                            });
86219336×
                            out.push_str(&format!("N{n}"));
86319336×
                            rest = &rest[end..];
864
                        }
8651468×
                        out.push_str(rest);
8661468×
                        out
8671468×
                    };
868734×
                    assert_eq!(
869734×
                        norm(&built),
870734×
                        norm(&full),
871
                        "a changed-only build is the full one"
872
                    );
873
                }
8741015×
                built
875
            } else {
876544×
                self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)?
877
            }
878
        };
879
        // Text files found equal to an unconflicted base rendering keep its
880
        // graph key, so the next snapshot need not read or render them.
881
        {
8823895×
            let changed: HashSet<&str> = built.statuses.iter().map(|s| s.path.as_str()).collect();
88312691×
            for (p, e) in fs_entries.iter_mut() {
88412691×
                if e.unchanged || e.kind != NodeKind::File || changed.contains(p.as_str()) {
8858093×
                    continue;
8864598×
                }
8874598×
                e.graph_key = match base_snapshot.files.get(p).map(|f| &f.content) {
888
                    Some(FileContent::Text {
8894474×
                        bytes,
8904474×
                        materialized,
8914474×
                    }) if !materialized.is_conflicted() && *bytes == *e.content => {
8924408×
                        keys.get(p).copied()
893
                    }
894190×
                    _ => None,
895
                };
896
            }
897
        }
898
8991562×
        let mut wc = Change {
9001562×
            version: crate::objects::CHANGE_VERSION,
9011562×
            change_id: old_wc.change_id,
9021562×
            deps: vec![],
9031562×
            author: self.author(),
9041562×
            hlc: old_wc.hlc,
9051562×
            message: old_wc.message.clone(),
9061562×
            tree_ops: built.tree_ops,
9071562×
            edits: built.edits,
9081562×
            kind: ChangeKind::WorkingCopy,
9091562×
        };
9101562×
        let base_heads = base.maximal(&old_wc.deps);
9111562×
        let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect();
9121562×
        deps.extend(referenced_changes(&wc, &base));
9131562×
        deps.extend(built.contributors.iter().copied());
9141562×
        wc.deps = deps.into_iter().collect();
9151562×
        wc.normalize();
9161562×
        let resolves = built.resolves;
9171562×
        let contributors = built.contributors;
9181562×
        let statuses = built.statuses;
919
        // Empty snapshot: hash the candidate before storing; only record an op on change.
9201562×
        let record = crate::timing::phase("snapshot: hash and record");
921
        // Encoded once, its texts stored a batch at a time as they are
922
        // made (a stored blob is not stored again); the change itself only
923
        // when it changed.
924
        // For a commit the change is neither encoded, stored nor recorded:
925
        // the commit stores its own, built from this one's operations, and
926
        // its texts with it. The hash it would have is not needed.
9271562×
        let (new_hash, changed, encoded) = if for_commit {
928552×
            (old_hash, true, Vec::new())
929
        } else {
9301010×
            let encoded = wc.encode_streaming(|batch| self.store.put_texts(&batch))?;
9311010×
            let new_hash = crate::hash::hash_object(crate::hash::ObjectType::Change, &encoded);
9321010×
            (new_hash, new_hash != old_hash, encoded)
933
        };
9341562×
        if changed && !for_commit {
935307×
            self.store.put(crate::hash::ObjectType::Change, &encoded)?;
936307×
            let mut view = op.view.clone();
937307×
            view.workspaces.insert(self.workspace, new_hash);
938307×
            self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?;
9391255×
        }
9401562×
        drop(record);
941
        // The prompt compares against this without recording anything.
9421562×
        let _t = crate::timing::phase("snapshot: write manifest");
9431562×
        let conflicted = base_snapshot.is_conflicted();
9441562×
        let uncommitted = !wc.tree_ops.is_empty() || !wc.edits.is_empty();
945
        // Rewriting the manifest pays only when it would teach the next
946
        // snapshot something: other flags, a file found equal to the base
947
        // (it need not be read again), a new blob, a path added or gone.
948
        // An edited file is read next time whatever it says.
9491562×
        let worth = match self.read_manifest()? {
950552×
            _ if for_commit => true,
951363×
            None => true,
952647×
            Some((c, u, m)) => {
953647×
                c != conflicted
954647×
                    || u != uncommitted
955407×
                    || m.entries.len() != fs_entries.len()
956517×
                    || fs_entries.iter().any(|(p, e)| {
957517×
                        !e.unchanged && (e.graph_key.is_some() || !m.entries.contains_key(p))
958517×
                    })
95970×
                    || built
96070×
                        .blobs
96170×
                        .iter()
96270×
                        .any(|(p, b)| m.entries.get(p).and_then(|e| e.blob) != Some(*b))
963
                    // With uncommitted work, the status cache below holds
964
                    // only while the manifest describes the edited files as
965
                    // they are now.
96674×
                    || (uncommitted && statuses.iter().any(|st| {
96774×
                        let Some(e) = fs_entries.get(&st.path) else {
9684×
                            return m.entries.contains_key(&st.path);
969
                        };
97070×
                        let Some(me) = m.entries.get(&st.path) else {
9710×
                            return true;
972
                        };
97370×
                        let hash = e
97470×
                            .content_hash
97570×
                            .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes());
97670×
                        let stat = e.stat.or_else(|| {
9770×
                            fs::symlink_metadata(self.root.join(&st.path))
9780×
                                .ok()
9790×
                                .map(|md| (md.len(), super::prompt::mtime_ns(&md)))
9800×
                        });
981
                        // The executable bit too: a mode change moves
982
                        // neither size nor time.
98370×
                        me.hash != hash || me.exec != e.exec || stat != Some((me.size, me.mtime_ns))
98474×
                    }))
985
            }
986
        };
9871562×
        let attributes = crate::attributes::Attributes::from_files(
9881562×
            fs_entries
9891562×
                .iter()
99012691×
                .filter(|(p, _)| is_attributes(p))
9911562×
                .map(|(p, e)| (p.as_str(), e.content.as_slice())),
992
        );
9931562×
        let mut manifest = None;
9941562×
        if for_commit {
995552×
            manifest = Some(PendingManifest {
996552×
                entries: fs_entries,
997552×
                blobs: built.blobs.clone(),
998552×
                dirs: self.walked_dirs.take(),
999552×
                written: super::prompt::now_ns(),
1000552×
            });
1001552×
        } else {
10021010×
            if worth {
1003944×
                self.write_manifest_entries(conflicted, uncommitted, &fs_entries, &built.blobs)?;
100466×
            }
10051010×
            self.write_status_cache((uncommitted && !conflicted).then_some(&new_hash), &statuses);
1006
        }
10071562×
        Ok(SnapshotResult {
10081562×
            wc_hash: new_hash,
10091562×
            wc,
10101562×
            base,
10111562×
            base_snapshot,
10121562×
            statuses,
10131562×
            changed,
10141562×
            resolves,
10151562×
            contributors,
10161562×
            attributes,
10171562×
            untracked_nested: self.untracked_nested.borrow().clone(),
10181562×
            manifest,
10191562×
        })
10201565×
    }
1021
1022
    /// `oxy status --json` (spec §9.5): the changed paths and every conflict
1023
    /// of the working copy's base with its kind, path, and contributing
1024
    /// ChangeIds.
10258×
    pub fn status_json(&self, snap: &SnapshotResult) -> Result<String> {
10268×
        let heads = self.bookmark_heads()?;
10278×
        let base_snapshot = &snap.base_snapshot;
10288×
        let by_node = base_snapshot.tree.by_node();
10298×
        let path_of = |n: &NodeId| by_node.get(n).map(|e| e.path.clone());
10308×
        let id_of = |h: &ChangeHash| {
10314×
            snap.base
10324×
                .get(h)
10334×
                .map(|m| m.change_id.to_hex())
10344×
                .unwrap_or_else(|| h.to_hex())
10354×
        };
10368×
        let paths = json_array(snap.statuses.iter().map(|s| {
10374×
            format!(
1038
                "{{\"path\":{},\"status\":{}}}",
10394×
                json_str(&s.path),
10404×
                json_str(&format!("{:?}", s.status))
1041
            )
10424×
        }));
10438×
        let mut conflicts: Vec<String> = Vec::new();
10449×
        for (p, f) in &base_snapshot.files {
10459×
            if !f.is_conflicted() {
10467×
                continue;
10472×
            }
10482×
            let (kind, changes) = match &f.content {
1049
                FileContent::Text { .. } => {
10502×
                    ("text", snap.base.conflict_regions(&f.node).contributors)
1051
                }
10520×
                FileContent::Opaque { blobs } => {
10530×
                    ("replace", snap.base.blob_contributors(&f.node, blobs))
1054
                }
10550×
                FileContent::Dir => continue,
1056
            };
10572×
            let resolved = match snap
10582×
                .statuses
10592×
                .iter()
10602×
                .find(|s| s.path == *p)
10612×
                .map(|s| s.status)
1062
            {
10631×
                Some(ChangeStatus::Resolved) => "true",
10640×
                Some(ChangeStatus::ConflictEdited) => "\"markers\"",
10651×
                _ => "false",
1066
            };
10672×
            conflicts.push(format!(
1068
                "{{\"kind\":{},\"path\":{},\"node\":{},\"changes\":{},\"resolved\":{resolved}}}",
10692×
                json_str(kind),
10702×
                json_str(p),
10712×
                json_str(&f.node.to_hex()),
10724×
                json_array(changes.iter().map(|h| json_str(&id_of(h))))
1073
            ));
1074
        }
10758×
        for c in &base_snapshot.tree.conflicts {
10760×
            let (kind, nodes): (&str, Vec<NodeId>) = match c {
10770×
                crate::tree::TreeConflict::Name { nodes, .. } => ("name", nodes.clone()),
10780×
                crate::tree::TreeConflict::MultiPlacement { node, .. } => {
10790×
                    ("multi-placement", vec![*node])
1080
                }
10810×
                crate::tree::TreeConflict::Orphan { node, .. } => ("orphan", vec![*node]),
10820×
                crate::tree::TreeConflict::Cycle { node } => ("cycle", vec![*node]),
10830×
                crate::tree::TreeConflict::Mode { node, .. } => ("mode", vec![*node]),
1084
            };
10850×
            let detail = match c {
10860×
                crate::tree::TreeConflict::Name { name, parent, .. } => {
10870×
                    let dir = if *parent == NodeId::ROOT {
10880×
                        String::new()
1089
                    } else {
10900×
                        path_of(parent).unwrap_or_default()
1091
                    };
10920×
                    format!(
1093
                        ",\"path\":{}",
10940×
                        json_str(&if dir.is_empty() {
10950×
                            name.clone()
1096
                        } else {
10970×
                            format!("{dir}/{name}")
1098
                        })
1099
                    )
1100
                }
11010×
                _ => nodes
11020×
                    .first()
11030×
                    .and_then(path_of)
11040×
                    .map(|p| format!(",\"path\":{}", json_str(&p)))
11050×
                    .unwrap_or_default(),
1106
            };
11070×
            conflicts.push(format!(
1108
                "{{\"kind\":{},\"nodes\":{}{detail}}}",
11090×
                json_str(kind),
11100×
                json_array(nodes.iter().map(|n| json_str(&n.to_hex())))
1111
            ));
1112
        }
11138×
        Ok(format!(
1114
            "{{\"bookmark\":{},\"working_copy\":{},\"heads\":{},\"clean\":{},\"conflicted\":{},\"paths\":{paths},\"conflicts\":{},\"untracked_nested\":{},\"skipped_large\":{}}}",
11158×
            json_str(&self.config.bookmark),
11168×
            json_str(&snap.wc.change_id.to_hex()),
11178×
            heads.len(),
11188×
            snap.statuses.is_empty(),
11198×
            base_snapshot.is_conflicted(),
11208×
            json_array(conflicts),
11218×
            json_array(snap.untracked_nested.iter().map(|p| json_str(p))),
11228×
            json_array(
11238×
                self.skipped_large
11248×
                    .borrow()
11258×
                    .iter()
11268×
                    .map(|(p, n)| format!("{{\"path\":{},\"size\":{n}}}", json_str(p)))
1127
            )
1128
        ))
11298×
    }
1130
1131
    /// Turn the working-copy change into a normal change on the bookmark (spec §12.3).
1132736×
    pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> {
1133736×
        self.commit_selection(message, extra_deps, &Selection::All)
1134736×
    }
1135
1136
    /// The hunks of the working copy, for `oxy commit --interactive`: each
1137
    /// tree operation and each edit, in the order `select_hunks` numbers them.
11388×
    pub fn hunks(&self) -> Result<(SnapshotResult, Vec<Hunk>)> {
11398×
        let snap = self.snapshot_status("oxy commit")?;
11408×
        let hunks = render_hunks(&snap.wc, &snap.base, &snap.base_snapshot);
11418×
        Ok((snap, hunks))
11428×
    }
1143
1144
    /// Commit part of the working copy (spec §12.3). What is not selected
1145
    /// stays on disk as uncommitted work: the next snapshot rebuilds it
1146
    /// against the new base, so the working copy always describes the
1147
    /// directory.
1148
    /// The graph key in `st` of each text file at `paths`, the paths a
1149
    /// commit changed: only their graphs changed, so the keys the manifest
1150
    /// holds for the others still stand (and any that does not is simply
1151
    /// not trusted by the next snapshot, which compares keys). Files with
1152
    /// an `eol` attribute are left out: their bytes on disk are not the
1153
    /// rendering.
1154754×
    fn committed_graph_keys(
1155754×
        &self,
1156754×
        st: &State,
1157754×
        paths: &[String],
1158754×
        attrs: &crate::attributes::Attributes,
1159754×
    ) -> HashMap<String, [u8; 32]> {
1160754×
        let _t = crate::timing::phase("commit: graph keys");
1161754×
        let view = st.view();
1162754×
        paths
1163754×
            .iter()
11643599×
            .filter_map(|p| {
11653599×
                let e = view.entries.get(p)?;
11663559×
                (e.kind == NodeKind::File && !e.mode.opaque && attrs.lookup(p).eol.is_none())
11673559×
                    .then_some(())?;
11683488×
                Some((p.clone(), st.graph_key(&e.node)?))
11693599×
            })
1170754×
            .collect()
1171754×
    }
1172
1173743×
    pub fn commit_selection(
1174743×
        &self,
1175743×
        message: &str,
1176743×
        extra_deps: &[ChangeHash],
1177743×
        selection: &Selection,
1178743×
    ) -> Result<ChangeHash> {
1179743×
        Ok(self
1180743×
            .commit_selection_partial(message, extra_deps, selection)?
1181
            .0)
1182743×
    }
1183
1184
    /// As [`Repo::commit_selection`], and whether the commit left part of
1185
    /// the working copy uncommitted: when it did not, nothing is left, and
1186
    /// no snapshot is needed to say so.
1187772×
    pub fn commit_selection_partial(
1188772×
        &self,
1189772×
        message: &str,
1190772×
        extra_deps: &[ChangeHash],
1191772×
        selection: &Selection,
1192772×
    ) -> Result<(ChangeHash, bool)> {
1193
        // The commit's objects are written together, made durable at once
1194
        // before the operation that names them (spec §13.6).
1195772×
        let batch = self.store.begin_batch()?;
1196772×
        let out = self.commit_selection_batched(message, extra_deps, selection);
1197772×
        batch.finish()?;
1198772×
        out
1199772×
    }
1200
1201
    /// Write the manifest a commit's snapshot left to the commit, with the
1202
    /// graph keys `keys` sets.
1203540×
    fn write_pending_manifest(
1204540×
        &self,
1205540×
        mut m: PendingManifest,
1206540×
        conflicted: bool,
1207540×
        keys: HashMap<String, [u8; 32]>,
1208540×
    ) -> Result<()> {
12092899×
        for (path, k) in keys {
12102899×
            if let Some(e) = m.entries.get_mut(&path) {
12112899×
                e.graph_key = Some(k);
12122899×
            }
1213
        }
1214540×
        self.write_manifest_entries_as_of(
1215540×
            conflicted,
1216
            false,
1217540×
            &m.entries,
1218540×
            &m.blobs,
1219540×
            m.written,
1220540×
            m.dirs.unwrap_or_default(),
1221
        )
1222540×
    }
1223
1224772×
    fn commit_selection_batched(
1225772×
        &self,
1226772×
        message: &str,
1227772×
        extra_deps: &[ChangeHash],
1228772×
        selection: &Selection,
1229772×
    ) -> Result<(ChangeHash, bool)> {
1230772×
        let _wc = self.lock_working_copy()?;
1231772×
        let mut snap = match self.snapshot_from_cache()? {
1232220×
            Some(snap) => snap,
1233552×
            None => self.snapshot_as("oxy commit", true, true)?,
1234
        };
1235772×
        let pending = snap.manifest.take();
1236772×
        if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() {
12374×
            if let Some(m) = pending {
12384×
                let conflicted = snap.base_snapshot.is_conflicted();
12394×
                self.write_pending_manifest(m, conflicted, HashMap::new())?;
12400×
            }
12414×
            return fail("nothing to commit: the working copy matches its base");
1242768×
        }
1243768×
        if let Some(s) = snap
1244768×
            .statuses
1245768×
            .iter()
12463628×
            .find(|s| s.status == ChangeStatus::ConflictEdited)
1247
        {
12481×
            return fail(format!("{} still contains conflict markers", s.path));
1249767×
        }
1250767×
        if message.trim().is_empty() {
12510×
            return fail("a commit message is required");
1252767×
        }
1253
        // What the whole working copy references, before a whole commit
1254
        // takes its operations over (they are moved, not copied: for a
1255
        // large tree they are most of the memory).
1256767×
        let wc_refs = referenced_changes(&snap.wc, &snap.base);
12573626×
        let changed_paths: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
1258767×
        let (tree_ops, edits, partial) = match selection {
1259754×
            Selection::All => (
1260754×
                std::mem::take(&mut snap.wc.tree_ops),
1261754×
                std::mem::take(&mut snap.wc.edits),
1262754×
                false,
1263754×
            ),
12648×
            Selection::Paths(paths) => {
12658×
                let (o, e) = select_paths(&snap.wc, &snap.base_snapshot, paths);
12668×
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
12678×
                (o, e, partial)
1268
            }
12695×
            Selection::Hunks(indices) => {
12705×
                let (o, e) = select_hunks(&snap.wc, indices, &snap.base_snapshot.tree.unified);
12715×
                let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len();
12725×
                (o, e, partial)
1273
            }
1274
        };
1275767×
        if tree_ops.is_empty() && edits.is_empty() {
12760×
            return fail("nothing selected to commit");
1277767×
        }
1278
        // A resolution may leave other work uncommitted (oxygen#118), but
1279
        // takes every file it resolves, whole.
1280767×
        if partial && snap.resolves {
12812×
            let resolved = snap
12822×
                .statuses
12832×
                .iter()
12844×
                .filter(|s| s.status == ChangeStatus::Resolved)
12852×
                .map(|s| s.path.as_str());
12862×
            let missing: Vec<&str> = match selection {
12872×
                Selection::Paths(paths) => resolved
12882×
                    .filter(|p| {
12892×
                        !paths.iter().any(|x| {
12902×
                            p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))
12912×
                        })
12922×
                    })
12932×
                    .collect(),
12940×
                _ => resolved.collect(),
1295
            };
12962×
            if !missing.is_empty() {
12971×
                return fail(format!(
1298
                    "a conflict resolution takes every file it resolves, whole: commit by paths, including {}",
12991×
                    missing.join(", ")
1300
                ));
13011×
            }
1302765×
        }
1303
        // pre-commit hook: may veto.
1304766×
        self.run_hook(
1305766×
            "pre-commit",
1306766×
            &format!(
1307
                "{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}",
1308766×
                json_str(message),
13093624×
                json_array(snap.statuses.iter().map(|s| format!(
1310
                    "{{\"path\":{},\"status\":{}}}",
13113624×
                    json_str(&s.path),
13123624×
                    json_str(&format!("{:?}", s.status))
1313
                )))
1314
            ),
13151×
        )?;
1316765×
        let (op_ref, op) = self.head_op()?;
1317765×
        let mut deps: BTreeSet<ChangeHash> = wc_refs;
1318
        // semantic-deps hook: may add explicit dependencies (spec §8.1).
1319765×
        if self.config.semantic_deps {
13201×
            let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
13211×
            let by_node = snap.base_snapshot.tree.by_node();
13221×
            let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new();
13231×
            let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
13242×
            for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) {
13252×
                if !seen.insert(*h) {
13260×
                    continue;
13272×
                }
13282×
                let c = self.store.get_change(h)?;
13292×
                let mut paths: BTreeSet<String> = BTreeSet::new();
13306×
                for n in c
13312×
                    .tree_ops
13322×
                    .iter()
13333×
                    .map(|o| o.node())
13343×
                    .chain(c.edits.iter().map(|e| e.node()))
1335
                {
13366×
                    if let Some(e) = by_node.get(&n) {
13376×
                        paths.insert(e.path.clone());
13386×
                    }
1339
                }
13402×
                candidates.push((c.change_id, *h, paths.into_iter().collect()));
1341
            }
13421×
            for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? {
13431×
                if snap.base.contains(&d) {
13441×
                    deps.insert(d);
13451×
                }
1346
            }
1347764×
        }
1348
        // A resolution depends on every change that contributed to the
1349
        // conflict (spec §8.1). Tree and opaque conflicts are resolved through
1350
        // ordinary operations whose references already cover the contributors.
1351765×
        deps.extend(snap.contributors.iter().copied());
1352765×
        for d in extra_deps {
13534×
            if !snap.base.contains(d) {
13540×
                return fail(format!(
1355
                    "--depends-on {d}: not in the working copy's base state"
1356
                ));
13574×
            }
13584×
            deps.insert(*d);
1359
        }
1360765×
        let mut change = Change {
1361
            version: crate::objects::CHANGE_VERSION,
1362765×
            change_id: if partial {
136310×
                ChangeId::random()
1364
            } else {
1365755×
                snap.wc.change_id
1366
            },
1367765×
            deps: vec![],
1368765×
            author: self.author(),
1369765×
            hlc: Hlc::now(),
1370765×
            message: message.trim_end().to_string(),
1371765×
            tree_ops,
1372765×
            edits,
1373765×
            kind: if snap.resolves {
137413×
                ChangeKind::Resolution
1375
            } else {
1376752×
                ChangeKind::Normal
1377
            },
1378
        };
1379765×
        if partial {
1380
            // Dependencies come from what the selected part references, not
1381
            // from the whole working copy.
138210×
            deps = referenced_changes(&change, &snap.base);
138310×
            for d in extra_deps {
13841×
                deps.insert(*d);
13851×
            }
1386755×
        }
1387765×
        change.deps = deps.into_iter().collect();
1388765×
        change.normalize();
1389765×
        let signing = crate::timing::phase("commit: sign and store");
1390765×
        let hash = self.store_signed(&change)?;
1391764×
        drop(signing);
1392764×
        let extending = crate::timing::phase("commit: extend the state");
1393
1394
        // New bookmark heads: maximal elements of old heads plus the new change.
1395
        // The working copy continues on its own base plus the new change, which
1396
        // is exactly what the files on disk contain; the bookmark may hold
1397
        // other heads (and conflicts) the working copy has not checked out.
1398764×
        let old_heads = self.heads_of(&op.view, &self.config.bookmark)?;
1399764×
        let old_base = snap.base.maximal(&snap.wc.deps);
1400764×
        let mut all: Vec<ChangeHash> = old_heads.clone();
1401764×
        all.extend(old_base.iter().copied());
1402764×
        all.push(hash);
1403
        // The snapshot already loaded the working copy's base; when that also
1404
        // holds every bookmark head, it is the state to extend.
1405764×
        let mut st = if old_heads.iter().all(|h| snap.base.contains(h)) {
1406718×
            snap.base
1407
        } else {
140846×
            self.load_state(&all)?
1409
        };
1410764×
        st.add_hashed(hash, change)?;
1411764×
        let mut with_new = old_heads.clone();
1412764×
        with_new.push(hash);
1413764×
        let new_heads = st.maximal(&with_new);
1414
        // Automatic checkpoint once enough changes accumulated (spec §11).
1415764×
        let since = self
1416764×
            .latest_checkpoint(&self.config.bookmark)?
1417764×
            .map(|(_, cp)| cp.count)
1418764×
            .unwrap_or(0);
1419764×
        if self.config.checkpoint_interval > 0
1420764×
            && (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval
1421
        {
14222×
            let cp_state = self.load_state_full(&new_heads)?;
14232×
            self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?;
1424762×
        }
1425764×
        let mut wc_base = old_base.clone();
1426764×
        wc_base.push(hash);
1427764×
        let wc_base = st.maximal(&wc_base);
1428764×
        drop(extending);
1429
1430
        // The graph keys of the files as they now are, from the state the
1431
        // commit just extended: a file whose graph it built has none, and
1432
        // is read by the next snapshot; a key the next base does not share
1433
        // (the bookmark held other heads) is simply not trusted then.
1434764×
        let keying = crate::timing::phase("commit: graph keys of the files");
1435764×
        let committed_keys =
1436764×
            (!partial).then(|| self.committed_graph_keys(&st, &changed_paths, &snap.attributes));
1437764×
        drop(keying);
1438764×
        let recording = crate::timing::phase("commit: record");
1439764×
        let wc = self.new_working_copy(wc_base, "");
1440764×
        let wc_hash = self.store.put_change(&wc)?;
1441764×
        let mut view = op.view.clone();
1442764×
        view.bookmarks.insert(
1443764×
            self.config.bookmark.clone(),
1444764×
            self.store.put_state(&StateObject::new(new_heads))?,
1445
        );
1446764×
        view.workspaces.insert(self.workspace, wc_hash);
1447764×
        self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?;
1448759×
        drop(recording);
1449759×
        let rewriting = crate::timing::phase("commit: manifest");
1450759×
        if partial {
145110×
            // The rest is rebuilt from disk by the next snapshot.
145210×
            self.drop_manifest();
145310×
        } else {
1454749×
            let conflicted = snap.base_snapshot.is_conflicted() && !snap.resolves;
1455
            // The files on disk are now exactly the new base's renderings:
1456
            // record their graph keys, so the next snapshot neither reads nor
1457
            // renders them (oxygen#35).
1458749×
            let keys = if conflicted {
14590×
                HashMap::new()
1460
            } else {
1461749×
                committed_keys.unwrap_or_default()
1462
            };
1463749×
            match pending {
1464536×
                Some(m) => self.write_pending_manifest(m, conflicted, keys)?,
1465213×
                None => self.rewrite_manifest(conflicted, false, &keys)?,
1466
            }
1467
        }
1468759×
        drop(rewriting);
1469759×
        let packing = crate::timing::phase("commit: autopack");
1470759×
        self.maybe_autopack()?;
1471759×
        drop(packing);
1472759×
        let _hook = crate::timing::phase("commit: hook");
1473759×
        let _ = self.run_hook(
1474759×
            "post-commit",
1475759×
            &format!(
1476759×
                "{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}",
1477759×
                json_str(&snap.wc.change_id.to_hex()),
1478759×
                json_str(&hash.to_hex()),
1479759×
                json_str(message)
1480759×
            ),
1481759×
        );
1482759×
        Ok((hash, partial))
1483772×
    }
1484
}
1485
1486
/// What part of the working copy a commit takes (spec §12.3).
1487
#[derive(Clone, Debug)]
1488
pub enum Selection {
1489
    All,
1490
    /// Every operation and edit on nodes under these paths.
1491
    Paths(Vec<String>),
1492
    /// Hunk indices as `Repo::hunks` numbers them; dependent hunks are added.
1493
    Hunks(Vec<usize>),
1494
}
1495
1496
/// One selectable piece of the working copy: a tree operation or an edit.
1497
#[derive(Clone, Debug)]
1498
pub struct Hunk {
1499
    pub index: usize,
1500
    pub path: String,
1501
    /// Rendered like `oxy diff`.
1502
    pub text: String,
1503
}
1504
1505
/// The tree operations and edits on nodes under `paths`, plus the
1506
/// additions of any new parent directories they need.
15078×
fn select_paths(
15088×
    wc: &Change,
15098×
    base_snapshot: &Snapshot,
15108×
    paths: &[String],
15118×
) -> (Vec<TreeOp>, Vec<Edit>) {
151227×
    let selected = |p: &str| {
151327×
        paths
151427×
            .iter()
151527×
            .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
151627×
    };
15178×
    let by_node = base_snapshot.tree.by_node();
15188×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
15198×
    for op in &wc.tree_ops {
1520
        if let TreeOp::AddNode {
15215×
            node,
15225×
            parent,
15235×
            name,
15245×
            kind,
1525
            ..
15266×
        } = op
15275×
        {
15285×
            new_names.insert(*node, (*parent, name.clone(), *kind));
15295×
        }
1530
    }
15318×
    let mut nodes: BTreeSet<NodeId> = BTreeSet::new();
153228×
    for n in wc
15338×
        .tree_ops
15348×
        .iter()
15358×
        .map(|o| o.node())
153622×
        .chain(wc.edits.iter().map(|e| e.node()))
1537
    {
153828×
        if path_of_node(&n, &by_node, &new_names).is_some_and(|p| selected(&p)) {
153914×
            nodes.insert(n);
154014×
        }
1541
    }
1542
    // The nodes shown as one with a selected node are retired with it.
15438×
    for (dup, shown) in &base_snapshot.tree.unified {
15441×
        if nodes.contains(shown) {
15451×
            nodes.insert(*dup);
15461×
        }
1547
    }
1548
    // New parents of selected new nodes come along.
15498×
    let mut changed = true;
155016×
    while changed {
15518×
        changed = false;
15528×
        for (n, (parent, _, _)) in &new_names {
15535×
            if nodes.contains(n) && new_names.contains_key(parent) && nodes.insert(*parent) {
15540×
                changed = true;
15555×
            }
1556
        }
1557
    }
15588×
    let ops = wc
15598×
        .tree_ops
15608×
        .iter()
15618×
        .filter(|o| nodes.contains(&o.node()))
15628×
        .cloned()
15638×
        .collect();
15648×
    let edits = wc
15658×
        .edits
15668×
        .iter()
156722×
        .filter(|e| nodes.contains(&e.node()))
15688×
        .cloned()
15698×
        .collect();
15708×
    (ops, edits)
15718×
}
1572
1573
/// The hunks `indices` name plus whatever they refer to within the working
1574
/// copy (the parent directory of an added node, the insert that introduced
1575
/// an anchor line), renumbered so the result is a well-formed change.
1576
/// The nodes `unified` shows as one with another (a base's
1577
/// [`TreeView::unified`](crate::tree::TreeView::unified)) are retired with
1578
/// any hunk of the node shown.
15796×
fn select_hunks(
15806×
    wc: &Change,
15816×
    indices: &[usize],
15826×
    unified: &BTreeMap<NodeId, NodeId>,
15836×
) -> (Vec<TreeOp>, Vec<Edit>) {
15846×
    let n_ops = wc.tree_ops.len();
15856×
    let mut ops_sel = vec![false; n_ops];
15866×
    let mut edits_sel = vec![false; wc.edits.len()];
15876×
    for &i in indices {
15885×
        if i < n_ops {
15891×
            ops_sel[i] = true;
15904×
        } else if i - n_ops < wc.edits.len() {
15914×
            edits_sel[i - n_ops] = true;
15924×
        }
1593
    }
1594
    // Atom ranges each insert introduces, per node, in the working copy's numbering.
15956×
    let mut per_node: HashMap<NodeId, u32> = HashMap::new();
15966×
    let mut insert_ranges: Vec<Option<(NodeId, u32, u32)>> = Vec::new();
159712×
    for e in &wc.edits {
159812×
        insert_ranges.push(match e {
159912×
            Edit::Insert { node, lines, .. } => {
160012×
                let start = per_node.entry(*node).or_default();
160112×
                let r = (*node, *start, *start + lines.len() as u32);
160212×
                *start += lines.len() as u32;
160312×
                Some(r)
1604
            }
16050×
            _ => None,
1606
        });
1607
    }
16086×
    let add_index: HashMap<NodeId, usize> = wc
16096×
        .tree_ops
16106×
        .iter()
16116×
        .enumerate()
161210×
        .filter_map(|(i, o)| {
161310×
            if let TreeOp::AddNode { node, .. } = o {
16149×
                Some((*node, i))
1615
            } else {
16161×
                None
1617
            }
161810×
        })
16196×
        .collect();
16206×
    let insert_for = |node: &NodeId, index: u32| -> Option<usize> {
16210×
        insert_ranges
16220×
            .iter()
16230×
            .position(|r| matches!(r, Some((n, a, b)) if n == node && *a <= index && index < *b))
16240×
    };
1625
    // Closure.
16266×
    let mut changed = true;
162715×
    while changed {
16289×
        changed = false;
162919×
        for i in 0..n_ops {
163019×
            if !ops_sel[i] {
163113×
                continue;
16326×
            }
16336×
            if let TreeOp::AddNode { node, parent, .. } = &wc.tree_ops[i] {
16346×
                if let Some(&p) = add_index.get(parent) {
16354×
                    if !ops_sel[p] {
16362×
                        ops_sel[p] = true;
16372×
                        changed = true;
16382×
                    }
16392×
                }
1640
                // A new node's content belongs with its addition: nobody
1641
                // wants an empty file committed apart from its lines.
164212×
                for (j, e) in wc.edits.iter().enumerate() {
164312×
                    if e.node() == *node && !edits_sel[j] {
16441×
                        edits_sel[j] = true;
16451×
                        changed = true;
164611×
                    }
1647
                }
16480×
            }
1649
        }
165018×
        for j in 0..wc.edits.len() {
165118×
            if !edits_sel[j] {
165210×
                continue;
16538×
            }
16548×
            let e = &wc.edits[j];
16558×
            if let Some(&a) = add_index.get(&e.node()) {
16565×
                if !ops_sel[a] {
16571×
                    ops_sel[a] = true;
16581×
                    changed = true;
16594×
                }
16603×
            }
1661
            if let Edit::Insert {
16628×
                node,
16638×
                after,
16648×
                before,
1665
                ..
16668×
            } = e
1667
            {
166816×
                for r in [after, before] {
166916×
                    if let AtomRef::Atom { change, index } = r {
16703×
                        if change.is_zero() {
16710×
                            if let Some(k) = insert_for(node, *index) {
16720×
                                if !edits_sel[k] {
16730×
                                    edits_sel[k] = true;
16740×
                                    changed = true;
16750×
                                }
16760×
                            }
16773×
                        }
167813×
                    }
1679
                }
16800×
            }
1681
        }
1682
    }
1683
    // The retirement of a node shown as one with a selected node.
16846×
    if !unified.is_empty() {
16851×
        let chosen: HashSet<NodeId> = (0..n_ops)
16862×
            .filter(|i| ops_sel[*i])
16871×
            .map(|i| wc.tree_ops[i].node())
16881×
            .chain(
16891×
                (0..wc.edits.len())
16902×
                    .filter(|j| edits_sel[*j])
16911×
                    .map(|j| wc.edits[j].node()),
1692
            )
16931×
            .collect();
16942×
        for (i, o) in wc.tree_ops.iter().enumerate() {
16952×
            if let TreeOp::RemoveNode { node, .. } = o {
16961×
                if unified.get(node).is_some_and(|s| chosen.contains(s)) {
16971×
                    ops_sel[i] = true;
16981×
                }
16991×
            }
1700
        }
17015×
    }
1702
    // Renumber self-references: old atom index → new index among selected inserts.
17036×
    let mut remap: HashMap<(NodeId, u32), u32> = HashMap::new();
17046×
    let mut new_counts: HashMap<NodeId, u32> = HashMap::new();
170512×
    for (j, r) in insert_ranges.iter().enumerate() {
170612×
        if let (true, Some((node, a, b))) = (edits_sel[j], r) {
17075×
            let next = new_counts.entry(*node).or_default();
17085×
            for old in *a..*b {
17095×
                remap.insert((*node, old), *next);
17105×
                *next += 1;
17115×
            }
17127×
        }
1713
    }
171410×
    let map_ref = |r: AtomRef, node: &NodeId| -> AtomRef {
17153×
        match r {
17163×
            AtomRef::Atom { change, index } if change.is_zero() => AtomRef::Atom {
17170×
                change,
17180×
                index: remap.get(&(*node, index)).copied().unwrap_or(index),
17190×
            },
172010×
            other => other,
1721
        }
172210×
    };
17236×
    let ops = wc
17246×
        .tree_ops
17256×
        .iter()
17266×
        .enumerate()
172710×
        .filter(|(i, _)| ops_sel[*i])
17286×
        .map(|(_, o)| o.clone())
17296×
        .collect();
17306×
    let edits = wc
17316×
        .edits
17326×
        .iter()
17336×
        .enumerate()
173412×
        .filter(|(j, _)| edits_sel[*j])
17356×
        .map(|(_, e)| match e {
1736
            Edit::Insert {
17375×
                node,
17385×
                after,
17395×
                before,
17405×
                lines,
17415×
            } => Edit::Insert {
17425×
                node: *node,
17435×
                after: map_ref(*after, node),
17445×
                before: map_ref(*before, node),
17455×
                lines: lines.clone(),
17465×
            },
17470×
            other => other.clone(),
17485×
        })
17496×
        .collect();
17506×
    (ops, edits)
17516×
}
1752
1753
/// Render each tree operation and edit of `change` on its own, numbered.
17548×
pub fn render_hunks(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<Hunk> {
17558×
    let by_node = base_snapshot.tree.by_node();
17568×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
175710×
    for op in &change.tree_ops {
1758
        if let TreeOp::AddNode {
17599×
            node,
17609×
            parent,
17619×
            name,
17629×
            kind,
1763
            ..
176410×
        } = op
17659×
        {
17669×
            new_names.insert(*node, (*parent, name.clone(), *kind));
17679×
        }
1768
    }
17698×
    let mut out = Vec::new();
177010×
    for (i, op) in change.tree_ops.iter().enumerate() {
1771
        // A retired duplicate goes with the node shown: no hunk of its own.
177210×
        if is_unified_retirement(op, base_snapshot) {
17731×
            continue;
17749×
        }
17759×
        let one = Change {
17769×
            tree_ops: vec![op.clone()],
17779×
            edits: vec![],
17789×
            ..change.clone()
17799×
        };
17809×
        let path = path_of_node(&op.node(), &by_node, &new_names).unwrap_or_default();
17819×
        out.push(Hunk {
17829×
            index: i,
17839×
            path,
17849×
            text: render_change(&one, base, base_snapshot),
17859×
        });
1786
    }
178715×
    for (j, e) in change.edits.iter().enumerate() {
178815×
        let one = Change {
178915×
            tree_ops: vec![],
179015×
            edits: vec![e.clone()],
179115×
            ..change.clone()
179215×
        };
179315×
        let path = path_of_node(&e.node(), &by_node, &new_names).unwrap_or_default();
179415×
        out.push(Hunk {
179515×
            index: change.tree_ops.len() + j,
179615×
            path,
179715×
            text: render_change(&one, base, base_snapshot),
179815×
        });
179915×
    }
18008×
    out
18018×
}
1802
1803
/// One file's part of a diff, for `oxy diff --json`.
1804
#[derive(Clone, Debug)]
1805
pub struct FileDiff {
1806
    pub path: String,
1807
    /// `added`, `removed`, `modified`, `renamed`, `mode`, `replaced`.
1808
    pub kind: String,
1809
    pub hunks: Vec<DiffHunk>,
1810
    /// For renames, the previous path.
1811
    pub from: Option<String>,
1812
}
1813
1814
/// Lines inserted after `base_line` (0 = before the first line) and lines
1815
/// removed, with their base line numbers.
1816
#[derive(Clone, Debug, Default)]
1817
pub struct DiffHunk {
1818
    pub base_line: u32,
1819
    pub added: Vec<Vec<u8>>,
1820
    pub removed: Vec<(u32, Vec<u8>)>,
1821
}
1822
1823
impl Repo {
1824
    /// The content of `path` in a state: `heads`, or the working copy's base.
182527×
    pub fn file_at(&self, path: &str, heads: Option<&[ChangeHash]>) -> Result<Vec<u8>> {
1826
        // The state's history and tree, then the one file's graph: nothing
1827
        // else is built or rendered (oxygen#92).
1828
        // The working copy's base, and a state named by its heads (`-r
1829
        // main` on main), come from the workspace's state index.
183027×
        let base = if self.is_bare() {
18315×
            None
1832
        } else {
183322×
            Some(self.working_copy()?.1.deps)
1834
        };
183527×
        let same = |a: &[ChangeHash], b: &[ChangeHash]| {
18361×
            let (mut a, mut b) = (a.to_vec(), b.to_vec());
18371×
            a.sort();
18381×
            b.sort();
18391×
            a == b
18401×
        };
184127×
        let mut state = match (heads, base) {
18421×
            (Some(h), Some(b)) if same(h, &b) => self.load_state_indexed(&b)?,
18435×
            (Some(h), _) => self.load_state_history(h)?,
184421×
            (None, Some(b)) => self.load_state_indexed(&b)?,
18450×
            (None, None) => return fail("this is a bare repository: it has no working copy"),
1846
        };
184727×
        let view = self.tree_view(&mut state)?;
184827×
        let Some(node) = view.entries.get(path).map(|e| e.node) else {
18490×
            return fail(format!("{path} is not in that state"));
1850
        };
185127×
        self.load_graphs_of(&mut state, &[node])?;
185226×
        let snapshot = state.materialize_paths(view, [path]);
185326×
        let Some(f) = snapshot.files.get(path) else {
18540×
            return fail(format!("{path} is not in that state"));
1855
        };
185626×
        match &f.content {
18570×
            FileContent::Dir => fail(format!("{path} is a directory")),
185826×
            FileContent::Text { bytes, .. } => Ok(bytes.clone()),
18590×
            FileContent::Opaque { blobs } => match blobs.first() {
18600×
                Some(b) => self.read_blob(b),
18610×
                None => Ok(vec![]),
1862
            },
1863
        }
186427×
    }
1865
1866
    /// Restore `paths` (files or directories) to the working copy's base:
1867
    /// edits are undone, additions removed, removals put back. Returns the
1868
    /// number of paths touched.
18692×
    pub fn discard(&self, paths: &[String]) -> Result<usize> {
18702×
        let _wc = self.lock_working_copy()?;
18712×
        let snap = self.snapshot_status("oxy discard")?;
18727×
        let under = |p: &str| {
18737×
            paths
18747×
                .iter()
18757×
                .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))))
18767×
        };
18772×
        let attrs = Repo::state_attributes(&snap.base_snapshot);
18782×
        let mut n = 0;
18793×
        for s in &snap.statuses {
18803×
            if !under(&s.path) {
18811×
                continue;
18822×
            }
18832×
            match snap.base_snapshot.files.get(&s.path) {
1884
                None => {
1885
                    // Added (or the new name of a rename): remove it.
18861×
                    let full = self.root.join(&s.path);
18871×
                    if full.is_dir() {
18880×
                        let _ = fs::remove_dir_all(&full);
18891×
                    } else {
18901×
                        let _ = fs::remove_file(&full);
18911×
                    }
18921×
                    n += 1;
1893
                }
18941×
                Some(f) => {
18951×
                    match &f.content {
18960×
                        FileContent::Dir => fs::create_dir_all(self.root.join(&s.path))?,
18971×
                        FileContent::Text { bytes, .. } => {
18981×
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&s.path).eol
18991×
                                == Some(crate::attributes::Eol::Crlf)
1900
                            {
19010×
                                crate::attributes::to_crlf(bytes).into()
1902
                            } else {
19031×
                                bytes.as_slice().into()
1904
                            };
19051×
                            self.write_file(&s.path, &on_disk, f.mode.exec)?;
1906
                        }
19070×
                        FileContent::Opaque { blobs } => {
19080×
                            let bytes = match blobs.first() {
19090×
                                Some(b) => self.read_blob(b)?,
19100×
                                None => vec![],
1911
                            };
19120×
                            if f.kind == NodeKind::Symlink {
19130×
                                self.write_symlink(&s.path, &bytes)?;
19140×
                            } else if f.kind == NodeKind::File {
19150×
                                self.write_file(&s.path, &bytes, f.mode.exec)?;
19160×
                            }
1917
                        }
1918
                    }
19191×
                    n += 1;
1920
                }
1921
            }
1922
        }
1923
        // A rename's old name is not in the statuses; put it back too.
19244×
        for (p, f) in &snap.base_snapshot.files {
19254×
            if under(p)
19261×
                && !self.root.join(p).exists()
19270×
                && f.kind != NodeKind::Dir
19280×
                && f.kind != NodeKind::Subrepo
19290×
                && !snap.statuses.iter().any(|s| s.path == *p)
1930
            {
19310×
                let bytes = match &f.content {
19320×
                    FileContent::Text { bytes, .. } => bytes.clone(),
19330×
                    FileContent::Opaque { blobs } => blobs
19340×
                        .first()
19350×
                        .map(|b| self.read_blob(b))
19360×
                        .transpose()?
19370×
                        .unwrap_or_default(),
19380×
                    FileContent::Dir => continue,
1939
                };
19400×
                if f.kind == NodeKind::Symlink {
19410×
                    self.write_symlink(p, &bytes)?
1942
                } else {
19430×
                    self.write_file(p, &bytes, f.mode.exec)?
1944
                }
19450×
                n += 1;
19464×
            }
1947
        }
19482×
        self.drop_manifest();
19492×
        Ok(n)
19502×
    }
1951
1952
    /// Undo the working-copy hunks `indices` (as `hunks` numbers them) on
1953
    /// disk, keeping the others: the files are rewritten from the base plus
1954
    /// the remaining hunks.
19551×
    pub fn discard_hunks(&self, indices: &[usize]) -> Result<usize> {
19561×
        let _wc = self.lock_working_copy()?;
19571×
        let (snap, hunks) = self.hunks()?;
19581×
        let keep: Vec<usize> = hunks
19591×
            .iter()
19601×
            .map(|h| h.index)
19611×
            .filter(|i| !indices.contains(i))
19621×
            .collect();
19631×
        let (ops, edits) = select_hunks(&snap.wc, &keep, &snap.base_snapshot.tree.unified);
19641×
        let dropped_nodes: BTreeSet<NodeId> = hunks
19651×
            .iter()
19661×
            .filter(|h| indices.contains(&h.index))
19671×
            .map(|h| {
19681×
                if h.index < snap.wc.tree_ops.len() {
19690×
                    snap.wc.tree_ops[h.index].node()
1970
                } else {
19711×
                    snap.wc.edits[h.index - snap.wc.tree_ops.len()].node()
1972
                }
19731×
            })
19741×
            .collect();
19751×
        let mut partial = snap.wc.clone();
19761×
        partial.tree_ops = ops;
19771×
        partial.edits = edits;
19781×
        partial.normalize();
19791×
        let mut st = snap.base.clone();
19801×
        st.add_unchecked(partial);
1981
        // Every file of the target is rendered: build the graphs the
1982
        // partial snapshot left out.
19831×
        self.load_all_graphs(&mut st)?;
19841×
        let target = st.materialize();
19851×
        let by_node_target = target.tree.by_node();
19861×
        let by_node_base = snap.base_snapshot.tree.by_node();
19871×
        let attrs = Repo::state_attributes(&target);
19881×
        let mut n = 0;
19891×
        for node in dropped_nodes {
19901×
            match by_node_target.get(&node) {
19911×
                Some(e) => {
19921×
                    let f = &target.files[&e.path];
19931×
                    match &f.content {
19940×
                        FileContent::Dir => {}
19951×
                        FileContent::Text { bytes, .. } => {
19961×
                            let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&e.path).eol
19971×
                                == Some(crate::attributes::Eol::Crlf)
1998
                            {
19990×
                                crate::attributes::to_crlf(bytes).into()
2000
                            } else {
20011×
                                bytes.as_slice().into()
2002
                            };
20031×
                            self.write_file(&e.path, &on_disk, f.mode.exec)?;
2004
                        }
20050×
                        FileContent::Opaque { blobs } => {
20060×
                            let bytes = blobs
20070×
                                .first()
20080×
                                .map(|b| self.read_blob(b))
20090×
                                .transpose()?
20100×
                                .unwrap_or_default();
20110×
                            if f.kind == NodeKind::Symlink {
20120×
                                self.write_symlink(&e.path, &bytes)?
2013
                            } else {
20140×
                                self.write_file(&e.path, &bytes, f.mode.exec)?
2015
                            }
2016
                        }
2017
                    }
2018
                    // A discarded rename: the new name goes.
20191×
                    if let Some(old_path) = snap
20201×
                        .statuses
20211×
                        .iter()
20221×
                        .find(|s| {
20231×
                            s.status == ChangeStatus::Renamed && !target.files.contains_key(&s.path)
20241×
                        })
20251×
                        .map(|s| s.path.clone())
20260×
                    {
20270×
                        let _ = fs::remove_file(self.root.join(old_path));
20281×
                    }
20291×
                    n += 1;
2030
                }
2031
                None => {
2032
                    // The node exists in neither target nor base with a live path: an addition to remove.
20330×
                    if let Some(added) = snap.statuses.iter().find(|s| {
20340×
                        s.status == ChangeStatus::Added
20350×
                            && !target.files.contains_key(&s.path)
20360×
                            && !by_node_base.contains_key(&node)
20370×
                    }) {
20380×
                        let full = self.root.join(&added.path);
20390×
                        if full.is_dir() {
20400×
                            let _ = fs::remove_dir_all(&full);
20410×
                        } else {
20420×
                            let _ = fs::remove_file(&full);
20430×
                        }
20440×
                        n += 1;
20450×
                    }
2046
                }
2047
            }
2048
        }
20491×
        self.drop_manifest();
20501×
        Ok(n)
20511×
    }
2052
2053
    /// The diff of the working copy (`None`) or of a change, per file with
2054
    /// line-numbered hunks (spec: `oxy diff --json`).
20557×
    pub fn diff_files(&self, change: Option<&ChangeHash>) -> Result<Vec<FileDiff>> {
20567×
        let (c, base, base_snapshot) = match change {
20573×
            Some(h) => {
20583×
                let c = self.store.get_change(h)?;
20593×
                let (base, snapshot) = self.change_base(&c)?;
20603×
                (c, base, snapshot)
2061
            }
20624×
            None => match self.working_copy_with_base_unless_unchanged()? {
20633×
                Some(all) => all,
20641×
                None => return Ok(Vec::new()),
2065
            },
2066
        };
20676×
        Ok(diff_files_of(&c, &base, &base_snapshot))
20687×
    }
2069
2070
    /// The snapshot a command would take, without taking it, when the
2071
    /// status cache holds: every file is as the last snapshot left it, so
2072
    /// that snapshot's working-copy change is the answer. Its base comes
2073
    /// from the state index, the `.oxyattributes` files from disk. `None`
2074
    /// when a snapshot must be taken, and when there is nothing to commit
2075
    /// (the snapshot then says so). A conflicted base never gets here: the
2076
    /// cache does not hold for one.
2077773×
    pub(crate) fn snapshot_from_cache(&self) -> Result<Option<SnapshotResult>> {
2078773×
        let Some(statuses) = self.status_by_manifest()? else {
2079549×
            return Ok(None);
2080
        };
2081224×
        if statuses.is_empty() {
20823×
            return Ok(None);
2083221×
        }
2084221×
        let _t = crate::timing::phase("snapshot: from the status cache");
2085221×
        let (wc_hash, wc) = self.working_copy()?;
2086221×
        let base = self.load_state_indexed(&wc.deps)?;
2087221×
        let (base, base_snapshot) = self.render_base_for(&wc, base)?;
2088221×
        let attribute_files: Vec<(String, Vec<u8>)> = match self.read_manifest()? {
2089221×
            Some((_, _, m)) => m
2090221×
                .entries
2091221×
                .keys()
2092679×
                .filter(|p| is_attributes(p))
2093221×
                .filter_map(|p| fs::read(self.root.join(p)).ok().map(|b| (p.clone(), b)))
2094221×
                .collect(),
20950×
            None => Vec::new(),
2096
        };
2097221×
        let attributes = crate::attributes::Attributes::from_files(
2098221×
            attribute_files
2099221×
                .iter()
2100221×
                .map(|(p, b)| (p.as_str(), b.as_slice())),
2101
        );
2102221×
        Ok(Some(SnapshotResult {
2103221×
            wc_hash,
2104221×
            wc,
2105221×
            base,
2106221×
            base_snapshot,
2107221×
            statuses,
2108221×
            changed: false,
2109221×
            resolves: false,
2110221×
            contributors: Vec::new(),
2111221×
            attributes,
2112221×
            untracked_nested: self.untracked_nested.borrow().clone(),
2113221×
            manifest: None,
2114221×
        }))
2115773×
    }
2116
2117
    /// The working-copy change with its base state and a snapshot of the
2118
    /// base, for showing what the working copy changes ([`render_change`],
2119
    /// [`diff_files_of`]). While the status cache holds (every file as the
2120
    /// last snapshot left it), the stored working-copy change is the
2121
    /// answer, no snapshot is taken, and the base's snapshot holds its tree
2122
    /// but no file; otherwise a snapshot makes all three.
21232×
    pub fn working_copy_with_base(&self) -> Result<(Change, State, Snapshot)> {
21242×
        match self.working_copy_with_base_unless_unchanged()? {
21251×
            Some(all) => Ok(all),
2126
            None => {
21271×
                let (_, wc) = self.working_copy()?;
21281×
                let base = self.load_state_indexed(&wc.deps)?;
21291×
                let (base, snapshot) = self.tree_base_for(&wc, base)?;
21301×
                Ok((wc, base, snapshot))
2131
            }
2132
        }
21332×
    }
2134
2135
    /// [`Repo::working_copy_with_base`], or `None` when the status cache
2136
    /// holds and the working-copy change changes nothing: its diff is
2137
    /// empty, and the base is not loaded for it (oxygen#94).
21387×
    pub fn working_copy_with_base_unless_unchanged(
21397×
        &self,
21407×
    ) -> Result<Option<(Change, State, Snapshot)>> {
21417×
        if self.status_by_manifest()?.is_some() {
21425×
            let (_, wc) = self.working_copy()?;
21435×
            if wc.tree_ops.is_empty() && wc.edits.is_empty() {
21443×
                return Ok(None);
21452×
            }
21462×
            let base = self.load_state_indexed(&wc.deps)?;
21472×
            let (base, snapshot) = self.tree_base_for(&wc, base)?;
21482×
            return Ok(Some((wc, base, snapshot)));
21492×
        }
21502×
        let snap = self.snapshot_status("oxy diff")?;
21512×
        Ok(Some((snap.wc, snap.base, snap.base_snapshot)))
21527×
    }
2153
2154
    /// The log restricted to changes touching `path` (its node, so renames
2155
    /// are followed), on the current bookmark.
21566×
    pub fn log_for_path(&self, path: &str) -> Result<Vec<LogEntry>> {
21576×
        let heads = self.bookmark_heads()?;
21586×
        if self.is_bare() {
21591×
            return self.log_for_path_in(path, &heads);
21605×
        }
21615×
        let entries = self.log()?;
21625×
        let (_, wc) = self.working_copy()?;
2163
        // The path's node from the trees alone: the base's (from the
2164
        // workspace's state index), else the bookmark's. Nothing is
2165
        // rendered.
21665×
        let node_in = |mut state: State| -> Result<Option<HashSet<NodeId>>> {
21675×
            Ok(nodes_at(&self.tree_view(&mut state)?, path))
21685×
        };
21695×
        let nodes = match node_in(self.load_state_indexed(&wc.deps)?)? {
21705×
            Some(n) => Some(n),
21710×
            None => node_in(self.load_state_history(&heads)?)?,
2172
        };
21735×
        let Some(nodes) = nodes else {
21740×
            return fail(format!("{path} is not tracked"));
2175
        };
21765×
        Ok(self.entries_touching_up_to(entries, nodes, None))
21776×
    }
2178
2179
    /// The log of the state with these heads restricted to changes touching
2180
    /// `path` (its node, so renames are followed).
21812×
    pub fn log_for_path_in(&self, path: &str, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> {
21822×
        self.log_for_path_of(&self.load_state_history(heads)?, path, None)
21832×
    }
2184
2185
    /// The changes of a loaded state touching `path`, newest first, at most
2186
    /// `limit` of them. The path is found in the state's tree alone, so no
2187
    /// file is rendered, and the walk stops once `limit` are found.
21885×
    pub fn log_for_path_of(
21895×
        &self,
21905×
        state: &State,
21915×
        path: &str,
21925×
        limit: Option<usize>,
21935×
    ) -> Result<Vec<LogEntry>> {
21945×
        let Some(nodes) = nodes_at(&state.view(), path) else {
21951×
            return fail(format!("{path} is not in that state"));
2196
        };
21974×
        Ok(self.entries_touching_up_to(self.log_of(state), nodes, limit))
21985×
    }
2199
22009×
    fn entries_touching_up_to(
22019×
        &self,
22029×
        entries: Vec<LogEntry>,
22039×
        wanted: HashSet<NodeId>,
22049×
        limit: Option<usize>,
22059×
    ) -> Vec<LogEntry> {
22069×
        let mut touched = self.touched();
22079×
        if limit.is_none() {
22088×
            let hashes: Vec<ChangeHash> = entries.iter().map(|e| e.hash).collect();
22098×
            touched.fill(self, &hashes);
22101×
        }
22119×
        let mut out = Vec::new();
221232×
        for e in entries {
221332×
            if limit.is_some_and(|n| out.len() >= n) {
22141×
                break;
221531×
            }
221631×
            if touched.touches(self, &e.hash, &wanted) {
221720×
                out.push(e);
221820×
            }
2219
        }
22209×
        touched.save();
22219×
        out
22229×
    }
2223
}
2224
2225
/// The node at `path` in `view`, with the nodes it shows as one with it
2226
/// (identical additions, spec §9.2): the file's history is all of theirs.
222710×
fn nodes_at(view: &crate::tree::TreeView, path: &str) -> Option<HashSet<NodeId>> {
222810×
    let node = view.entries.get(path)?.node;
22299×
    let mut out = HashSet::from([node]);
22309×
    out.extend(
22319×
        view.unified
22329×
            .iter()
22339×
            .filter(|(_, shown)| **shown == node)
22349×
            .map(|(dup, _)| *dup),
2235
    );
22369×
    Some(out)
223710×
}
2238
2239
/// Per-file hunks of `change` against `base`.
22406×
pub fn diff_files_of(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<FileDiff> {
22416×
    let by_node = base_snapshot.tree.by_node();
22426×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
22436×
    for op in &change.tree_ops {
2244
        if let TreeOp::AddNode {
22452×
            node,
22462×
            parent,
22472×
            name,
22482×
            kind,
2249
            ..
22503×
        } = op
22512×
        {
22522×
            new_names.insert(*node, (*parent, name.clone(), *kind));
22532×
        }
2254
    }
225510×
    let path_of = |n: &NodeId| -> String {
225610×
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
225710×
    };
22586×
    let mut files: BTreeMap<NodeId, FileDiff> = BTreeMap::new();
22596×
    let mut order: Vec<NodeId> = Vec::new();
226015×
    let mut entry = |files: &mut BTreeMap<NodeId, FileDiff>, n: NodeId, kind: &str| {
226115×
        if let std::collections::btree_map::Entry::Vacant(v) = files.entry(n) {
226210×
            order.push(n);
226310×
            v.insert(FileDiff {
226410×
                path: path_of(&n),
226510×
                kind: kind.to_string(),
226610×
                hunks: vec![],
226710×
                from: None,
226810×
            });
226910×
        }
227015×
    };
22716×
    for op in change
22726×
        .tree_ops
22736×
        .iter()
22746×
        .filter(|o| !is_unified_retirement(o, base_snapshot))
2275
    {
22763×
        match op {
22772×
            TreeOp::AddNode { node, kind, .. } => {
22782×
                if *kind != NodeKind::Dir {
22792×
                    entry(&mut files, *node, "added");
22802×
                }
2281
            }
22821×
            TreeOp::RemoveNode { node, .. } => {
22831×
                entry(&mut files, *node, "removed");
22841×
                if let Some(e) = by_node.get(node) {
22851×
                    files.get_mut(node).unwrap().path = e.path.clone();
22861×
                }
2287
            }
2288
            TreeOp::Move {
22890×
                node, parent, name, ..
2290
            } => {
22910×
                entry(&mut files, *node, "renamed");
22920×
                let f = files.get_mut(node).unwrap();
22930×
                f.from = by_node.get(node).map(|e| e.path.clone());
22940×
                let dir = if parent.is_root() {
22950×
                    String::new()
2296
                } else {
22970×
                    path_of_node(parent, &by_node, &new_names).unwrap_or_default()
2298
                };
22990×
                f.path = if dir.is_empty() {
23000×
                    name.clone()
2301
                } else {
23020×
                    format!("{dir}/{name}")
2303
                };
2304
            }
23050×
            TreeOp::SetMode { node, .. } => entry(&mut files, *node, "mode"),
2306
        }
2307
    }
2308
    // Base line numbers per node, 1-based.
23096×
    let mut line_of: HashMap<NodeId, HashMap<(ChangeHash, u32), u32>> = HashMap::new();
231012×
    let mut lines_for = |n: &NodeId| -> HashMap<(ChangeHash, u32), u32> {
231112×
        line_of
231212×
            .entry(*n)
231312×
            .or_insert_with(|| {
23149×
                base.live_lines(n)
23159×
                    .into_iter()
23169×
                    .enumerate()
231712×
                    .map(|(i, (k, _))| (k, i as u32 + 1))
23189×
                    .collect()
23199×
            })
232012×
            .clone()
232112×
    };
232212×
    for e in &change.edits {
232312×
        let n = e.node();
232412×
        let is_new = new_names.contains_key(&n);
232512×
        entry(&mut files, n, if is_new { "added" } else { "modified" });
232612×
        let f = files.get_mut(&n).unwrap();
232712×
        match e {
232810×
            Edit::Insert { after, lines, .. } => {
232910×
                let map = lines_for(&n);
233010×
                let base_line = match after {
23313×
                    AtomRef::Start => 0,
23320×
                    AtomRef::End => map.len() as u32,
23337×
                    AtomRef::Atom { change, index } if change.is_zero() => {
23340×
                        f.hunks.last().map(|h| h.base_line).unwrap_or(0)
2335
                    }
23367×
                    AtomRef::Atom { change, index } => {
23377×
                        map.get(&(*change, *index)).copied().unwrap_or(0)
2338
                    }
2339
                };
234010×
                f.hunks.push(DiffHunk {
234110×
                    base_line,
234210×
                    added: lines.to_vec(),
234310×
                    removed: vec![],
234410×
                });
2345
            }
23462×
            Edit::Delete { atoms, .. } => {
23472×
                let map = lines_for(&n);
23482×
                let mut removed = Vec::new();
23492×
                for r in atoms {
23502×
                    for i in r.from..r.to {
23512×
                        let line = map.get(&(r.change, i)).copied().unwrap_or(0);
23522×
                        let text = base
23532×
                            .file(&n)
23542×
                            .and_then(|g| g.content(&(r.change, i)))
23552×
                            .unwrap_or(b"")
23562×
                            .to_vec();
23572×
                        removed.push((line, text));
2358
                    }
2359
                }
23602×
                let base_line = removed
23612×
                    .first()
23622×
                    .map(|(l, _)| l.saturating_sub(1))
23632×
                    .unwrap_or(0);
23642×
                f.hunks.push(DiffHunk {
23652×
                    base_line,
23662×
                    added: vec![],
23672×
                    removed,
23682×
                });
2369
            }
2370
            Edit::Replace { .. } => {
23710×
                if !is_new {
23720×
                    f.kind = "replaced".into();
23730×
                }
2374
            }
2375
        }
2376
    }
237710×
    order.into_iter().filter_map(|n| files.remove(&n)).collect()
23786×
}
2379
2380
/// Builds the tree ops and edits of a working-copy change against a base.
2381
/// `BLAKE3("oxy node 1" 0 seed kind path)`, truncated: a node identifier
2382
/// that the same seed and path always give (never the root's).
238359×
fn seeded_node(seed: &[u8; 32], path: &str, kind: NodeKind) -> NodeId {
238459×
    let mut h = blake3::Hasher::new();
238559×
    h.update(b"oxy node 1\0");
238659×
    h.update(seed);
238759×
    h.update(&[match kind {
238845×
        NodeKind::File => 1,
238913×
        NodeKind::Dir => 2,
23900×
        NodeKind::Symlink => 3,
23911×
        NodeKind::Subrepo => 4,
2392
    }]);
239359×
    h.update(path.as_bytes());
239459×
    let mut id = [0u8; 16];
239559×
    id.copy_from_slice(&h.finalize().as_bytes()[..16]);
239659×
    if id == [0; 16] {
23970×
        id[15] = 1;
239859×
    }
239959×
    NodeId(id)
240059×
}
2401
2402
struct ChangeBuilder<'a> {
2403
    base: &'a State,
2404
    base_snapshot: &'a Snapshot,
2405
    tree_ops: Vec<TreeOp>,
2406
    edits: Vec<Edit>,
2407
    statuses: Vec<PathStatus>,
2408
    dir_nodes: HashMap<String, NodeId>,
2409
    reuse: HashMap<String, (NodeId, NodeKind)>,
2410
    /// Changes that contributed to conflicts this working copy resolves.
2411
    contributors: BTreeSet<ChangeHash>,
2412
    resolves: bool,
2413
    attrs: crate::attributes::Attributes,
2414
    blobs: HashMap<String, ObjectRef>,
2415
    /// Opaque files whose replace conflict the content settles.
2416
    resolve: HashSet<String>,
2417
    /// When set, new nodes' identifiers derive from it and their paths.
2418
    node_seed: Option<[u8; 32]>,
2419
    /// For a Git import, the base's dead directories by where they were
2420
    /// (see `ensure_dir`), worked out when first needed.
2421
    dead_dirs: Option<HashMap<(NodeId, String), (NodeId, crate::tree::OpKey)>>,
2422
    /// Text edits of edited files, worked out in parallel beforehand
2423
    /// (`diff_in_parallel`); a file not here is diffed when it is updated.
2424
    diffs: HashMap<String, Vec<Edit>>,
2425
}
2426
2427
impl<'a> ChangeBuilder<'a> {
24282406×
    fn new(
24292406×
        base: &'a State,
24302406×
        base_snapshot: &'a Snapshot,
24312406×
        reuse: HashMap<String, (NodeId, NodeKind)>,
24322406×
    ) -> Self {
24332406×
        let mut dir_nodes: HashMap<String, NodeId> = HashMap::new();
24342406×
        dir_nodes.insert(String::new(), NodeId::ROOT);
243518324×
        for (p, e) in base_snapshot.tree.entries.iter() {
243618324×
            if e.kind == NodeKind::Dir {
2437605×
                dir_nodes.insert(p.clone(), e.node);
243817719×
            }
2439
        }
24402406×
        ChangeBuilder {
24412406×
            base,
24422406×
            base_snapshot,
24432406×
            tree_ops: vec![],
24442406×
            edits: vec![],
24452406×
            statuses: vec![],
24462406×
            dir_nodes,
24472406×
            reuse,
24482406×
            contributors: BTreeSet::new(),
24492406×
            resolves: false,
24502406×
            attrs: crate::attributes::Attributes::default(),
24512406×
            blobs: HashMap::new(),
24522406×
            resolve: HashSet::new(),
24532406×
            node_seed: None,
24542406×
            dead_dirs: None,
24552406×
            diffs: HashMap::new(),
24562406×
        }
24572406×
    }
2458
2459
    /// Diff the edited text files among `entries` against their base
2460
    /// renderings at once, on every core, for `update_path` to take: the
2461
    /// same diff it would make, only not one file after another.
24622406×
    fn diff_in_parallel(&mut self, entries: &BTreeMap<String, FsEntry>) {
2463
        use rayon::prelude::*;
24642406×
        let files = &self.base_snapshot.files;
24652406×
        let edited: Vec<(&String, &FsEntry, &crate::state::FileState)> = entries
24662406×
            .iter()
246719501×
            .filter_map(|(p, e)| {
246819501×
                let x = files.get(p)?;
246913260×
                (x.kind == e.kind && !e.unchanged && !self.opaque_for(p, e)).then_some((p, e, x))
247019501×
            })
24712406×
            .collect();
2472
        // Below this, starting the threads costs more than it saves.
24732406×
        if edited.len() < 16 {
24742396×
            return;
247510×
        }
247610×
        let this = &*self;
247710×
        let attrs = &this.attrs;
247810×
        let diffs: Vec<(String, Vec<Edit>)> = edited
247910×
            .par_iter()
24805564×
            .filter_map(|(p, e, x)| {
2481
                let FileContent::Text {
24825564×
                    bytes,
24835564×
                    materialized,
24845564×
                } = &x.content
2485
                else {
24860×
                    return None;
2487
                };
24885564×
                if materialized.is_conflicted() {
24890×
                    return None;
24905564×
                }
24915564×
                let disk_text = if attrs.lookup(p).eol.is_some() {
24920×
                    std::sync::Arc::new(crate::attributes::normalize_eol(&e.content))
2493
                } else {
24945564×
                    e.content.clone()
2495
                };
24965564×
                if *bytes == *disk_text {
24975484×
                    return None;
249880×
                }
249980×
                let keys = materialized.live_atoms();
250080×
                let lines = crate::diff::split_lines(bytes);
250180×
                (keys.len() == lines.len()).then(|| {
250280×
                    let edits = crate::diff::text_edits_of(x.node, &keys, &lines, &disk_text);
250380×
                    ((*p).clone(), edits)
250480×
                })
25055564×
            })
250610×
            .collect();
250710×
        self.diffs = diffs.into_iter().collect();
25082406×
    }
2509
25102406×
    fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self {
25112406×
        self.attrs = attrs;
25122406×
        self
25132406×
    }
2514
2515
    /// Whether a file is opaque: forced by `binary`, forced text by `text`
2516
    /// only when it has no NUL bytes, else sniffed.
2517
    /// The blob for an opaque entry: the cached one, else stored now.
2518179×
    fn blob_of(
2519179×
        &mut self,
2520179×
        repo: &Repo,
2521179×
        path: &str,
2522179×
        e: &FsEntry,
2523179×
        nocompress: bool,
2524179×
    ) -> Result<ObjectRef> {
2525179×
        let b = match e.blob {
25260×
            Some(b) => b,
2527179×
            None => repo.store_blob_for(&e.content, nocompress)?,
2528
        };
2529179×
        self.blobs.insert(path.to_string(), b);
2530179×
        Ok(b)
2531179×
    }
2532
253324274×
    fn opaque_for(&self, path: &str, e: &FsEntry) -> bool {
253424274×
        if e.blob.is_some() {
253570×
            return true;
253624204×
        }
253724204×
        if e.kind == NodeKind::Symlink || e.kind == NodeKind::Subrepo {
253876×
            return true;
253924128×
        }
254024128×
        match self.attrs.lookup(path).binary {
254112×
            Some(true) => true,
25420×
            Some(false) => is_binary(&e.content),
254324116×
            None => is_binary(&e.content),
2544
        }
254524274×
    }
2546
2547
    /// Text content as stored: `\r\n` normalized for `eol=` files.
254815096×
    fn stored_text(
254915096×
        &self,
255015096×
        path: &str,
255115096×
        content: &std::sync::Arc<Vec<u8>>,
255215096×
    ) -> std::sync::Arc<Vec<u8>> {
255315096×
        if self.attrs.lookup(path).eol.is_some() {
25547×
            std::sync::Arc::new(crate::attributes::normalize_eol(content))
2555
        } else {
255615089×
            content.clone()
2557
        }
255815096×
    }
2559
2560
    /// The identifier of a node added at `path`: reused when asked, derived
2561
    /// from the seed when there is one, random otherwise.
25626365×
    fn new_node(&self, path: &str, kind: NodeKind) -> NodeId {
25636365×
        if let Some(n) = self.reused_node(path, kind) {
2564170×
            return n;
25656195×
        }
25666195×
        match &self.node_seed {
256759×
            Some(seed) => seeded_node(seed, path, kind),
25686136×
            None => NodeId::random(),
2569
        }
25706365×
    }
2571
25726365×
    fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> {
25736365×
        self.reuse
25746365×
            .get(path)
25756365×
            .filter(|(_, k)| *k == kind)
25766365×
            .map(|(n, _)| *n)
25776365×
    }
2578
2579
    /// A dead directory last placed at (`parent`, `name`) in the base.
258015×
    fn revivable_dir(
258115×
        &mut self,
258215×
        parent: NodeId,
258315×
        name: &str,
258415×
    ) -> Option<(NodeId, crate::tree::OpKey)> {
258515×
        let base = self.base;
258615×
        self.dead_dirs
258715×
            .get_or_insert_with(|| base.tree().dead_dirs())
258815×
            .get(&(parent, name.to_string()))
258915×
            .copied()
259015×
    }
2591
25926242×
    fn ensure_dir(&mut self, path: &str) -> NodeId {
25936242×
        if let Some(n) = self.dir_nodes.get(path) {
25946088×
            return *n;
2595154×
        }
2596154×
        let mut parent = NodeId::ROOT;
2597154×
        let mut prefix = String::new();
2598173×
        for comp in path.split('/') {
2599173×
            if !prefix.is_empty() {
260019×
                prefix.push('/');
2601154×
            }
2602173×
            prefix.push_str(comp);
2603173×
            let known = self.dir_nodes.get(&prefix).copied();
2604173×
            let revive = match known {
260513×
                Some(_) => None,
2606160×
                None if self.node_seed.is_some() => self.revivable_dir(parent, comp),
2607145×
                None => None,
2608
            };
2609173×
            parent = match (known, revive) {
261013×
                (Some(n), _) => n,
26112×
                (None, Some((node, old))) => {
2612
                    // Git has no identity for a directory: one that comes
2613
                    // back where it was is the same directory, brought back
2614
                    // with a move, so lines of history that placed files in
2615
                    // it before and after agree (oxygen#30).
26162×
                    self.tree_ops.push(TreeOp::Move {
26172×
                        node,
26182×
                        old: OpRef {
26192×
                            change: old.0,
26202×
                            index: old.1,
26212×
                        },
26222×
                        parent,
26232×
                        name: comp.to_string(),
26242×
                    });
26252×
                    self.dir_nodes.insert(prefix.clone(), node);
26262×
                    node
2627
                }
2628
                (None, None) => {
2629158×
                    let node = self.new_node(&prefix, NodeKind::Dir);
2630158×
                    self.tree_ops.push(TreeOp::AddNode {
2631158×
                        node,
2632158×
                        kind: NodeKind::Dir,
2633158×
                        mode: Mode::default(),
2634158×
                        parent,
2635158×
                        name: comp.to_string(),
2636158×
                    });
2637158×
                    self.dir_nodes.insert(prefix.clone(), node);
2638158×
                    node
2639
                }
2640
            };
2641
        }
2642154×
        parent
26436242×
    }
2644
26456207×
    fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> {
26466207×
        let (dir, name) = split_path(path);
26476207×
        let parent = self.ensure_dir(dir);
26486207×
        let node = self.new_node(path, e.kind);
26496207×
        let opaque = self.opaque_for(path, e);
26506207×
        let nocompress = self.attrs.lookup(path).nocompress;
26516207×
        self.tree_ops.push(TreeOp::AddNode {
26526207×
            node,
26536207×
            kind: e.kind,
26546207×
            mode: Mode {
26556207×
                exec: e.exec,
26566207×
                opaque,
26576207×
            },
26586207×
            parent,
26596207×
            name: name.to_string(),
26606207×
        });
26616207×
        if opaque {
2662129×
            let blob = self.blob_of(repo, path, e, nocompress)?;
2663129×
            self.edits.push(Edit::Replace {
2664129×
                node,
2665129×
                old: None,
2666129×
                new: Some(blob),
2667129×
            });
26686078×
        } else if !e.content.is_empty() {
26696045×
            let content = self.stored_text(path, &e.content);
26706045×
            self.edits.push(Edit::Insert {
26716045×
                node,
26726045×
                after: crate::objects::AtomRef::Start,
26736045×
                before: crate::objects::AtomRef::End,
26746045×
                lines: crate::lines::Lines::split_shared(content),
26756045×
            });
26766045×
        }
26776207×
        self.statuses.push(PathStatus {
26786207×
            path: path.to_string(),
26796207×
            status: ChangeStatus::Added,
26806207×
        });
26816207×
        Ok(())
26826207×
    }
2683
2684
    /// Move the node at `old` to `new` and apply `e`'s content to it.
268535×
    fn move_path(
268635×
        &mut self,
268735×
        repo: &Repo,
268835×
        old: &str,
268935×
        new: &str,
269035×
        e: &FsEntry,
269135×
        expected: &crate::state::FileState,
269235×
    ) -> Result<()> {
269335×
        let placement = self.base_snapshot.tree.entries[old].placement;
269435×
        let (dir, name) = split_path(new);
269535×
        let parent = self.ensure_dir(dir);
269635×
        self.tree_ops.push(TreeOp::Move {
269735×
            node: expected.node,
269835×
            old: op_ref(placement),
269935×
            parent,
270035×
            name: name.to_string(),
270135×
        });
270235×
        for k in self.same_place(&expected.node, placement) {
27030×
            self.tree_ops.push(TreeOp::RemoveNode {
27040×
                node: expected.node,
27050×
                placement: op_ref(k),
27060×
            });
27070×
        }
270835×
        self.update_path(repo, old, e, expected)?;
2709
        // update_path reports under the old path; the rename is what the user sees.
271035×
        self.statuses.retain(|s| s.path != old);
271135×
        self.statuses.push(PathStatus {
271235×
            path: new.to_string(),
271335×
            status: ChangeStatus::Renamed,
271435×
        });
271535×
        Ok(())
271635×
    }
2717
2718
    /// For a Git import: a node the commit keeps is in one place, the one
2719
    /// its tree names, so any other live placement it has in the base goes;
2720
    /// and a node the tree cannot show at all (an orphan) goes too. Such a
2721
    /// placement is out of sight, under a directory another line of history
2722
    /// removed, where no diff of the tree would ever reach it (oxygen#30).
272388×
    fn retire_stray_placements(&mut self) {
272488×
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
272588×
        let mut placed: BTreeSet<NodeId> = BTreeSet::new();
272699×
        for op in &self.tree_ops {
272799×
            match op {
272819×
                TreeOp::Move { node, old, .. } => {
272919×
                    retired.insert((old.change, old.index));
273019×
                    placed.insert(*node);
273119×
                }
273221×
                TreeOp::RemoveNode { placement, .. } => {
273321×
                    retired.insert((placement.change, placement.index));
273421×
                }
273559×
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
2736
            }
2737
        }
2738
        // Nodes kept where they were: what the base's tree view shows and
2739
        // this change neither moves nor removes.
274088×
        let mut kept: HashMap<NodeId, (ChangeHash, u32)> = HashMap::new();
2741241×
        for e in self.base_snapshot.tree.entries.values() {
2742241×
            if !retired.contains(&e.placement) {
2743204×
                kept.insert(e.node, e.placement);
2744204×
            }
2745
        }
2746
        // Only a node with several live placements can have a stray one,
2747
        // and the tree view names each as a conflict.
274888×
        let multi: BTreeSet<NodeId> = self
274988×
            .base_snapshot
275088×
            .tree
275188×
            .conflicts
275288×
            .iter()
275388×
            .filter_map(|c| match c {
27541×
                crate::tree::TreeConflict::MultiPlacement { node, .. } => Some(*node),
27556×
                _ => None,
27567×
            })
275788×
            .collect();
275888×
        let mut extra = Vec::new();
2759
        // A node whose only placements are under a directory another line
2760
        // removed (an orphan) is nowhere the tree can show, and Git's tree
2761
        // has no such path: it goes.
276288×
        for c in &self.base_snapshot.tree.conflicts {
27637×
            let crate::tree::TreeConflict::Orphan { node, .. } = c else {
27646×
                continue;
2765
            };
27661×
            if kept.contains_key(node) || placed.contains(node) {
27670×
                continue;
27681×
            }
27691×
            for (k, _) in self.base.tree().live_placements(node) {
27701×
                if retired.insert(k) {
27711×
                    extra.push(TreeOp::RemoveNode {
27721×
                        node: *node,
27731×
                        placement: op_ref(k),
27741×
                    });
27751×
                }
2776
            }
2777
        }
277888×
        for node in placed
277988×
            .iter()
278088×
            .chain(multi.iter().filter(|n| kept.contains_key(*n)))
2781
        {
278219×
            for (k, _) in self.base.tree().live_placements(node) {
278318×
                if retired.contains(&k) || kept.get(node) == Some(&k) {
278417×
                    continue;
27851×
                }
27861×
                retired.insert(k);
27871×
                extra.push(TreeOp::RemoveNode {
27881×
                    node: *node,
27891×
                    placement: op_ref(k),
27901×
                });
2791
            }
2792
        }
279388×
        if !extra.is_empty() {
27942×
            self.resolves = true;
27952×
            self.tree_ops.extend(extra);
279686×
        }
279788×
    }
2798
2799
    /// Nodes the base shows as one with another (identical additions at one
2800
    /// path, spec §9.2) are retired by the change that first touches the
2801
    /// node shown: edited, moved, removed or given a mode alone, it would
2802
    /// differ from them and the name conflict would come back. Nothing
2803
    /// else changes for the user, so no status reports it.
28042406×
    fn retire_unified(&mut self) {
28052406×
        let unified = &self.base_snapshot.tree.unified;
28062406×
        if unified.is_empty() {
28072376×
            return;
280830×
        }
280930×
        let touched: HashSet<NodeId> = self
281030×
            .tree_ops
281130×
            .iter()
281230×
            .map(|o| o.node())
281330×
            .chain(self.edits.iter().map(|e| e.node()))
281430×
            .collect();
281530×
        let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new();
281630×
        for op in &self.tree_ops {
281714×
            match op {
28183×
                TreeOp::Move { old, .. } => {
28193×
                    retired.insert((old.change, old.index));
28203×
                }
28213×
                TreeOp::RemoveNode { placement, .. } => {
28223×
                    retired.insert((placement.change, placement.index));
28233×
                }
28248×
                TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {}
2825
            }
2826
        }
282730×
        let mut extra = Vec::new();
282831×
        for (dup, shown) in unified {
282931×
            if !touched.contains(shown) {
283014×
                continue;
283117×
            }
283217×
            for (k, _) in self.base.tree().live_placements(dup) {
283317×
                if retired.insert(k) {
283417×
                    extra.push(TreeOp::RemoveNode {
283517×
                        node: *dup,
283617×
                        placement: op_ref(k),
283717×
                    });
283817×
                }
2839
            }
2840
        }
284130×
        self.tree_ops.extend(extra);
28422406×
    }
2843
2844
    /// The node's other live placements at the same place as `k`, the one
2845
    /// the tree shows: two lines of history that put a node back where it
2846
    /// was each leave a placement, and the tree shows them as one file. A
2847
    /// change that removes or moves it retires them together, or the copy
2848
    /// left behind outlives its directory (oxygen#88).
2849147×
    fn same_place(&self, node: &NodeId, k: (ChangeHash, u32)) -> Vec<(ChangeHash, u32)> {
2850147×
        let tree = self.base.tree();
2851147×
        let Some(shown) = tree.placement(&k) else {
28520×
            return Vec::new();
2853
        };
2854147×
        tree.live_placements(node)
2855147×
            .into_iter()
2856149×
            .filter(|(o, p)| *o != k && p.parent == shown.parent && p.name == shown.name)
2857147×
            .map(|(o, _)| o)
2858147×
            .collect()
2859147×
    }
2860
2861112×
    fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) {
2862112×
        let entry = &self.base_snapshot.tree.entries[path];
2863112×
        let placement = entry.placement;
2864112×
        self.tree_ops.push(TreeOp::RemoveNode {
2865112×
            node: expected.node,
2866112×
            placement: op_ref(placement),
2867112×
        });
2868112×
        for k in self.same_place(&expected.node, placement) {
28691×
            self.tree_ops.push(TreeOp::RemoveNode {
28701×
                node: expected.node,
28711×
                placement: op_ref(k),
28721×
            });
28731×
        }
2874112×
        if path.contains(".oxy-conflict-") || expected.is_conflicted() {
28757×
            self.resolves = true;
2876105×
        }
2877112×
        self.statuses.push(PathStatus {
2878112×
            path: path.to_string(),
2879112×
            status: ChangeStatus::Removed,
2880112×
        });
2881112×
    }
2882
288313295×
    fn update_path(
288413295×
        &mut self,
288513295×
        repo: &Repo,
288613295×
        path: &str,
288713295×
        e: &FsEntry,
288813295×
        expected: &crate::state::FileState,
288913295×
    ) -> Result<()> {
289013295×
        if expected.kind != e.kind {
28911×
            self.remove_path(path, expected);
28921×
            self.statuses.pop();
28931×
            self.add_path(repo, path, e)?;
28941×
            self.statuses.pop();
28951×
            self.statuses.push(PathStatus {
28961×
                path: path.to_string(),
28971×
                status: ChangeStatus::Modified,
28981×
            });
28991×
            return Ok(());
290013294×
        }
290113294×
        let entry = &self.base_snapshot.tree.entries[path];
290213294×
        let node = expected.node;
290313294×
        let mode_atom = entry.mode_atom.map(op_ref);
290413294×
        if e.unchanged {
2905
            // Known equal to the base's unconflicted rendering (spec
2906
            // §12.3); only the executable bit can differ.
29074243×
            if e.exec != expected.mode.exec {
29083×
                if let Some(old) = mode_atom {
29093×
                    self.tree_ops.push(TreeOp::SetMode {
29103×
                        node,
29113×
                        old,
29123×
                        mode: Mode {
29133×
                            exec: e.exec,
29143×
                            opaque: false,
29153×
                        },
29163×
                    });
29173×
                    self.statuses.push(PathStatus {
29183×
                        path: path.to_string(),
29193×
                        status: ChangeStatus::ModeChanged,
29203×
                    });
29213×
                }
29224240×
            }
29234243×
            return Ok(());
29249051×
        }
29259051×
        let mut status: Option<ChangeStatus> = None;
29269051×
        let wants_opaque = self.opaque_for(path, e);
29279051×
        let nocompress = self.attrs.lookup(path).nocompress;
29289051×
        let disk_text = self.stored_text(path, &e.content);
29299051×
        match &expected.content {
29300×
            FileContent::Dir => {}
2931
            FileContent::Text {
29328776×
                bytes,
29338776×
                materialized,
2934
            } => {
29358776×
                if materialized.is_conflicted() {
2936109×
                    if bytes == &*disk_text {
293766×
                        return Ok(());
293843×
                    }
293943×
                    let regions = self.base.conflict_regions(&node);
294043×
                    let plain: Vec<&[u8]> =
294196×
                        regions.plain.iter().map(|(_, l)| l.as_slice()).collect();
294243×
                    if has_own_conflict_markers(&e.content, bytes, &plain) {
29435×
                        self.statuses.push(PathStatus {
29445×
                            path: path.to_string(),
29455×
                            status: ChangeStatus::ConflictEdited,
29465×
                        });
29475×
                        return Ok(());
294838×
                    }
2949
                    // Resolution (spec §9.6): diff against the unconflicted lines
2950
                    // with each region as a gap, then retire every region atom.
2951
                    // Whatever the author kept from a region is re-inserted as
2952
                    // new atoms, which leaves the graph totally ordered.
295338×
                    let mut edits = text_edits(node, &regions.plain, &disk_text);
295438×
                    if !regions.region_atoms.is_empty() {
295538×
                        edits.insert(
295638×
                            0,
295738×
                            Edit::Delete {
295838×
                                node,
295938×
                                atoms: ranges(&regions.region_atoms),
296038×
                            },
296138×
                        );
296238×
                    }
296338×
                    self.edits.extend(edits);
296438×
                    self.contributors.extend(regions.contributors);
296538×
                    self.resolves = true;
296638×
                    if e.exec != expected.mode.exec {
29670×
                        if let Some(old) = mode_atom {
29680×
                            self.tree_ops.push(TreeOp::SetMode {
29690×
                                node,
29700×
                                old,
29710×
                                mode: Mode {
29720×
                                    exec: e.exec,
29730×
                                    opaque: false,
29740×
                                },
29750×
                            });
29760×
                        }
297738×
                    }
297838×
                    self.statuses.push(PathStatus {
297938×
                        path: path.to_string(),
298038×
                        status: ChangeStatus::Resolved,
298138×
                    });
298238×
                    return Ok(());
29838667×
                }
29848667×
                if wants_opaque {
2985
                    // Text → opaque (spec §7.5), by content or by attribute.
29862×
                    if let Some(old) = mode_atom {
29872×
                        self.tree_ops.push(TreeOp::SetMode {
29882×
                            node,
29892×
                            old,
29902×
                            mode: Mode {
29912×
                                exec: e.exec,
29922×
                                opaque: true,
29932×
                            },
29942×
                        });
29952×
                    }
29962×
                    let live: Vec<_> = repo_live_keys(self.base, &node);
29972×
                    if !live.is_empty() {
29982×
                        self.edits.push(Edit::Delete {
29992×
                            node,
30002×
                            atoms: ranges(&live),
30012×
                        });
30022×
                    }
30032×
                    let blob = self.blob_of(repo, path, e, nocompress)?;
30042×
                    self.edits.push(Edit::Replace {
30052×
                        node,
30062×
                        old: None,
30072×
                        new: Some(blob),
30082×
                    });
30092×
                    status = Some(ChangeStatus::Modified);
30108665×
                } else if bytes != &*disk_text {
3011857×
                    let mut laps = crate::timing::Laps::start();
3012
                    // The base's rendering holds its live atoms in order,
3013
                    // one per line (it is not conflicted here).
3014857×
                    let edits = match self.diffs.remove(path) {
301580×
                        Some(edits) => edits,
3016
                        None => {
3017777×
                            let keys = materialized.live_atoms();
3018777×
                            let lines = crate::diff::split_lines(bytes);
3019777×
                            if keys.len() == lines.len() {
3020777×
                                crate::diff::text_edits_of(node, &keys, &lines, &disk_text)
3021
                            } else {
30220×
                                text_edits(node, &self.base.live_lines(&node), &disk_text)
3023
                            }
3024
                        }
3025
                    };
3026857×
                    laps.lap("build: text diff");
3027857×
                    if !edits.is_empty() {
3028857×
                        self.edits.extend(edits);
3029857×
                        status = Some(ChangeStatus::Modified);
3030857×
                    }
30317808×
                }
30328667×
                let switched_to_opaque = wants_opaque;
30338667×
                if !switched_to_opaque && e.exec != expected.mode.exec {
30344×
                    if let Some(old) = mode_atom {
30354×
                        self.tree_ops.push(TreeOp::SetMode {
30364×
                            node,
30374×
                            old,
30384×
                            mode: Mode {
30394×
                                exec: e.exec,
30404×
                                opaque: false,
30414×
                            },
30424×
                        });
30434×
                        status.get_or_insert(ChangeStatus::ModeChanged);
30444×
                    }
30458663×
                }
3046
            }
3047275×
            FileContent::Opaque { blobs } => {
3048275×
                let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content));
3049275×
                if e.kind == NodeKind::File && wants_opaque {
3050217×
                    // Unchanged or replaced, the file stays opaque: remember its blob.
3051217×
                    self.blobs.insert(path.to_string(), new_ref);
3052217×
                }
3053275×
                if blobs.len() > 1 && self.resolve.contains(path) && e.kind == expected.kind {
3054
                    // Settle the replace conflict (spec §9.3): replace every
3055
                    // live candidate with the chosen blob, depending on each
3056
                    // change that made one live, so all are superseded.
30575×
                    let chosen = self.blob_of(repo, path, e, nocompress)?;
305810×
                    for b in blobs {
305910×
                        self.edits.push(Edit::Replace {
306010×
                            node,
306110×
                            old: Some(*b),
306210×
                            new: Some(chosen),
306310×
                        });
306410×
                    }
30655×
                    self.contributors
30665×
                        .extend(self.base.blob_contributors(&node, blobs));
30675×
                    self.resolves = true;
30685×
                    self.statuses.push(PathStatus {
30695×
                        path: path.to_string(),
30705×
                        status: ChangeStatus::Resolved,
30715×
                    });
30725×
                    return Ok(());
3073270×
                }
3074270×
                if blobs.len() > 1 {
30759×
                    if !blobs.contains(&new_ref) {
30764×
                        self.statuses.push(PathStatus {
30774×
                            path: path.to_string(),
30784×
                            status: ChangeStatus::ConflictEdited,
30794×
                        });
30805×
                    }
30819×
                    return Ok(());
3082261×
                }
3083261×
                let current = blobs.first().copied();
3084261×
                if current != Some(new_ref) {
308545×
                    if e.kind == NodeKind::File && !wants_opaque {
3086
                        // Opaque → text (spec §7.5).
30872×
                        if let Some(old) = mode_atom {
30882×
                            self.tree_ops.push(TreeOp::SetMode {
30892×
                                node,
30902×
                                old,
30912×
                                mode: Mode {
30922×
                                    exec: e.exec,
30932×
                                    opaque: false,
30942×
                                },
30952×
                            });
30962×
                        }
30972×
                        self.edits.push(Edit::Replace {
30982×
                            node,
30992×
                            old: current,
31002×
                            new: None,
31012×
                        });
31022×
                        if !e.content.is_empty() {
31032×
                            self.edits.push(Edit::Insert {
31042×
                                node,
31052×
                                after: crate::objects::AtomRef::Start,
31062×
                                before: crate::objects::AtomRef::End,
31072×
                                lines: crate::lines::Lines::split(&e.content),
31082×
                            });
31092×
                        }
3110
                    } else {
311143×
                        let blob = self.blob_of(repo, path, e, nocompress)?;
311243×
                        self.edits.push(Edit::Replace {
311343×
                            node,
311443×
                            old: current,
311543×
                            new: Some(blob),
311643×
                        });
3117
                        // New contents and a new executable bit at once: the
3118
                        // bit was dropped (VS Code's inno_updater.exe,
3119
                        // 2018-02-28, lost +x for 49 commits; oxygen#87).
312043×
                        if e.kind == NodeKind::File && e.exec != expected.mode.exec {
31212×
                            if let Some(old) = mode_atom {
31222×
                                self.tree_ops.push(TreeOp::SetMode {
31232×
                                    node,
31242×
                                    old,
31252×
                                    mode: Mode {
31262×
                                        exec: e.exec,
31272×
                                        opaque: true,
31282×
                                    },
31292×
                                });
31302×
                            }
313141×
                        }
3132
                    }
313345×
                    status = Some(ChangeStatus::Modified);
3134216×
                } else if e.kind == NodeKind::File && e.exec != expected.mode.exec {
31350×
                    if let Some(old) = mode_atom {
31360×
                        self.tree_ops.push(TreeOp::SetMode {
31370×
                            node,
31380×
                            old,
31390×
                            mode: Mode {
31400×
                                exec: e.exec,
31410×
                                opaque: true,
31420×
                            },
31430×
                        });
31440×
                        status = Some(ChangeStatus::ModeChanged);
31450×
                    }
3146216×
                }
3147
            }
3148
        }
31498928×
        if let Some(s) = status {
3150908×
            self.statuses.push(PathStatus {
3151908×
                path: path.to_string(),
3152908×
                status: s,
3153908×
            });
31548020×
        }
31558928×
        Ok(())
315613295×
    }
3157
}
3158
31592×
fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> {
31602×
    base.live_lines(node).into_iter().map(|(k, _)| k).collect()
31612×
}
3162
3163
/// The changes a change's scope references (spec §8.1): introducers of every
3164
/// atom, placement, and mode it touches, and of every node it operates on.
31652451×
pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> {
31662451×
    let mut out = BTreeSet::new();
31672451×
    let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| {
3168195×
        if !r.change.is_zero() {
3169195×
            out.insert(r.change);
3170195×
        }
3171195×
    };
31722451×
    let mut nodes: Vec<NodeId> = Vec::new();
31736778×
    for op in &change.tree_ops {
31746778×
        match op {
31756583×
            TreeOp::AddNode { parent, .. } => nodes.push(*parent),
3176146×
            TreeOp::RemoveNode { node, placement } => {
3177146×
                nodes.push(*node);
3178146×
                opref(placement, &mut out);
3179146×
            }
3180
            TreeOp::Move {
318137×
                node, old, parent, ..
318237×
            } => {
318337×
                nodes.push(*node);
318437×
                nodes.push(*parent);
318537×
                opref(old, &mut out);
318637×
            }
318712×
            TreeOp::SetMode { node, old, .. } => {
318812×
                nodes.push(*node);
318912×
                opref(old, &mut out);
319012×
            }
3191
        }
3192
    }
31938449×
    for e in &change.edits {
31948449×
        nodes.push(e.node());
3195189×
        match e {
31967672×
            Edit::Insert { after, before, .. } => {
319715344×
                for r in [after, before] {
319815344×
                    if let crate::objects::AtomRef::Atom { change, .. } = r {
31992266×
                        if !change.is_zero() {
32002263×
                            out.insert(*change);
32012263×
                        }
320213078×
                    }
3203
                }
3204
            }
3205588×
            Edit::Delete { atoms, .. } => {
3206
                // Deleting an atom also depends on whoever inserted the live
3207
                // atoms right after it: the author saw those insertions, and
3208
                // without the dependency spec §9.1 would call the deletion
3209
                // concurrent with them (a delete/insert conflict).
3210588×
                let graph = base.file(&e.node());
32111159×
                for r in atoms {
32121159×
                    if !r.change.is_zero() {
32131158×
                        out.insert(r.change);
32141158×
                    }
32151159×
                    if let Some(g) = graph {
32161206×
                        for i in r.from..r.to {
32171206×
                            out.extend(
32181206×
                                g.live_successor_changes(&(r.change, i))
32191206×
                                    .into_iter()
32201206×
                                    .filter(|c| !c.is_zero()),
3221
                            );
3222
                        }
32230×
                    }
3224
                }
3225
            }
3226
            // Replacing a blob depends on the change that made it current,
3227
            // as deleting a line depends on the line's introducer.
3228
            Edit::Replace {
322960×
                node,
323060×
                old: Some(old),
3231
                ..
3232
            } => {
323360×
                out.extend(
323460×
                    base.live_replaces(node)
323560×
                        .into_iter()
323677×
                        .filter(|(c, b)| b == old && !c.is_zero())
323760×
                        .map(|(c, _)| c),
3238
                );
3239
            }
3240129×
            Edit::Replace { .. } => {}
3241
        }
3242
    }
324315264×
    for n in nodes {
324415264×
        if let Some(h) = base.tree().introducer(&n) {
32452334×
            out.insert(h);
324612930×
        }
3247
    }
32482451×
    out
32492451×
}
3250
3251
/// Paths of the nodes a change adds on top of `base_snapshot`.
32521565×
pub fn added_node_paths(
32531565×
    change: &Change,
32541565×
    base_snapshot: &Snapshot,
32551565×
) -> HashMap<String, (NodeId, NodeKind)> {
32561565×
    let by_node = base_snapshot.tree.by_node();
32571565×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
32581565×
    for op in &change.tree_ops {
3259
        if let TreeOp::AddNode {
326097×
            node,
326197×
            parent,
326297×
            name,
326397×
            kind,
3264
            ..
3265129×
        } = op
326697×
        {
326797×
            new_names.insert(*node, (*parent, name.clone(), *kind));
326897×
        }
3269
    }
32701565×
    let mut out = HashMap::new();
32711565×
    for (node, (_, _, kind)) in &new_names {
327297×
        if let Some(path) = path_of_node(node, &by_node, &new_names) {
327397×
            out.insert(path, (*node, *kind));
327497×
        }
3275
    }
32761565×
    out
32771565×
}
3278
3279
/// Whether `op` retires a node `base_snapshot` shows as one with another
3280
/// (spec §9.2): part of touching that node, never a removal the user made.
328137×
fn is_unified_retirement(op: &TreeOp, base_snapshot: &Snapshot) -> bool {
32828×
    matches!(op, TreeOp::RemoveNode { node, .. } if base_snapshot.tree.unified.contains_key(node))
328337×
}
3284
3285232×
fn path_of_node(
3286232×
    n: &NodeId,
3287232×
    by_node: &HashMap<NodeId, &crate::tree::TreeEntry>,
3288232×
    new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>,
3289232×
) -> Option<String> {
3290232×
    if let Some(e) = by_node.get(n) {
329169×
        return Some(e.path.clone());
3292163×
    }
3293163×
    let mut parts = Vec::new();
3294163×
    let mut cur = *n;
3295
    loop {
3296322×
        if cur.is_root() {
3297146×
            break;
3298176×
        }
3299176×
        if let Some(e) = by_node.get(&cur) {
33000×
            if !e.path.is_empty() {
33010×
                parts.push(e.path.clone());
33020×
            }
33030×
            break;
3304176×
        }
3305176×
        let (parent, name, _) = new_names.get(&cur)?;
3306159×
        parts.push(name.clone());
3307159×
        cur = *parent;
3308
    }
3309146×
    parts.reverse();
3310146×
    Some(parts.join("/"))
3311232×
}
3312
3313
/// Render a change's operations with line content, using `base` for deleted lines.
331433×
pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String {
331533×
    let by_node = base_snapshot.tree.by_node();
331633×
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
331733×
    for op in &change.tree_ops {
3318
        if let TreeOp::AddNode {
331916×
            node,
332016×
            parent,
332116×
            name,
332216×
            kind,
3323
            ..
332424×
        } = op
332516×
        {
332616×
            new_names.insert(*node, (*parent, name.clone(), *kind));
332716×
        }
3328
    }
332973×
    let path_of = |n: &NodeId| -> String {
333073×
        path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8)))
333173×
    };
3332
333333×
    let mut out = String::new();
333433×
    for op in change
333533×
        .tree_ops
333633×
        .iter()
333733×
        .filter(|o| !is_unified_retirement(o, base_snapshot))
3338
    {
333923×
        match op {
3340
            TreeOp::AddNode {
334116×
                node, kind, mode, ..
3342
            } => {
334316×
                out.push_str(&format!(
3344
                    "A {} ({:?}{}{})\n",
334516×
                    path_of(node),
3346
                    kind,
334716×
                    if mode.exec { ", exec" } else { "" },
334816×
                    if mode.opaque { ", opaque" } else { "" }
3349
                ));
3350
            }
33515×
            TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))),
3352
            TreeOp::Move {
33532×
                node, parent, name, ..
3354
            } => {
33552×
                let to = if parent.is_root() {
33560×
                    name.clone()
3357
                } else {
33582×
                    format!("{}/{name}", path_of(parent))
3359
                };
33602×
                out.push_str(&format!("R {} -> {to}\n", path_of(node)))
3361
            }
33620×
            TreeOp::SetMode { node, mode, .. } => {
33630×
                out.push_str(&format!(
33640×
                    "M {} mode: exec={} opaque={}\n",
33650×
                    path_of(node),
33660×
                    mode.exec,
33670×
                    mode.opaque
33680×
                ));
33690×
            }
3370
        }
3371
    }
3372
    // Every other file the change edits is modified: the summary names
3373
    // every path it touches, not only those with a tree operation.
337433×
    let with_op: HashSet<NodeId> = change.tree_ops.iter().map(|o| o.node()).collect();
337533×
    let mut listed: HashSet<NodeId> = HashSet::new();
337636×
    for e in &change.edits {
337736×
        let n = e.node();
337836×
        if !with_op.contains(&n) && listed.insert(n) {
337921×
            out.push_str(&format!("M {}\n", path_of(&n)));
338021×
        }
3381
    }
338233×
    let mut last_node: Option<NodeId> = None;
338336×
    for e in &change.edits {
338436×
        let n = e.node();
338536×
        if last_node != Some(n) {
338627×
            out.push_str(&format!("--- {}\n", path_of(&n)));
338727×
            last_node = Some(n);
338827×
        }
338936×
        match e {
339027×
            Edit::Insert { lines, .. } => {
339132×
                for l in lines.iter() {
339232×
                    out.push('+');
339332×
                    out.push_str(&String::from_utf8_lossy(l));
339432×
                    if !l.ends_with(b"\n") {
33950×
                        out.push('\n');
339632×
                    }
3397
                }
3398
            }
33998×
            Edit::Delete { atoms, .. } => {
34008×
                for r in atoms {
34018×
                    for i in r.from..r.to {
34028×
                        let content = base
34038×
                            .file(&n)
34048×
                            .and_then(|g| g.content(&(r.change, i)))
34058×
                            .unwrap_or(b"<unknown>\n");
34068×
                        out.push('-');
34078×
                        out.push_str(&String::from_utf8_lossy(content));
34088×
                        if !content.ends_with(b"\n") {
34090×
                            out.push('\n');
34108×
                        }
3411
                    }
3412
                }
3413
            }
34141×
            Edit::Replace { old, new, .. } => {
34151×
                out.push_str(&format!(
3416
                    "  opaque content {} -> {}\n",
34171×
                    old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()),
34181×
                    new.map(|o| o.short(12)).unwrap_or_else(|| "none".into())
3419
                ));
3420
            }
3421
        }
3422
    }
342333×
    out
342433×
}
3425
3426
/// Pair removed files with added files of the same kind whose contents are
3427
/// at least `RENAME_THRESHOLD` percent alike, best matches first. Beyond
3428
/// `RENAME_LIMIT` candidate pairs only identical contents are paired.
3429
const RENAME_THRESHOLD: u32 = 50;
3430
const RENAME_LIMIT: usize = 10_000;
3431
3432
/// How a change is built beyond its entries: for a Git import, the seed
3433
/// its new nodes' identifiers derive from; and, when it is built from part
3434
/// of the tree, whether the new tree has anything under a directory.
3435
#[derive(Default)]
3436
struct Scope<'a> {
3437
    seed: Option<[u8; 32]>,
3438
    dir_needed: Option<&'a dyn Fn(&str) -> bool>,
3439
}
3440
34412406×
fn detect_renames(
34422406×
    base_snapshot: &Snapshot,
34432406×
    fs_entries: &BTreeMap<String, FsEntry>,
34442406×
    in_scope: impl Fn(&str) -> bool,
34452406×
) -> Vec<(String, String)> {
34462406×
    let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot
34472406×
        .files
34482406×
        .iter()
344913860×
        .filter(|(p, f)| {
345013860×
            f.kind != NodeKind::Dir
345113392×
                && !fs_entries.contains_key(*p)
3452132×
                && in_scope(p)
3453122×
                && !f.is_conflicted()
345413860×
        })
34552406×
        .collect();
34562406×
    let added: Vec<(&String, &FsEntry)> = fs_entries
34572406×
        .iter()
345819501×
        .filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p))
34592406×
        .collect();
34602406×
    if removed.is_empty() || added.is_empty() {
34612343×
        return vec![];
346263×
    }
346363×
    let exact_only = removed.len() * added.len() > RENAME_LIMIT;
346477×
    let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> {
346577×
        match &f.content {
346673×
            FileContent::Text { bytes, .. } => Some(bytes.clone()),
34674×
            _ => None,
3468
        }
346977×
    };
347063×
    let mut scored: Vec<(u32, usize, usize)> = Vec::new();
347177×
    for (ri, (_, f)) in removed.iter().enumerate() {
347277×
        let old = old_content(f);
347377×
        let old_blob = match &f.content {
34744×
            FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]),
347573×
            _ => None,
3476
        };
347796×
        for (ai, (_, e)) in added.iter().enumerate() {
347896×
            if e.kind != f.kind {
34790×
                continue;
348096×
            }
348196×
            let score = match (&old, old_blob) {
348286×
                (Some(o), _) => {
348386×
                    if exact_only {
34840×
                        if *o == *e.content {
34850×
                            100
3486
                        } else {
34870×
                            0
3488
                        }
3489
                    } else {
349086×
                        similarity(o, &e.content)
3491
                    }
3492
                }
349310×
                (None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100,
34947×
                _ => 0,
3495
            };
349696×
            if score >= RENAME_THRESHOLD {
349741×
                scored.push((score, ri, ai));
349855×
            }
3499
        }
3500
    }
350163×
    scored.sort_by(|a, b| {
35029×
        b.0.cmp(&a.0)
35039×
            .then(removed[a.1].0.cmp(removed[b.1].0))
35049×
            .then(added[a.2].0.cmp(added[b.2].0))
35059×
    });
350663×
    let mut used_r = vec![false; removed.len()];
350763×
    let mut used_a = vec![false; added.len()];
350863×
    let mut out = Vec::new();
350963×
    for (_, ri, ai) in scored {
351041×
        if used_r[ri] || used_a[ai] {
35116×
            continue;
351235×
        }
351335×
        used_r[ri] = true;
351435×
        used_a[ai] = true;
351535×
        out.push((removed[ri].0.clone(), added[ai].0.clone()));
3516
    }
351763×
    out
35182406×
}
3519
3520
/// Percentage of the larger file's bytes found in lines the two share
3521
/// (multiset intersection of lines), like Git's similarity index.
352290×
pub fn similarity(a: &[u8], b: &[u8]) -> u32 {
352390×
    if a == b {
352425×
        return 100;
352565×
    }
352665×
    if a.is_empty() || b.is_empty() {
35276×
        return 0;
352859×
    }
352959×
    let mut counts: HashMap<&[u8], usize> = HashMap::new();
3530113×
    for l in split_lines(a) {
3531113×
        *counts.entry(l).or_default() += 1;
3532113×
    }
353359×
    let mut shared = 0usize;
3534111×
    for l in split_lines(b) {
3535111×
        if let Some(n) = counts.get_mut(l) {
353642×
            if *n > 0 {
353742×
                *n -= 1;
353842×
                shared += l.len();
353942×
            }
354069×
        }
3541
    }
354259×
    (shared * 100 / a.len().max(b.len())) as u32
354390×
}
3544
3545
#[cfg(test)]
3546
mod join_tests {
3547
    use super::*;
3548
3549
    #[test]
35501×
    fn missing_from_finds_what_the_other_lacks() {
35511×
        let a: BTreeMap<String, u8> = [("a", 1), ("b", 2), ("c/d", 3), ("e", 4)]
35521×
            .into_iter()
35534×
            .map(|(k, v)| (k.to_string(), v))
35541×
            .collect();
35551×
        let b: BTreeMap<String, ()> = [("0", ()), ("b", ()), ("c", ()), ("e", ())]
35561×
            .into_iter()
35574×
            .map(|(k, v)| (k.to_string(), v))
35581×
            .collect();
35591×
        let got: Vec<&str> = missing_from(&a, &b)
35601×
            .iter()
35612×
            .map(|(k, _)| k.as_str())
35621×
            .collect();
35631×
        assert_eq!(got, vec!["a", "c/d"]);
35641×
    }
3565
3566
    #[test]
35671×
    fn join_pairs_every_key_in_order() {
35681×
        let a: BTreeMap<String, u8> = [("a", 1), ("b/c", 2), ("d", 3)]
35691×
            .into_iter()
35703×
            .map(|(k, v)| (k.to_string(), v))
35711×
            .collect();
35721×
        let b: BTreeMap<String, char> = [("b", 'x'), ("b/c", 'y'), ("e", 'z')]
35731×
            .into_iter()
35743×
            .map(|(k, v)| (k.to_string(), v))
35751×
            .collect();
35761×
        let got: Vec<(String, Option<u8>, Option<char>)> = join(&a, &b)
35775×
            .map(|(k, x, y)| (k.clone(), x.copied(), y.copied()))
35781×
            .collect();
35791×
        let want = vec![
35801×
            ("a".to_string(), Some(1), None),
35811×
            ("b".to_string(), None, Some('x')),
35821×
            ("b/c".to_string(), Some(2), Some('y')),
35831×
            ("d".to_string(), Some(3), None),
35841×
            ("e".to_string(), None, Some('z')),
3585
        ];
35861×
        assert_eq!(got, want);
35871×
    }
3588
}