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