OXYGENHUB
# da58bbbbc4d9 oxygen / crates / oxy-core / src / repo / snapshot.rs
35.5 KiB · Rust · last changed Snapshot: the manifest is a stat cache; unchanged opaque files are not read and contribute their recorded blob, with a two-second racy-stat window (status on 1 GiB 2.4 s to 4 ms; spec 0.6s) 1 month ago source blame history raw
1
//! Working-copy snapshots, commits, and the change builder (spec §12.3).
2
3
use super::*;
4
5
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6
pub enum ChangeStatus {
7
    Added,
8
    Modified,
9
    Removed,
10
    /// Moved from another path (detected by similarity, tracked as a placement change).
11
    Renamed,
12
    ModeChanged,
13
    /// The file is conflicted in the base state and still contains conflict markers.
14
    ConflictEdited,
15
    /// The file was conflicted in the base state and the working copy resolves it.
16
    Resolved,
17
}
18
19
/// Lines that mark a conflict region in a materialized file (spec §9.5).
20
pub fn has_conflict_markers(content: &[u8]) -> bool {
21
    split_lines(content).iter().any(|l| {
22
        l.starts_with(b"<<<<<<< oxy conflict") || l.starts_with(b"======= oxy conflict") || l.starts_with(b">>>>>>> oxy conflict")
23
    })
24
}
25
26
#[derive(Clone, Debug, PartialEq, Eq)]
27
pub struct PathStatus {
28
    pub path: String,
29
    pub status: ChangeStatus,
30
}
31
32
/// Operations of a change built from the working directory against a base.
33
pub(crate) struct BuiltChange {
34
    pub tree_ops: Vec<TreeOp>,
35
    pub edits: Vec<Edit>,
36
    pub contributors: Vec<ChangeHash>,
37
    pub resolves: bool,
38
    pub statuses: Vec<PathStatus>,
39
    /// The stored blob of every opaque path, for the manifest's stat cache.
40
    pub blobs: HashMap<String, ObjectRef>,
41
}
42
43
/// Result of snapshotting the working copy.
44
pub struct SnapshotResult {
45
    pub wc_hash: ChangeHash,
46
    pub wc: Change,
47
    /// The base state (closure of the working-copy change's dependencies).
48
    pub base: State,
49
    pub base_snapshot: Snapshot,
50
    pub statuses: Vec<PathStatus>,
51
    /// Whether the snapshot rewrote the working-copy change.
52
    pub changed: bool,
53
    /// Whether the working copy resolves conflicts of its base (spec §9.6).
54
    pub resolves: bool,
55
    /// Changes that contributed to the conflicts being resolved.
56
    pub contributors: Vec<ChangeHash>,
57
}
58
59
pub fn is_binary(content: &[u8]) -> bool {
60
    content[..content.len().min(BINARY_SNIFF)].contains(&0)
61
}
62
63
impl Repo {
64
    /// Diff the files on disk against `base` and build the operations of a
65
    /// change from `base` to the working directory. `reuse` maps paths to the
66
    /// NodeIds an earlier version of the same change assigned to new paths.
67
    pub(crate) fn build_against(&self, base: &State, base_snapshot: &Snapshot, reuse: HashMap<String, (NodeId, NodeKind)>) -> Result<BuiltChange> {
68
        let fs_entries = self.walk_working_copy()?;
69
        self.build_from_entries(base, base_snapshot, reuse, &fs_entries)
70
    }
71
72
    /// Like `build_against`, for files given in memory (imports).
73
    pub(crate) fn build_from_entries(&self, base: &State, base_snapshot: &Snapshot, reuse: HashMap<String, (NodeId, NodeKind)>, fs_entries: &BTreeMap<String, FsEntry>) -> Result<BuiltChange> {
74
        // Attributes come from the files being committed, so a new
75
        // `.oxyattributes` applies to the same change that adds it.
76
        let attrs = crate::attributes::Attributes::from_files(
77
            fs_entries.iter().filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes")).map(|(p, e)| (p.as_str(), e.content.as_slice())),
78
        );
79
        let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs);
80
        // Rename detection (spec §7.1, §23): a removed file and an added file
81
        // of the same kind whose contents are at least half alike become one
82
        // move of the same node, so identity survives across the rename.
83
        let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p));
84
        let renamed_from: HashMap<&str, &str> = renames.iter().map(|(old, new)| (new.as_str(), old.as_str())).collect();
85
        let renamed_to: HashMap<&str, &str> = renames.iter().map(|(old, new)| (old.as_str(), new.as_str())).collect();
86
        for (path, fs_entry) in fs_entries {
87
            match base_snapshot.files.get(path) {
88
                None => match renamed_from.get(path.as_str()) {
89
                    Some(old) => b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])?,
90
                    None => b.add_path(self, path, fs_entry)?,
91
                },
92
                Some(expected) => b.update_path(self, path, fs_entry, expected)?,
93
            }
94
        }
95
        for (path, expected) in &base_snapshot.files {
96
            if fs_entries.contains_key(path) || !self.in_scope(path) || renamed_to.contains_key(path.as_str()) {
97
                continue;
98
            }
99
            // A directory stays while anything under it exists or is out of scope.
100
            let still_needed = expected.kind == NodeKind::Dir
101
                && (fs_entries.keys().any(|p| p.starts_with(&format!("{path}/")))
102
                    || base_snapshot.files.keys().any(|p| p.starts_with(&format!("{path}/")) && !self.in_scope(p)));
103
            if !still_needed {
104
                b.remove_path(path, expected);
105
            }
106
        }
107
        let mut statuses = b.statuses;
108
        statuses.sort_by(|a, b| a.path.cmp(&b.path));
109
        Ok(BuiltChange { tree_ops: b.tree_ops, edits: b.edits, contributors: b.contributors.into_iter().collect(), resolves: b.resolves, statuses, blobs: b.blobs })
110
    }
111
112
    /// Store a change and, when `core.sign = change` and we are its author,
113
    /// sign it. A rewritten change by another author is stored unsigned: only
114
    /// its author (or a delegate) can vouch for it.
115
    pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> {
116
        let ours = change.author == self.author();
117
        if self.config.sign_changes && ours && self.secret_key()?.is_none() {
118
            return fail("core.sign is \"change\" but no secret key is available for author.key");
119
        }
120
        let hash = self.store.put_change(change)?;
121
        if self.config.sign_changes && ours {
122
            self.sign_change(&hash)?;
123
        }
124
        Ok(hash)
125
    }
126
127
    /// Snapshot the working copy into the working-copy change (spec §12.3).
128
    pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> {
129
        let _lock = self.lock_snapshot()?;
130
        let batch = self.store.begin_batch()?;
131
        let result = self.snapshot_locked(command);
132
        batch.finish()?;
133
        result
134
    }
135
136
    fn snapshot_locked(&self, command: &str) -> Result<SnapshotResult> {
137
        let (op_ref, op) = self.head_op()?;
138
        let (old_hash, old_wc) = self.working_copy()?;
139
        let base = self.load_state(&old_wc.deps)?;
140
        let base_snapshot = base.materialize();
141
142
        // Reuse the NodeIds the previous working-copy version assigned to new
143
        // paths, so an unchanged working copy hashes identically (spec §12.3).
144
        let reuse = added_node_paths(&old_wc, &base_snapshot);
145
        // The last manifest lets unchanged opaque files go unread.
146
        let manifest = self.read_manifest()?.map(|(_, _, m)| m);
147
        let fs_entries = self.walk_working_copy_with(manifest.as_ref())?;
148
        let built = self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)?;
149
150
        let mut wc = Change {
151
            change_id: old_wc.change_id,
152
            deps: vec![],
153
            author: self.author(),
154
            hlc: old_wc.hlc,
155
            message: old_wc.message.clone(),
156
            tree_ops: built.tree_ops,
157
            edits: built.edits,
158
            kind: ChangeKind::WorkingCopy,
159
        };
160
        let base_heads = base.maximal(&old_wc.deps);
161
        let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect();
162
        deps.extend(referenced_changes(&wc, &base));
163
        deps.extend(built.contributors.iter().copied());
164
        wc.deps = deps.into_iter().collect();
165
        wc.normalize();
166
        let resolves = built.resolves;
167
        let contributors = built.contributors;
168
        let statuses = built.statuses;
169
        // Empty snapshot: hash the candidate before storing; only record an op on change.
170
        let new_hash = wc.hash();
171
        let changed = new_hash != old_hash;
172
        if changed {
173
            self.store.put_change(&wc)?;
174
            let mut view = op.view.clone();
175
            view.workspaces.insert(self.workspace, new_hash);
176
            self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?;
177
        }
178
        // The prompt compares against this without recording anything.
179
        self.write_manifest_entries(base_snapshot.is_conflicted(), !wc.tree_ops.is_empty() || !wc.edits.is_empty(), &fs_entries, &built.blobs)?;
180
        Ok(SnapshotResult { wc_hash: new_hash, wc, base, base_snapshot, statuses, changed, resolves, contributors })
181
    }
182
183
    /// Turn the working-copy change into a normal change on the bookmark (spec §12.3).
184
    pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> {
185
        let snap = self.snapshot("oxy commit")?;
186
        if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() {
187
            return fail("nothing to commit: the working copy matches its base");
188
        }
189
        if let Some(s) = snap.statuses.iter().find(|s| s.status == ChangeStatus::ConflictEdited) {
190
            return fail(format!("{} still contains conflict markers", s.path));
191
        }
192
        if message.trim().is_empty() {
193
            return fail("a commit message is required");
194
        }
195
        // pre-commit hook: may veto.
196
        self.run_hook(
197
            "pre-commit",
198
            &format!(
199
                "{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}",
200
                json_str(message),
201
                json_array(snap.statuses.iter().map(|s| format!("{{\"path\":{},\"status\":{}}}", json_str(&s.path), json_str(&format!("{:?}", s.status)))))
202
            ),
203
        )?;
204
        let (op_ref, op) = self.head_op()?;
205
        let mut deps: BTreeSet<ChangeHash> = referenced_changes(&snap.wc, &snap.base);
206
        // semantic-deps hook: may add explicit dependencies (spec §8.1).
207
        if self.config.semantic_deps {
208
            let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect();
209
            let by_node = snap.base_snapshot.tree.by_node();
210
            let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new();
211
            let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
212
            for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) {
213
                if !seen.insert(*h) {
214
                    continue;
215
                }
216
                let c = self.store.get_change(h)?;
217
                let mut paths: BTreeSet<String> = BTreeSet::new();
218
                for n in c.tree_ops.iter().map(|o| o.node()).chain(c.edits.iter().map(|e| e.node())) {
219
                    if let Some(e) = by_node.get(&n) {
220
                        paths.insert(e.path.clone());
221
                    }
222
                }
223
                candidates.push((c.change_id, *h, paths.into_iter().collect()));
224
            }
225
            for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? {
226
                if snap.base.contains(&d) {
227
                    deps.insert(d);
228
                }
229
            }
230
        }
231
        // A resolution depends on every change that contributed to the
232
        // conflict (spec §8.1). Tree and opaque conflicts are resolved through
233
        // ordinary operations whose references already cover the contributors.
234
        deps.extend(snap.contributors.iter().copied());
235
        for d in extra_deps {
236
            if !snap.base.contains(d) {
237
                return fail(format!("--depends-on {d}: not in the working copy's base state"));
238
            }
239
            deps.insert(*d);
240
        }
241
        let mut change = Change {
242
            change_id: snap.wc.change_id,
243
            deps: deps.into_iter().collect(),
244
            author: self.author(),
245
            hlc: Hlc::now(),
246
            message: message.trim_end().to_string(),
247
            tree_ops: snap.wc.tree_ops.clone(),
248
            edits: snap.wc.edits.clone(),
249
            kind: if snap.resolves { ChangeKind::Resolution } else { ChangeKind::Normal },
250
        };
251
        change.normalize();
252
        let hash = self.store_signed(&change)?;
253
254
        // New bookmark heads: maximal elements of old heads plus the new change.
255
        // The working copy continues on its own base plus the new change, which
256
        // is exactly what the files on disk contain; the bookmark may hold
257
        // other heads (and conflicts) the working copy has not checked out.
258
        let old_heads = self.heads_of(&op.view, &self.config.bookmark)?;
259
        let old_base = snap.base.maximal(&snap.wc.deps);
260
        let mut all: Vec<ChangeHash> = old_heads.clone();
261
        all.extend(old_base.iter().copied());
262
        all.push(hash);
263
        let mut st = self.load_state(&all)?;
264
        st.add(change)?;
265
        let mut with_new = old_heads.clone();
266
        with_new.push(hash);
267
        let new_heads = st.maximal(&with_new);
268
        // Automatic checkpoint once enough changes accumulated (spec §11).
269
        let since = self.latest_checkpoint(&self.config.bookmark)?.map(|(_, cp)| cp.count).unwrap_or(0);
270
        if self.config.checkpoint_interval > 0 && (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval {
271
            let cp_state = self.load_state_full(&new_heads)?;
272
            self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?;
273
        }
274
        let mut wc_base = old_base.clone();
275
        wc_base.push(hash);
276
        let wc_base = st.maximal(&wc_base);
277
278
        let wc = self.new_working_copy(wc_base, "");
279
        let wc_hash = self.store.put_change(&wc)?;
280
        let mut view = op.view.clone();
281
        view.bookmarks.insert(self.config.bookmark.clone(), self.store.put_state(&StateObject::new(new_heads))?);
282
        view.workspaces.insert(self.workspace, wc_hash);
283
        self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?;
284
        self.rewrite_manifest_flags(snap.base_snapshot.is_conflicted() && !snap.resolves, false)?;
285
        self.maybe_autopack()?;
286
        let _ = self.run_hook("post-commit", &format!("{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}", json_str(&snap.wc.change_id.to_hex()), json_str(&hash.to_hex()), json_str(message)));
287
        Ok(hash)
288
    }
289
}
290
291
/// Builds the tree ops and edits of a working-copy change against a base.
292
struct ChangeBuilder<'a> {
293
    base: &'a State,
294
    base_snapshot: &'a Snapshot,
295
    tree_ops: Vec<TreeOp>,
296
    edits: Vec<Edit>,
297
    statuses: Vec<PathStatus>,
298
    dir_nodes: HashMap<String, NodeId>,
299
    reuse: HashMap<String, (NodeId, NodeKind)>,
300
    /// Changes that contributed to conflicts this working copy resolves.
301
    contributors: BTreeSet<ChangeHash>,
302
    resolves: bool,
303
    attrs: crate::attributes::Attributes,
304
    blobs: HashMap<String, ObjectRef>,
305
}
306
307
impl<'a> ChangeBuilder<'a> {
308
    fn new(base: &'a State, base_snapshot: &'a Snapshot, reuse: HashMap<String, (NodeId, NodeKind)>) -> Self {
309
        let mut dir_nodes: HashMap<String, NodeId> = HashMap::new();
310
        dir_nodes.insert(String::new(), NodeId::ROOT);
311
        for (p, f) in &base_snapshot.files {
312
            if f.kind == NodeKind::Dir {
313
                dir_nodes.insert(p.clone(), f.node);
314
            }
315
        }
316
        ChangeBuilder { base, base_snapshot, tree_ops: vec![], edits: vec![], statuses: vec![], dir_nodes, reuse, contributors: BTreeSet::new(), resolves: false, attrs: crate::attributes::Attributes::default(), blobs: HashMap::new() }
317
    }
318
319
    fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self {
320
        self.attrs = attrs;
321
        self
322
    }
323
324
    /// Whether a file is opaque: forced by `binary`, forced text by `text`
325
    /// only when it has no NUL bytes, else sniffed.
326
    /// The blob for an opaque entry: the cached one, else stored now.
327
    fn blob_of(&mut self, repo: &Repo, path: &str, e: &FsEntry, nocompress: bool) -> Result<ObjectRef> {
328
        let b = match e.blob {
329
            Some(b) => b,
330
            None => repo.store_blob_for(&e.content, nocompress)?,
331
        };
332
        self.blobs.insert(path.to_string(), b);
333
        Ok(b)
334
    }
335
336
    fn opaque_for(&self, path: &str, e: &FsEntry) -> bool {
337
        if e.blob.is_some() {
338
            return true;
339
        }
340
        if e.kind == NodeKind::Symlink {
341
            return true;
342
        }
343
        match self.attrs.lookup(path).binary {
344
            Some(true) => true,
345
            Some(false) => is_binary(&e.content),
346
            None => is_binary(&e.content),
347
        }
348
    }
349
350
    /// Text content as stored: `\r\n` normalized for `eol=` files.
351
    fn stored_text(&self, path: &str, content: &[u8]) -> Vec<u8> {
352
        if self.attrs.lookup(path).eol.is_some() { crate::attributes::normalize_eol(content) } else { content.to_vec() }
353
    }
354
355
    fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> {
356
        self.reuse.get(path).filter(|(_, k)| *k == kind).map(|(n, _)| *n)
357
    }
358
359
    fn ensure_dir(&mut self, path: &str) -> NodeId {
360
        if let Some(n) = self.dir_nodes.get(path) {
361
            return *n;
362
        }
363
        let mut parent = NodeId::ROOT;
364
        let mut prefix = String::new();
365
        for comp in path.split('/') {
366
            if !prefix.is_empty() {
367
                prefix.push('/');
368
            }
369
            prefix.push_str(comp);
370
            parent = match self.dir_nodes.get(&prefix) {
371
                Some(n) => *n,
372
                None => {
373
                    let node = self.reused_node(&prefix, NodeKind::Dir).unwrap_or_else(NodeId::random);
374
                    self.tree_ops.push(TreeOp::AddNode { node, kind: NodeKind::Dir, mode: Mode::default(), parent, name: comp.to_string() });
375
                    self.dir_nodes.insert(prefix.clone(), node);
376
                    node
377
                }
378
            };
379
        }
380
        parent
381
    }
382
383
    fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> {
384
        let (dir, name) = split_path(path);
385
        let parent = self.ensure_dir(dir);
386
        let node = self.reused_node(path, e.kind).unwrap_or_else(NodeId::random);
387
        let opaque = self.opaque_for(path, e);
388
        let nocompress = self.attrs.lookup(path).nocompress;
389
        self.tree_ops.push(TreeOp::AddNode { node, kind: e.kind, mode: Mode { exec: e.exec, opaque }, parent, name: name.to_string() });
390
        if opaque {
391
            let blob = self.blob_of(repo, path, e, nocompress)?;
392
            self.edits.push(Edit::Replace { node, old: None, new: Some(blob) });
393
        } else if !e.content.is_empty() {
394
            let content = self.stored_text(path, &e.content);
395
            self.edits.push(Edit::Insert {
396
                node,
397
                after: crate::objects::AtomRef::Start,
398
                before: crate::objects::AtomRef::End,
399
                lines: split_lines(&content).iter().map(|l| l.to_vec()).collect(),
400
            });
401
        }
402
        self.statuses.push(PathStatus { path: path.to_string(), status: ChangeStatus::Added });
403
        Ok(())
404
    }
405
406
    /// Move the node at `old` to `new` and apply `e`'s content to it.
407
    fn move_path(&mut self, repo: &Repo, old: &str, new: &str, e: &FsEntry, expected: &crate::state::FileState) -> Result<()> {
408
        let entry = &self.base_snapshot.tree.entries[old];
409
        let (dir, name) = split_path(new);
410
        let parent = self.ensure_dir(dir);
411
        self.tree_ops.push(TreeOp::Move { node: expected.node, old: op_ref(entry.placement), parent, name: name.to_string() });
412
        self.update_path(repo, old, e, expected)?;
413
        // update_path reports under the old path; the rename is what the user sees.
414
        self.statuses.retain(|s| s.path != old);
415
        self.statuses.push(PathStatus { path: new.to_string(), status: ChangeStatus::Renamed });
416
        Ok(())
417
    }
418
419
    fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) {
420
        let entry = &self.base_snapshot.tree.entries[path];
421
        self.tree_ops.push(TreeOp::RemoveNode { node: expected.node, placement: op_ref(entry.placement) });
422
        if path.contains(".oxy-conflict-") || expected.is_conflicted() {
423
            self.resolves = true;
424
        }
425
        self.statuses.push(PathStatus { path: path.to_string(), status: ChangeStatus::Removed });
426
    }
427
428
    fn update_path(&mut self, repo: &Repo, path: &str, e: &FsEntry, expected: &crate::state::FileState) -> Result<()> {
429
        if expected.kind != e.kind {
430
            self.remove_path(path, expected);
431
            self.statuses.pop();
432
            self.add_path(repo, path, e)?;
433
            self.statuses.pop();
434
            self.statuses.push(PathStatus { path: path.to_string(), status: ChangeStatus::Modified });
435
            return Ok(());
436
        }
437
        let entry = &self.base_snapshot.tree.entries[path];
438
        let node = expected.node;
439
        let mode_atom = entry.mode_atom.map(op_ref);
440
        let mut status: Option<ChangeStatus> = None;
441
        let wants_opaque = self.opaque_for(path, e);
442
        let nocompress = self.attrs.lookup(path).nocompress;
443
        let disk_text = self.stored_text(path, &e.content);
444
        match &expected.content {
445
            FileContent::Dir => {}
446
            FileContent::Text { bytes, materialized } => {
447
                if materialized.is_conflicted() {
448
                    if bytes == &disk_text {
449
                        return Ok(());
450
                    }
451
                    if has_conflict_markers(&e.content) {
452
                        self.statuses.push(PathStatus { path: path.to_string(), status: ChangeStatus::ConflictEdited });
453
                        return Ok(());
454
                    }
455
                    // Resolution (spec §9.6): diff against the unconflicted lines
456
                    // with each region as a gap, then retire every region atom.
457
                    // Whatever the author kept from a region is re-inserted as
458
                    // new atoms, which leaves the graph totally ordered.
459
                    let regions = self.base.conflict_regions(&node);
460
                    let mut edits = text_edits(node, &regions.plain, &disk_text);
461
                    if !regions.region_atoms.is_empty() {
462
                        edits.insert(0, Edit::Delete { node, atoms: ranges(&regions.region_atoms) });
463
                    }
464
                    self.edits.extend(edits);
465
                    self.contributors.extend(regions.contributors);
466
                    self.resolves = true;
467
                    if e.exec != expected.mode.exec {
468
                        if let Some(old) = mode_atom {
469
                            self.tree_ops.push(TreeOp::SetMode { node, old, mode: Mode { exec: e.exec, opaque: false } });
470
                        }
471
                    }
472
                    self.statuses.push(PathStatus { path: path.to_string(), status: ChangeStatus::Resolved });
473
                    return Ok(());
474
                }
475
                if wants_opaque {
476
                    // Text → opaque (spec §7.5), by content or by attribute.
477
                    if let Some(old) = mode_atom {
478
                        self.tree_ops.push(TreeOp::SetMode { node, old, mode: Mode { exec: e.exec, opaque: true } });
479
                    }
480
                    let live: Vec<_> = repo_live_keys(self.base, &node);
481
                    if !live.is_empty() {
482
                        self.edits.push(Edit::Delete { node, atoms: ranges(&live) });
483
                    }
484
                    let blob = self.blob_of(repo, path, e, nocompress)?;
485
                    self.edits.push(Edit::Replace { node, old: None, new: Some(blob) });
486
                    status = Some(ChangeStatus::Modified);
487
                } else if bytes != &disk_text {
488
                    let old = self.base.live_lines(&node);
489
                    let edits = text_edits(node, &old, &disk_text);
490
                    if !edits.is_empty() {
491
                        self.edits.extend(edits);
492
                        status = Some(ChangeStatus::Modified);
493
                    }
494
                }
495
                let switched_to_opaque = wants_opaque;
496
                if !switched_to_opaque && e.exec != expected.mode.exec {
497
                    if let Some(old) = mode_atom {
498
                        self.tree_ops.push(TreeOp::SetMode { node, old, mode: Mode { exec: e.exec, opaque: false } });
499
                        status.get_or_insert(ChangeStatus::ModeChanged);
500
                    }
501
                }
502
            }
503
            FileContent::Opaque { blobs } => {
504
                let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content));
505
                if e.kind == NodeKind::File && wants_opaque {
506
                    // Unchanged or replaced, the file stays opaque: remember its blob.
507
                    self.blobs.insert(path.to_string(), new_ref);
508
                }
509
                if blobs.len() > 1 {
510
                    if !blobs.contains(&new_ref) {
511
                        self.statuses.push(PathStatus { path: path.to_string(), status: ChangeStatus::ConflictEdited });
512
                    }
513
                    return Ok(());
514
                }
515
                let current = blobs.first().copied();
516
                if current != Some(new_ref) {
517
                    if e.kind == NodeKind::File && !wants_opaque {
518
                        // Opaque → text (spec §7.5).
519
                        if let Some(old) = mode_atom {
520
                            self.tree_ops.push(TreeOp::SetMode { node, old, mode: Mode { exec: e.exec, opaque: false } });
521
                        }
522
                        self.edits.push(Edit::Replace { node, old: current, new: None });
523
                        if !e.content.is_empty() {
524
                            self.edits.push(Edit::Insert {
525
                                node,
526
                                after: crate::objects::AtomRef::Start,
527
                                before: crate::objects::AtomRef::End,
528
                                lines: split_lines(&e.content).iter().map(|l| l.to_vec()).collect(),
529
                            });
530
                        }
531
                    } else {
532
                        let blob = self.blob_of(repo, path, e, nocompress)?;
533
                        self.edits.push(Edit::Replace { node, old: current, new: Some(blob) });
534
                    }
535
                    status = Some(ChangeStatus::Modified);
536
                } else if e.kind == NodeKind::File && e.exec != expected.mode.exec {
537
                    if let Some(old) = mode_atom {
538
                        self.tree_ops.push(TreeOp::SetMode { node, old, mode: Mode { exec: e.exec, opaque: true } });
539
                        status = Some(ChangeStatus::ModeChanged);
540
                    }
541
                }
542
            }
543
        }
544
        if let Some(s) = status {
545
            self.statuses.push(PathStatus { path: path.to_string(), status: s });
546
        }
547
        Ok(())
548
    }
549
}
550
551
fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> {
552
    base.live_lines(node).into_iter().map(|(k, _)| k).collect()
553
}
554
555
/// The changes a change's scope references (spec §8.1): introducers of every
556
/// atom, placement, and mode it touches, and of every node it operates on.
557
pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> {
558
    let mut out = BTreeSet::new();
559
    let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| {
560
        if !r.change.is_zero() {
561
            out.insert(r.change);
562
        }
563
    };
564
    let mut nodes: Vec<NodeId> = Vec::new();
565
    for op in &change.tree_ops {
566
        match op {
567
            TreeOp::AddNode { parent, .. } => nodes.push(*parent),
568
            TreeOp::RemoveNode { node, placement } => {
569
                nodes.push(*node);
570
                opref(placement, &mut out);
571
            }
572
            TreeOp::Move { node, old, parent, .. } => {
573
                nodes.push(*node);
574
                nodes.push(*parent);
575
                opref(old, &mut out);
576
            }
577
            TreeOp::SetMode { node, old, .. } => {
578
                nodes.push(*node);
579
                opref(old, &mut out);
580
            }
581
        }
582
    }
583
    for e in &change.edits {
584
        nodes.push(e.node());
585
        match e {
586
            Edit::Insert { after, before, .. } => {
587
                for r in [after, before] {
588
                    if let crate::objects::AtomRef::Atom { change, .. } = r {
589
                        if !change.is_zero() {
590
                            out.insert(*change);
591
                        }
592
                    }
593
                }
594
            }
595
            Edit::Delete { atoms, .. } => {
596
                for r in atoms {
597
                    if !r.change.is_zero() {
598
                        out.insert(r.change);
599
                    }
600
                }
601
            }
602
            Edit::Replace { .. } => {}
603
        }
604
    }
605
    for n in nodes {
606
        if let Some(h) = base.tree().introducer(&n) {
607
            out.insert(h);
608
        }
609
    }
610
    out
611
}
612
613
/// Paths of the nodes a change adds on top of `base_snapshot`.
614
pub fn added_node_paths(change: &Change, base_snapshot: &Snapshot) -> HashMap<String, (NodeId, NodeKind)> {
615
    let by_node = base_snapshot.tree.by_node();
616
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
617
    for op in &change.tree_ops {
618
        if let TreeOp::AddNode { node, parent, name, kind, .. } = op {
619
            new_names.insert(*node, (*parent, name.clone(), *kind));
620
        }
621
    }
622
    let mut out = HashMap::new();
623
    for (node, (_, _, kind)) in &new_names {
624
        if let Some(path) = path_of_node(node, &by_node, &new_names) {
625
            out.insert(path, (*node, *kind));
626
        }
627
    }
628
    out
629
}
630
631
fn path_of_node(
632
    n: &NodeId,
633
    by_node: &HashMap<NodeId, &crate::tree::TreeEntry>,
634
    new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>,
635
) -> Option<String> {
636
    if let Some(e) = by_node.get(n) {
637
        return Some(e.path.clone());
638
    }
639
    let mut parts = Vec::new();
640
    let mut cur = *n;
641
    loop {
642
        if cur.is_root() {
643
            break;
644
        }
645
        if let Some(e) = by_node.get(&cur) {
646
            if !e.path.is_empty() {
647
                parts.push(e.path.clone());
648
            }
649
            break;
650
        }
651
        let (parent, name, _) = new_names.get(&cur)?;
652
        parts.push(name.clone());
653
        cur = *parent;
654
    }
655
    parts.reverse();
656
    Some(parts.join("/"))
657
}
658
659
/// Render a change's operations with line content, using `base` for deleted lines.
660
pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String {
661
    let by_node = base_snapshot.tree.by_node();
662
    let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new();
663
    for op in &change.tree_ops {
664
        if let TreeOp::AddNode { node, parent, name, kind, .. } = op {
665
            new_names.insert(*node, (*parent, name.clone(), *kind));
666
        }
667
    }
668
    let path_of = |n: &NodeId| -> String { path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8))) };
669
670
    let mut out = String::new();
671
    for op in &change.tree_ops {
672
        match op {
673
            TreeOp::AddNode { node, kind, mode, .. } => {
674
                out.push_str(&format!("A {} ({:?}{}{})\n", path_of(node), kind, if mode.exec { ", exec" } else { "" }, if mode.opaque { ", opaque" } else { "" }));
675
            }
676
            TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))),
677
            TreeOp::Move { node, name, .. } => out.push_str(&format!("R {} -> {}\n", path_of(node), name)),
678
            TreeOp::SetMode { node, mode, .. } => {
679
                out.push_str(&format!("M {} mode: exec={} opaque={}\n", path_of(node), mode.exec, mode.opaque));
680
            }
681
        }
682
    }
683
    let mut last_node: Option<NodeId> = None;
684
    for e in &change.edits {
685
        let n = e.node();
686
        if last_node != Some(n) {
687
            out.push_str(&format!("--- {}\n", path_of(&n)));
688
            last_node = Some(n);
689
        }
690
        match e {
691
            Edit::Insert { lines, .. } => {
692
                for l in lines {
693
                    out.push('+');
694
                    out.push_str(&String::from_utf8_lossy(l));
695
                    if !l.ends_with(b"\n") {
696
                        out.push('\n');
697
                    }
698
                }
699
            }
700
            Edit::Delete { atoms, .. } => {
701
                for r in atoms {
702
                    for i in r.from..r.to {
703
                        let content = base.file(&n).and_then(|g| g.content(&(r.change, i))).unwrap_or(b"<unknown>\n");
704
                        out.push('-');
705
                        out.push_str(&String::from_utf8_lossy(content));
706
                        if !content.ends_with(b"\n") {
707
                            out.push('\n');
708
                        }
709
                    }
710
                }
711
            }
712
            Edit::Replace { old, new, .. } => {
713
                out.push_str(&format!(
714
                    "  opaque content {} -> {}\n",
715
                    old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()),
716
                    new.map(|o| o.short(12)).unwrap_or_else(|| "none".into())
717
                ));
718
            }
719
        }
720
    }
721
    out
722
}
723
724
/// Pair removed files with added files of the same kind whose contents are
725
/// at least `RENAME_THRESHOLD` percent alike, best matches first. Beyond
726
/// `RENAME_LIMIT` candidate pairs only identical contents are paired.
727
const RENAME_THRESHOLD: u32 = 50;
728
const RENAME_LIMIT: usize = 10_000;
729
730
fn detect_renames(base_snapshot: &Snapshot, fs_entries: &BTreeMap<String, FsEntry>, in_scope: impl Fn(&str) -> bool) -> Vec<(String, String)> {
731
    let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot
732
        .files
733
        .iter()
734
        .filter(|(p, f)| f.kind != NodeKind::Dir && !fs_entries.contains_key(*p) && in_scope(p) && !f.is_conflicted())
735
        .collect();
736
    let added: Vec<(&String, &FsEntry)> = fs_entries.iter().filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p)).collect();
737
    if removed.is_empty() || added.is_empty() {
738
        return vec![];
739
    }
740
    let exact_only = removed.len() * added.len() > RENAME_LIMIT;
741
    let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> {
742
        match &f.content {
743
            FileContent::Text { bytes, .. } => Some(bytes.clone()),
744
            _ => None,
745
        }
746
    };
747
    let mut scored: Vec<(u32, usize, usize)> = Vec::new();
748
    for (ri, (_, f)) in removed.iter().enumerate() {
749
        let old = old_content(f);
750
        let old_blob = match &f.content {
751
            FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]),
752
            _ => None,
753
        };
754
        for (ai, (_, e)) in added.iter().enumerate() {
755
            if e.kind != f.kind {
756
                continue;
757
            }
758
            let score = match (&old, old_blob) {
759
                (Some(o), _) => {
760
                    if exact_only {
761
                        if *o == e.content { 100 } else { 0 }
762
                    } else {
763
                        similarity(o, &e.content)
764
                    }
765
                }
766
                (None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100,
767
                _ => 0,
768
            };
769
            if score >= RENAME_THRESHOLD {
770
                scored.push((score, ri, ai));
771
            }
772
        }
773
    }
774
    scored.sort_by(|a, b| b.0.cmp(&a.0).then(removed[a.1].0.cmp(removed[b.1].0)).then(added[a.2].0.cmp(added[b.2].0)));
775
    let mut used_r = vec![false; removed.len()];
776
    let mut used_a = vec![false; added.len()];
777
    let mut out = Vec::new();
778
    for (_, ri, ai) in scored {
779
        if used_r[ri] || used_a[ai] {
780
            continue;
781
        }
782
        used_r[ri] = true;
783
        used_a[ai] = true;
784
        out.push((removed[ri].0.clone(), added[ai].0.clone()));
785
    }
786
    out
787
}
788
789
/// Percentage of the larger file's bytes found in lines the two share
790
/// (multiset intersection of lines), like Git's similarity index.
791
pub fn similarity(a: &[u8], b: &[u8]) -> u32 {
792
    if a == b {
793
        return 100;
794
    }
795
    if a.is_empty() || b.is_empty() {
796
        return 0;
797
    }
798
    let mut counts: HashMap<&[u8], usize> = HashMap::new();
799
    for l in split_lines(a) {
800
        *counts.entry(l).or_default() += 1;
801
    }
802
    let mut shared = 0usize;
803
    for l in split_lines(b) {
804
        if let Some(n) = counts.get_mut(l) {
805
            if *n > 0 {
806
                *n -= 1;
807
                shared += l.len();
808
            }
809
        }
810
    }
811
    (shared * 100 / a.len().max(b.len())) as u32
812
}
813