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