| 98286406 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::*; |
| 957fee30 Andy McCall ↶ |
4 |
use crate::objects::AtomRef; |
| 98286406 Andy McCall ↶ |
5 |
|
| 6 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 7 |
pub enum ChangeStatus { |
| 8 |
Added, |
| 9 |
Modified, |
| 10 |
Removed, |
| 6e723b81 Andy McCall ↶ |
11 |
|
| 12 |
Renamed, |
| 98286406 Andy McCall ↶ |
13 |
ModeChanged, |
| 14 |
|
| 15 |
ConflictEdited, |
| 16 |
|
| 17 |
Resolved, |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
pub fn has_conflict_markers(content: &[u8]) -> bool { |
| 22 |
split_lines(content).iter().any(|l| { |
| 75167672 Andy McCall |
23 |
l.starts_with(b"<<<<<<< oxy conflict") |
| 24 |
|| l.starts_with(b"======= oxy conflict") |
| 25 |
|| l.starts_with(b">>>>>>> oxy conflict") |
| 98286406 Andy McCall ↶ |
26 |
}) |
| 27 |
} |
| 28 |
|
| 3652104a Andy McCall ↶ |
29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub fn has_own_conflict_markers(content: &[u8], materialized: &[u8], plain: &[&[u8]]) -> bool { |
| 35 |
let line = |l: &[u8]| -> Vec<u8> { |
| 36 |
let l = l.strip_suffix(b"\n").unwrap_or(l); |
| 37 |
l.strip_suffix(b"\r").unwrap_or(l).to_vec() |
| 38 |
}; |
| 39 |
let marker = |l: &[u8]| { |
| 40 |
l.starts_with(b"<<<<<<< oxy conflict") |
| 41 |
|| l.starts_with(b"======= oxy conflict") |
| 42 |
|| l.starts_with(b">>>>>>> oxy conflict") |
| 43 |
}; |
| 44 |
let unconflicted: HashSet<Vec<u8>> = plain.iter().map(|l| line(l)).collect(); |
| 45 |
let own: HashSet<Vec<u8>> = split_lines(materialized) |
| 46 |
.into_iter() |
| 47 |
.map(line) |
| 48 |
.filter(|l| marker(l) && !unconflicted.contains(l)) |
| 49 |
.collect(); |
| 50 |
!own.is_empty() |
| 51 |
&& split_lines(content) |
| 52 |
.into_iter() |
| 53 |
.any(|l| own.contains(&line(l))) |
| 54 |
} |
| 55 |
|
| 98286406 Andy McCall ↶ |
56 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 57 |
pub struct PathStatus { |
| 58 |
pub path: String, |
| 59 |
pub status: ChangeStatus, |
| 60 |
} |
| 61 |
|
| 614541c3 Andy McCall ↶ |
62 |
|
| 63 |
pub(crate) struct BuiltChange { |
| 64 |
pub tree_ops: Vec<TreeOp>, |
| 65 |
pub edits: Vec<Edit>, |
| 66 |
pub contributors: Vec<ChangeHash>, |
| 67 |
pub resolves: bool, |
| 68 |
pub statuses: Vec<PathStatus>, |
| b7f102fc Andy McCall ↶ |
69 |
|
| 70 |
pub blobs: HashMap<String, ObjectRef>, |
| 614541c3 Andy McCall ↶ |
71 |
} |
| 72 |
|
| 98286406 Andy McCall ↶ |
73 |
|
| 74 |
pub struct SnapshotResult { |
| 5b4c5130 Andy McCall ↶ |
75 |
|
| 76 |
|
| 98286406 Andy McCall ↶ |
77 |
pub wc_hash: ChangeHash, |
| 78 |
pub wc: Change, |
| 79 |
|
| 80 |
pub base: State, |
| 81 |
pub base_snapshot: Snapshot, |
| 82 |
pub statuses: Vec<PathStatus>, |
| 83 |
|
| 84 |
pub changed: bool, |
| 85 |
|
| 86 |
pub resolves: bool, |
| 87 |
|
| 88 |
pub contributors: Vec<ChangeHash>, |
| f9466021 Andy McCall ↶ |
89 |
|
| 90 |
pub(crate) attributes: crate::attributes::Attributes, |
| d4dbab97 Andy McCall ↶ |
91 |
|
| 92 |
|
| 93 |
pub untracked_nested: Vec<String>, |
| 236f35c0 Andy McCall ↶ |
94 |
|
| 95 |
|
| 96 |
pub(crate) manifest: Option<PendingManifest>, |
| 98286406 Andy McCall ↶ |
97 |
} |
| 98 |
|
| 236f35c0 Andy McCall ↶ |
99 |
|
| 100 |
|
| 101 |
pub(crate) struct PendingManifest { |
| 102 |
entries: BTreeMap<String, FsEntry>, |
| 103 |
blobs: HashMap<String, ObjectRef>, |
| 104 |
dirs: Option<super::WalkedDirs>, |
| 105 |
written: u128, |
| 106 |
} |
| 107 |
|
| 157d02e9 Andy McCall ↶ |
108 |
|
| 109 |
|
| 110 |
fn has_replace_conflict(base: &Snapshot, path: &str) -> bool { |
| 111 |
matches!(base.files.get(path).map(|f| &f.content), Some(FileContent::Opaque { blobs }) if blobs.len() > 1) |
| 112 |
} |
| 113 |
|
| 9bd98cfb Andy McCall ↶ |
114 |
|
| 115 |
#[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 116 |
pub struct KeptBoth { |
| 117 |
pub path: String, |
| 118 |
|
| 119 |
pub settled: Vec<usize>, |
| c0d4075c Andy McCall ↶ |
120 |
|
| 121 |
|
| 122 |
pub identical: Vec<usize>, |
| 9bd98cfb Andy McCall ↶ |
123 |
|
| 124 |
pub left: Vec<(usize, &'static str)>, |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
pub(crate) fn keep_both_text( |
| 131 |
text: &[u8], |
| 132 |
sides: &[Option<Vec<Vec<u8>>>], |
| 133 |
reverse: bool, |
| 134 |
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) { |
| c0d4075c Andy McCall ↶ |
135 |
let plans: Vec<Option<crate::state::Settle>> = sides |
| 136 |
.iter() |
| 137 |
.map(|s| { |
| 138 |
s.as_ref().map(|w| crate::state::Settle { |
| 139 |
sides: w.clone(), |
| 140 |
kept: w.clone(), |
| 141 |
identical: false, |
| 142 |
}) |
| 143 |
}) |
| 144 |
.collect(); |
| 145 |
settle_text(text, &plans, reverse) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
pub(crate) fn settle_text( |
| 154 |
text: &[u8], |
| 155 |
plans: &[Option<crate::state::Settle>], |
| 156 |
reverse: bool, |
| 157 |
) -> (Vec<u8>, Vec<usize>, Vec<(usize, &'static str)>) { |
| 9bd98cfb Andy McCall ↶ |
158 |
|
| 159 |
let with_newline = |s: &[u8]| { |
| 160 |
let mut s = s.to_vec(); |
| 161 |
if !s.is_empty() && !s.ends_with(b"\n") { |
| 162 |
s.push(b'\n'); |
| 163 |
} |
| 164 |
s |
| 165 |
}; |
| 166 |
let lines: Vec<&[u8]> = text.split_inclusive(|&b| b == b'\n').collect(); |
| 167 |
let (mut out, mut settled, mut left) = (Vec::new(), Vec::new(), Vec::new()); |
| 168 |
let mut i = 0; |
| 169 |
while i < lines.len() { |
| 170 |
let Some((n, total)) = conflict_header(lines[i]) else { |
| 171 |
out.extend_from_slice(lines[i]); |
| 172 |
i += 1; |
| 173 |
continue; |
| 174 |
}; |
| 175 |
let end = format!(">>>>>>> oxy conflict {n} of {total} end"); |
| 176 |
let next_side = format!("======= oxy conflict {n} of {total}, side "); |
| 177 |
let Some(stop) = |
| 178 |
(i + 1..lines.len()).find(|&j| lines[j].trim_ascii_end() == end.as_bytes()) |
| 179 |
else { |
| 180 |
left.push((n, "its end marker is missing; edit it by hand")); |
| 181 |
out.extend_from_slice(lines[i]); |
| 182 |
i += 1; |
| 183 |
continue; |
| 184 |
}; |
| 185 |
let mut found: Vec<Vec<u8>> = vec![Vec::new()]; |
| 186 |
for line in &lines[i + 1..stop] { |
| 187 |
if line.starts_with(next_side.as_bytes()) { |
| 188 |
found.push(Vec::new()); |
| 189 |
} else { |
| 190 |
found.last_mut().unwrap().extend_from_slice(line); |
| 191 |
} |
| 192 |
} |
| c0d4075c Andy McCall ↶ |
193 |
let wanted = match plans.get(n.wrapping_sub(1)) { |
| 194 |
_ if total != plans.len() => { |
| 9bd98cfb Andy McCall ↶ |
195 |
Err("the markers are not the file's conflicts now; see `oxy status`") |
| 196 |
} |
| 197 |
Some(Some(w)) => Ok(w), |
| 198 |
_ => Err("both sides changed these lines; edit the markers"), |
| 199 |
}; |
| 200 |
let keep = wanted.and_then(|w| { |
| c0d4075c Andy McCall ↶ |
201 |
let mut a: Vec<Vec<u8>> = w.sides.iter().map(|s| with_newline(s)).collect(); |
| 9bd98cfb Andy McCall ↶ |
202 |
let mut b = found.clone(); |
| 203 |
a.sort(); |
| 204 |
b.sort(); |
| 205 |
if a == b { |
| c0d4075c Andy McCall ↶ |
206 |
Ok(&w.kept) |
| 9bd98cfb Andy McCall ↶ |
207 |
} else { |
| 208 |
Err("the lines between its markers were edited; finish it by hand") |
| 209 |
} |
| 210 |
}); |
| 211 |
match keep { |
| 212 |
Ok(w) => { |
| 213 |
let order: Vec<&Vec<u8>> = if reverse { |
| 214 |
w.iter().rev().collect() |
| 215 |
} else { |
| 216 |
w.iter().collect() |
| 217 |
}; |
| 218 |
for s in order { |
| 219 |
out.extend_from_slice(&with_newline(s)); |
| 220 |
} |
| 221 |
settled.push(n); |
| 222 |
} |
| 223 |
Err(why) => { |
| 224 |
left.push((n, why)); |
| 225 |
for line in &lines[i..=stop] { |
| 226 |
out.extend_from_slice(line); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
i = stop + 1; |
| 231 |
} |
| 232 |
(out, settled, left) |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
fn conflict_header(line: &[u8]) -> Option<(usize, usize)> { |
| 237 |
let rest = line.strip_prefix(b"<<<<<<< oxy conflict ")?; |
| 238 |
let rest = std::str::from_utf8(rest).ok()?; |
| 239 |
let (n, rest) = rest.split_once(" of ")?; |
| 240 |
let total = rest.split(|c: char| !c.is_ascii_digit()).next()?; |
| 241 |
Some((n.parse().ok()?, total.parse().ok()?)) |
| 242 |
} |
| 243 |
|
| 98286406 Andy McCall ↶ |
244 |
pub fn is_binary(content: &[u8]) -> bool { |
| 245 |
content[..content.len().min(BINARY_SNIFF)].contains(&0) |
| 246 |
} |
| 247 |
|
| 248 |
impl Repo { |
| 614541c3 Andy McCall ↶ |
249 |
|
| 250 |
|
| 251 |
|
| 75167672 Andy McCall |
252 |
pub(crate) fn build_against( |
| 253 |
&self, |
| 254 |
base: &State, |
| 255 |
base_snapshot: &Snapshot, |
| 256 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 257 |
) -> Result<BuiltChange> { |
| c863c7a7 Andy McCall ↶ |
258 |
let mut fs_entries = |
| 696bbf29 Andy McCall ↶ |
259 |
self.walk_working_copy_with(None, &Self::tracked_kinds(base_snapshot), None)?; |
| c863c7a7 Andy McCall ↶ |
260 |
self.platform_normalize(&mut fs_entries, base_snapshot); |
| c3069537 Andy McCall ↶ |
261 |
self.build_from_entries(base, base_snapshot, reuse, &fs_entries) |
| 262 |
} |
| 263 |
|
| c863c7a7 Andy McCall ↶ |
264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
fn platform_normalize(&self, entries: &mut BTreeMap<String, FsEntry>, base: &Snapshot) { |
| 269 |
let p = self.platform; |
| 270 |
if p.exec_bits && p.symlinks { |
| 271 |
return; |
| 272 |
} |
| 273 |
for (path, e) in entries.iter_mut() { |
| 274 |
let Some(f) = base.files.get(path) else { |
| 275 |
continue; |
| 276 |
}; |
| 277 |
if !p.symlinks && f.kind == NodeKind::Symlink && e.kind == NodeKind::File { |
| 278 |
e.kind = NodeKind::Symlink; |
| 279 |
e.exec = false; |
| 280 |
} |
| 281 |
if !p.exec_bits && f.kind == NodeKind::File && e.kind == NodeKind::File { |
| 282 |
e.exec = f.mode.exec; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| c3069537 Andy McCall ↶ |
287 |
|
| 75167672 Andy McCall |
288 |
pub(crate) fn build_from_entries( |
| 289 |
&self, |
| 290 |
base: &State, |
| 291 |
base_snapshot: &Snapshot, |
| 292 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 293 |
fs_entries: &BTreeMap<String, FsEntry>, |
| 294 |
) -> Result<BuiltChange> { |
| f7510b63 Andy McCall ↶ |
295 |
self.build_from_entries_resolving(base, base_snapshot, reuse, fs_entries, &HashSet::new()) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
pub(crate) fn build_from_entries_resolving( |
| 304 |
&self, |
| 305 |
base: &State, |
| 306 |
base_snapshot: &Snapshot, |
| 307 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 308 |
fs_entries: &BTreeMap<String, FsEntry>, |
| 309 |
resolve: &HashSet<String>, |
| 310 |
) -> Result<BuiltChange> { |
| 23ff1288 Andy McCall ↶ |
311 |
self.build_from_entries_with( |
| 312 |
base, |
| 313 |
base_snapshot, |
| 314 |
reuse, |
| 315 |
fs_entries, |
| 316 |
resolve, |
| 317 |
Scope::default(), |
| 318 |
) |
| dfb23a3a Andy McCall ↶ |
319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 2306a4d2 Andy McCall ↶ |
324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| dfb23a3a Andy McCall ↶ |
329 |
pub(crate) fn build_from_entries_seeded( |
| 330 |
&self, |
| 331 |
base: &State, |
| 332 |
base_snapshot: &Snapshot, |
| 333 |
fs_entries: &BTreeMap<String, FsEntry>, |
| 334 |
seed: [u8; 32], |
| 2306a4d2 Andy McCall ↶ |
335 |
dir_needed: Option<&dyn Fn(&str) -> bool>, |
| dfb23a3a Andy McCall ↶ |
336 |
) -> Result<BuiltChange> { |
| 337 |
self.build_from_entries_with( |
| 338 |
base, |
| 339 |
base_snapshot, |
| 340 |
HashMap::new(), |
| 341 |
fs_entries, |
| 342 |
&HashSet::new(), |
| 23ff1288 Andy McCall ↶ |
343 |
Scope { |
| 344 |
seed: Some(seed), |
| 345 |
dir_needed, |
| 346 |
}, |
| dfb23a3a Andy McCall ↶ |
347 |
) |
| 348 |
} |
| 349 |
|
| 23ff1288 Andy McCall ↶ |
350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
fn build_changed_only( |
| 356 |
&self, |
| 357 |
base: &State, |
| 358 |
base_snapshot: &Snapshot, |
| 359 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 360 |
fs_entries: &BTreeMap<String, FsEntry>, |
| 361 |
) -> Result<BuiltChange> { |
| d616faf5 Andy McCall ↶ |
362 |
let pick = crate::timing::phase("snapshot: of which picking the changed"); |
| 23ff1288 Andy McCall ↶ |
363 |
|
| 364 |
|
| d616faf5 Andy McCall ↶ |
365 |
let changed: BTreeMap<String, FsEntry> = join(fs_entries, &base_snapshot.files) |
| 366 |
.filter_map(|(p, e, f)| { |
| 367 |
let e = e?; |
| 368 |
(!e.unchanged || f.is_none_or(|f| f.kind != e.kind || f.mode.exec != e.exec)) |
| 369 |
.then(|| (p.clone(), e.clone())) |
| 23ff1288 Andy McCall ↶ |
370 |
}) |
| 371 |
.collect(); |
| 372 |
let mut files: BTreeMap<String, crate::state::FileState> = BTreeMap::new(); |
| 373 |
for p in changed.keys() { |
| 374 |
if let Some(f) = base_snapshot.files.get(p) { |
| 375 |
files.insert(p.clone(), f.clone()); |
| 376 |
} |
| 377 |
} |
| d616faf5 Andy McCall ↶ |
378 |
|
| 379 |
let gone = join(&base_snapshot.files, fs_entries) |
| 380 |
.filter_map(|(p, f, e)| (e.is_none()).then_some((p, f?))); |
| 381 |
for (p, f) in gone { |
| 382 |
if f.kind == NodeKind::Dir { |
| 23ff1288 Andy McCall ↶ |
383 |
continue; |
| 384 |
} |
| 385 |
files.insert(p.clone(), f.clone()); |
| 386 |
let mut end = p.len(); |
| 387 |
while let Some(i) = p[..end].rfind('/') { |
| 388 |
let dir = &p[..i]; |
| 389 |
if files.contains_key(dir) { |
| 390 |
break; |
| 391 |
} |
| 392 |
if let Some(d) = base_snapshot.files.get(dir) { |
| 393 |
files.insert(dir.to_string(), d.clone()); |
| 394 |
} |
| 395 |
end = i; |
| 396 |
} |
| 397 |
} |
| 398 |
let part = Snapshot { |
| 399 |
tree: base_snapshot.tree.clone(), |
| 400 |
files, |
| 401 |
unrendered: BTreeSet::new(), |
| 402 |
}; |
| d616faf5 Andy McCall ↶ |
403 |
drop(pick); |
| 404 |
let _build = crate::timing::phase("snapshot: of which building the change"); |
| 23ff1288 Andy McCall ↶ |
405 |
|
| 406 |
let dir_needed = |dir: &str| { |
| 407 |
let prefix = format!("{dir}/"); |
| 408 |
fs_entries |
| 409 |
.range(prefix.clone()..) |
| 410 |
.next() |
| 411 |
.is_some_and(|(k, _)| k.starts_with(&prefix)) |
| 412 |
}; |
| 413 |
self.build_from_entries_with( |
| 414 |
base, |
| 415 |
&part, |
| 416 |
reuse, |
| 417 |
&changed, |
| 418 |
&HashSet::new(), |
| 419 |
Scope { |
| 420 |
seed: None, |
| 421 |
dir_needed: Some(&dir_needed), |
| 422 |
}, |
| 423 |
) |
| 424 |
} |
| 425 |
|
| dfb23a3a Andy McCall ↶ |
426 |
fn build_from_entries_with( |
| 427 |
&self, |
| 428 |
base: &State, |
| 429 |
base_snapshot: &Snapshot, |
| 430 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 431 |
fs_entries: &BTreeMap<String, FsEntry>, |
| 432 |
resolve: &HashSet<String>, |
| 23ff1288 Andy McCall ↶ |
433 |
scope: Scope, |
| dfb23a3a Andy McCall ↶ |
434 |
) -> Result<BuiltChange> { |
| 23ff1288 Andy McCall ↶ |
435 |
let Scope { seed, dir_needed } = scope; |
| 2306a4d2 Andy McCall ↶ |
436 |
let mut laps = crate::timing::Laps::start(); |
| cdb5beba Andy McCall ↶ |
437 |
|
| 438 |
|
| 439 |
let attrs = crate::attributes::Attributes::from_files( |
| 75167672 Andy McCall |
440 |
fs_entries |
| 441 |
.iter() |
| 442 |
.filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes")) |
| 443 |
.map(|(p, e)| (p.as_str(), e.content.as_slice())), |
| cdb5beba Andy McCall ↶ |
444 |
); |
| 445 |
let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs); |
| f7510b63 Andy McCall ↶ |
446 |
b.resolve = resolve.clone(); |
| dfb23a3a Andy McCall ↶ |
447 |
b.node_seed = seed; |
| 2306a4d2 Andy McCall ↶ |
448 |
laps.lap("build: builder"); |
| 6e723b81 Andy McCall ↶ |
449 |
|
| 450 |
|
| 451 |
|
| 452 |
let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p)); |
| 75167672 Andy McCall |
453 |
let renamed_from: HashMap<&str, &str> = renames |
| 454 |
.iter() |
| 455 |
.map(|(old, new)| (new.as_str(), old.as_str())) |
| 456 |
.collect(); |
| 457 |
let renamed_to: HashMap<&str, &str> = renames |
| 458 |
.iter() |
| 459 |
.map(|(old, new)| (old.as_str(), new.as_str())) |
| 460 |
.collect(); |
| 2306a4d2 Andy McCall ↶ |
461 |
laps.lap("build: renames"); |
| 360aa74a Andy McCall ↶ |
462 |
b.diff_in_parallel(fs_entries); |
| 463 |
laps.lap("build: diffs"); |
| c3069537 Andy McCall ↶ |
464 |
for (path, fs_entry) in fs_entries { |
| 98286406 Andy McCall ↶ |
465 |
match base_snapshot.files.get(path) { |
| 6e723b81 Andy McCall ↶ |
466 |
None => match renamed_from.get(path.as_str()) { |
| 75167672 Andy McCall |
467 |
Some(old) => { |
| 468 |
b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])? |
| 469 |
} |
| 6e723b81 Andy McCall ↶ |
470 |
None => b.add_path(self, path, fs_entry)?, |
| 471 |
}, |
| 98286406 Andy McCall ↶ |
472 |
Some(expected) => b.update_path(self, path, fs_entry, expected)?, |
| 473 |
} |
| 474 |
} |
| 2306a4d2 Andy McCall ↶ |
475 |
laps.lap("build: paths"); |
| 958fb27d Andy McCall ↶ |
476 |
|
| 477 |
|
| 478 |
let mut needed_dirs: HashSet<&str> = HashSet::new(); |
| 479 |
fn parents_of<'a>(p: &'a str, set: &mut HashSet<&'a str>) { |
| 480 |
let mut end = p.len(); |
| 481 |
while let Some(i) = p[..end].rfind('/') { |
| 482 |
if !set.insert(&p[..i]) { |
| 483 |
break; |
| 484 |
} |
| 485 |
end = i; |
| 486 |
} |
| 487 |
} |
| 488 |
for p in fs_entries.keys() { |
| 489 |
parents_of(p, &mut needed_dirs); |
| 490 |
} |
| 491 |
for p in base_snapshot.files.keys() { |
| 492 |
if !self.in_scope(p) { |
| 493 |
parents_of(p, &mut needed_dirs); |
| 494 |
} |
| 495 |
} |
| 98286406 Andy McCall ↶ |
496 |
for (path, expected) in &base_snapshot.files { |
| 75167672 Andy McCall |
497 |
if fs_entries.contains_key(path) |
| 498 |
|| !self.in_scope(path) |
| 499 |
|| renamed_to.contains_key(path.as_str()) |
| 500 |
{ |
| 98286406 Andy McCall ↶ |
501 |
continue; |
| 502 |
} |
| 2306a4d2 Andy McCall ↶ |
503 |
let still_needed = expected.kind == NodeKind::Dir |
| 504 |
&& match dir_needed { |
| 505 |
Some(f) => f(path), |
| 506 |
None => needed_dirs.contains(path.as_str()), |
| 507 |
}; |
| 98286406 Andy McCall ↶ |
508 |
if !still_needed { |
| 509 |
b.remove_path(path, expected); |
| 510 |
} |
| 511 |
} |
| 2306a4d2 Andy McCall ↶ |
512 |
laps.lap("build: removals"); |
| a87c7b9d Andy McCall ↶ |
513 |
if seed.is_some() { |
| 514 |
b.retire_stray_placements(); |
| 515 |
} |
| c2e1d090 Andy McCall |
516 |
b.retire_unified(); |
| 2306a4d2 Andy McCall ↶ |
517 |
laps.lap("build: stray placements"); |
| 614541c3 Andy McCall ↶ |
518 |
let mut statuses = b.statuses; |
| 519 |
statuses.sort_by(|a, b| a.path.cmp(&b.path)); |
| 75167672 Andy McCall |
520 |
Ok(BuiltChange { |
| 521 |
tree_ops: b.tree_ops, |
| 522 |
edits: b.edits, |
| 523 |
contributors: b.contributors.into_iter().collect(), |
| 524 |
resolves: b.resolves, |
| 525 |
statuses, |
| 526 |
blobs: b.blobs, |
| 527 |
}) |
| 614541c3 Andy McCall ↶ |
528 |
} |
| 98286406 Andy McCall ↶ |
529 |
|
| c07e0e7d Andy McCall ↶ |
530 |
|
| 531 |
|
| 532 |
|
| 614541c3 Andy McCall ↶ |
533 |
pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> { |
| c07e0e7d Andy McCall ↶ |
534 |
let ours = change.author == self.author(); |
| 535 |
if self.config.sign_changes && ours && self.secret_key()?.is_none() { |
| 614541c3 Andy McCall ↶ |
536 |
return fail("core.sign is \"change\" but no secret key is available for author.key"); |
| 537 |
} |
| 5b4c5130 Andy McCall ↶ |
538 |
let storing = crate::timing::phase("store: the change"); |
| 614541c3 Andy McCall ↶ |
539 |
let hash = self.store.put_change(change)?; |
| 5b4c5130 Andy McCall ↶ |
540 |
drop(storing); |
| c07e0e7d Andy McCall ↶ |
541 |
if self.config.sign_changes && ours { |
| 5b4c5130 Andy McCall ↶ |
542 |
let _t = crate::timing::phase("store: sign it"); |
| 614541c3 Andy McCall ↶ |
543 |
self.sign_change(&hash)?; |
| 544 |
} |
| 545 |
Ok(hash) |
| 157d02e9 Andy McCall ↶ |
546 |
} |
| 547 |
|
| 548 |
fn resolutions_path(&self) -> PathBuf { |
| 549 |
self.oxy |
| 550 |
.join("workspaces") |
| 551 |
.join(self.workspace.to_hex()) |
| 552 |
.join("resolve") |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
pub fn resolutions(&self) -> Result<BTreeSet<String>> { |
| 558 |
match fs::read_to_string(self.resolutions_path()) { |
| 559 |
Ok(text) => Ok(text |
| 560 |
.lines() |
| 561 |
.filter(|l| !l.is_empty()) |
| 562 |
.map(String::from) |
| 563 |
.collect()), |
| 564 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()), |
| 565 |
Err(e) => Err(e.into()), |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
pub fn choose_resolutions(&self, base: &Snapshot, paths: &[String]) -> Result<()> { |
| 574 |
for p in paths { |
| 575 |
if !has_replace_conflict(base, p) { |
| 576 |
return Err(RepoError::Msg(format!( |
| 9bd98cfb Andy McCall ↶ |
577 |
"{p} has no replace conflict to settle (a text file's conflict is settled by editing out its markers, or `oxy resolve --both {p}`)" |
| 157d02e9 Andy McCall ↶ |
578 |
))); |
| 579 |
} |
| 580 |
} |
| 581 |
let mut all = self.resolutions()?; |
| 582 |
all.extend(paths.iter().cloned()); |
| 583 |
let text: String = all.iter().map(|p| format!("{p}\n")).collect(); |
| 584 |
crate::durable::write_atomic(&self.resolutions_path(), text.as_bytes())?; |
| 585 |
Ok(()) |
| 9bd98cfb Andy McCall ↶ |
586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
pub fn keep_both( |
| 596 |
&self, |
| 597 |
snap: &SnapshotResult, |
| 598 |
paths: &[String], |
| 599 |
reverse: bool, |
| 600 |
) -> Result<Vec<KeptBoth>> { |
| 601 |
let paths: Vec<String> = if paths.is_empty() { |
| 602 |
snap.base_snapshot |
| 603 |
.files |
| 604 |
.iter() |
| 605 |
.filter(|(_, f)| matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted())) |
| 606 |
.map(|(p, _)| p.clone()) |
| 607 |
.collect() |
| 608 |
} else { |
| 609 |
paths.to_vec() |
| 610 |
}; |
| 611 |
let mut out = Vec::new(); |
| 612 |
for p in paths { |
| 613 |
let conflicted = match snap.base_snapshot.files.get(&p).map(|f| (f, &f.content)) { |
| 614 |
Some((f, FileContent::Text { materialized, .. })) |
| 615 |
if materialized.is_conflicted() => |
| 616 |
{ |
| 617 |
f |
| 618 |
} |
| 619 |
Some((_, FileContent::Opaque { blobs })) if blobs.len() > 1 => { |
| 620 |
return Err(RepoError::Msg(format!( |
| 621 |
"{p} has a replace conflict: keep one version with `oxy resolve {p}`" |
| 622 |
))) |
| 623 |
} |
| 624 |
_ => { |
| 625 |
return Err(RepoError::Msg(format!( |
| 626 |
"{p} has no text conflict to settle" |
| 627 |
))) |
| 628 |
} |
| 629 |
}; |
| 630 |
let sides = snap.base.both_sides(&conflicted.node); |
| 631 |
let disk = self.root().join(&p); |
| 632 |
let text = fs::read(&disk)?; |
| 633 |
let (bytes, settled, left) = keep_both_text(&text, &sides, reverse); |
| 634 |
if !settled.is_empty() { |
| 635 |
fs::write(&disk, bytes)?; |
| 636 |
} |
| 637 |
out.push(KeptBoth { |
| 638 |
path: p, |
| 639 |
settled, |
| c0d4075c Andy McCall ↶ |
640 |
identical: Vec::new(), |
| 9bd98cfb Andy McCall ↶ |
641 |
left, |
| 642 |
}); |
| 643 |
} |
| 644 |
Ok(out) |
| 157d02e9 Andy McCall ↶ |
645 |
} |
| 646 |
|
| c0d4075c Andy McCall ↶ |
647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
pub(crate) fn settle_conflicts(&self, command: &str) -> Result<Vec<KeptBoth>> { |
| 4afe288b Andy McCall ↶ |
654 |
let snap = self.snapshot(command)?; |
| c0d4075c Andy McCall ↶ |
655 |
let mut out = Vec::new(); |
| 656 |
for (p, f) in &snap.base_snapshot.files { |
| 657 |
if !matches!(&f.content, FileContent::Text { materialized, .. } if materialized.is_conflicted()) |
| 658 |
{ |
| 659 |
continue; |
| 660 |
} |
| 661 |
let plans = snap |
| 662 |
.base |
| 663 |
.settle_plans(&f.node, snap.attributes.lookup(p).union); |
| 664 |
if plans.iter().all(Option::is_none) { |
| 665 |
continue; |
| 666 |
} |
| 667 |
let disk = self.root().join(p); |
| 668 |
let Ok(text) = fs::read(&disk) else { continue }; |
| 669 |
let (bytes, settled, left) = settle_text(&text, &plans, false); |
| 670 |
if settled.is_empty() { |
| 671 |
continue; |
| 672 |
} |
| 673 |
fs::write(&disk, bytes)?; |
| 674 |
let identical = settled |
| 675 |
.iter() |
| 676 |
.copied() |
| 677 |
.filter(|n| plans[n - 1].as_ref().is_some_and(|w| w.identical)) |
| 678 |
.collect(); |
| 679 |
out.push(KeptBoth { |
| 680 |
path: p.clone(), |
| 681 |
settled, |
| 682 |
identical, |
| 683 |
left, |
| 684 |
}); |
| 4afe288b Andy McCall ↶ |
685 |
} |
| c0d4075c Andy McCall ↶ |
686 |
Ok(out) |
| 4afe288b Andy McCall ↶ |
687 |
} |
| 688 |
|
| c0d4075c Andy McCall ↶ |
689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| ac4ec124 Andy McCall ↶ |
697 |
pub fn union_resolution(state: &State, snap: &Snapshot) -> Option<Vec<(String, Vec<u8>)>> { |
| 698 |
if snap.tree.is_conflicted() { |
| 699 |
return None; |
| 700 |
} |
| 701 |
let attrs = Repo::state_attributes(snap); |
| 702 |
let mut out = Vec::new(); |
| 703 |
for (p, f) in &snap.files { |
| 704 |
if !f.is_conflicted() { |
| 705 |
continue; |
| 706 |
} |
| 707 |
let FileContent::Text { bytes, .. } = &f.content else { |
| 708 |
return None; |
| 709 |
}; |
| c0d4075c Andy McCall ↶ |
710 |
let plans = state.settle_plans(&f.node, attrs.lookup(p).union); |
| 711 |
let (text, _, left) = settle_text(bytes, &plans, false); |
| 712 |
if !left.is_empty() || plans.iter().any(Option::is_none) { |
| ac4ec124 Andy McCall ↶ |
713 |
return None; |
| 714 |
} |
| 715 |
out.push((p.clone(), text)); |
| 716 |
} |
| 717 |
(!out.is_empty()).then_some(out) |
| 718 |
} |
| 719 |
|
| 157d02e9 Andy McCall ↶ |
720 |
|
| 721 |
fn resolutions_for(&self, base: &Snapshot) -> Result<HashSet<String>> { |
| 722 |
let all = self.resolutions()?; |
| 723 |
if all.is_empty() { |
| 724 |
return Ok(HashSet::new()); |
| 725 |
} |
| 726 |
let live: HashSet<String> = all |
| 727 |
.iter() |
| 728 |
.filter(|p| has_replace_conflict(base, p)) |
| 729 |
.cloned() |
| 730 |
.collect(); |
| 731 |
if live.is_empty() { |
| 732 |
match fs::remove_file(self.resolutions_path()) { |
| 733 |
Ok(()) => {} |
| 734 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} |
| 735 |
Err(e) => return Err(e.into()), |
| 736 |
} |
| 737 |
} |
| 738 |
Ok(live) |
| 614541c3 Andy McCall ↶ |
739 |
} |
| 740 |
|
| 741 |
|
| cc7a0056 Andy McCall ↶ |
742 |
|
| 614541c3 Andy McCall ↶ |
743 |
pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> { |
| f9466021 Andy McCall ↶ |
744 |
self.snapshot_as(command, false, false) |
| cc7a0056 Andy McCall ↶ |
745 |
} |
| 746 |
|
| 747 |
|
| 8ab265dc Andy McCall ↶ |
748 |
|
| 749 |
|
| 750 |
|
| cc7a0056 Andy McCall ↶ |
751 |
|
| 752 |
|
| 8ab265dc Andy McCall ↶ |
753 |
|
| 754 |
|
| cc7a0056 Andy McCall ↶ |
755 |
pub fn snapshot_status(&self, command: &str) -> Result<SnapshotResult> { |
| f9466021 Andy McCall ↶ |
756 |
self.snapshot_as(command, true, false) |
| cc7a0056 Andy McCall ↶ |
757 |
} |
| 758 |
|
| cd4eb55e Andy McCall ↶ |
759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| f9466021 Andy McCall ↶ |
766 |
fn snapshot_as( |
| 767 |
&self, |
| 768 |
command: &str, |
| 769 |
partial: bool, |
| cd4eb55e Andy McCall ↶ |
770 |
for_commit: bool, |
| f9466021 Andy McCall ↶ |
771 |
) -> Result<SnapshotResult> { |
| 4e88e774 Andy McCall ↶ |
772 |
let _lock = self.lock_snapshot()?; |
| 8f10a07a Andy McCall ↶ |
773 |
let batch = self.store.begin_batch()?; |
| cd4eb55e Andy McCall ↶ |
774 |
let result = self.snapshot_locked(command, partial, for_commit); |
| 8f10a07a Andy McCall ↶ |
775 |
batch.finish()?; |
| 776 |
result |
| 777 |
} |
| 778 |
|
| f9466021 Andy McCall ↶ |
779 |
fn snapshot_locked( |
| 780 |
&self, |
| 781 |
command: &str, |
| 782 |
partial: bool, |
| cd4eb55e Andy McCall ↶ |
783 |
for_commit: bool, |
| f9466021 Andy McCall ↶ |
784 |
) -> Result<SnapshotResult> { |
| 0bdc3f91 Andy McCall ↶ |
785 |
let _all = crate::timing::phase("snapshot"); |
| 614541c3 Andy McCall ↶ |
786 |
let (op_ref, op) = self.head_op()?; |
| 787 |
let (old_hash, old_wc) = self.working_copy()?; |
| cc7a0056 Andy McCall ↶ |
788 |
|
| 789 |
|
| 790 |
let mut base = { |
| 791 |
let _t = crate::timing::phase("snapshot: load base history"); |
| f9575918 Andy McCall ↶ |
792 |
self.load_state_indexed(&old_wc.deps)? |
| 0bdc3f91 Andy McCall ↶ |
793 |
}; |
| d0d52107 Andy McCall ↶ |
794 |
let view = { |
| 795 |
let _t = crate::timing::phase("snapshot: tree view"); |
| c2e1d090 Andy McCall |
796 |
self.tree_view(&mut base)? |
| d0d52107 Andy McCall ↶ |
797 |
}; |
| 8b276e01 Andy McCall ↶ |
798 |
let manifest = { |
| 799 |
let _t = crate::timing::phase("snapshot: read manifest"); |
| 800 |
self.read_manifest()?.map(|(_, _, m)| m) |
| 801 |
}; |
| cc7a0056 Andy McCall ↶ |
802 |
let tracked: BTreeMap<String, (NodeKind, Option<ObjectRef>)> = { |
| 803 |
let _t = crate::timing::phase("snapshot: tracked paths"); |
| 36e51aa8 Andy McCall ↶ |
804 |
|
| 805 |
view.entries |
| 806 |
.iter() |
| 807 |
.filter(|(_, e)| e.kind != NodeKind::Dir) |
| 808 |
.map(|(p, e)| { |
| 809 |
let blob = if e.kind != NodeKind::File || e.mode.opaque { |
| 810 |
base.live_blobs(&e.node).first().copied() |
| 811 |
} else { |
| 812 |
None |
| 813 |
}; |
| 814 |
(p.clone(), (e.kind, blob)) |
| 815 |
}) |
| 816 |
.collect() |
| cc7a0056 Andy McCall ↶ |
817 |
}; |
| 992467d3 Andy McCall ↶ |
818 |
|
| 1966f5db Andy McCall ↶ |
819 |
let key_of = |p: &str| -> Option<[u8; 32]> { |
| 992467d3 Andy McCall ↶ |
820 |
let e = view.entries.get(p)?; |
| 821 |
if e.kind != NodeKind::File || e.mode.opaque { |
| 822 |
return None; |
| 823 |
} |
| 1966f5db Andy McCall ↶ |
824 |
base.graph_key(&e.node) |
| 992467d3 Andy McCall ↶ |
825 |
}; |
| cc7a0056 Andy McCall ↶ |
826 |
let mut fs_entries = { |
| 827 |
let _t = crate::timing::phase("snapshot: read working copy"); |
| 992467d3 Andy McCall ↶ |
828 |
self.walk_working_copy_with(manifest.as_deref(), &tracked, Some(&key_of))? |
| cc7a0056 Andy McCall ↶ |
829 |
}; |
| 992467d3 Andy McCall ↶ |
830 |
|
| 831 |
|
| 1966f5db Andy McCall ↶ |
832 |
let keys: HashMap<String, [u8; 32]> = fs_entries |
| 992467d3 Andy McCall ↶ |
833 |
.iter() |
| 834 |
.filter(|(_, e)| !e.unchanged && e.kind == NodeKind::File) |
| 835 |
.filter_map(|(p, _)| Some((p.clone(), key_of(p)?))) |
| 836 |
.collect(); |
| 0bdc3f91 Andy McCall ↶ |
837 |
let base_snapshot = { |
| cc7a0056 Andy McCall ↶ |
838 |
let _t = crate::timing::phase("snapshot: load and render base"); |
| 839 |
if partial { |
| d616faf5 Andy McCall ↶ |
840 |
let sets = crate::timing::phase("snapshot: of which choosing graphs"); |
| 841 |
|
| 842 |
let need: Vec<(&str, NodeId)> = join(&view.entries, &fs_entries) |
| 843 |
.filter_map(|(p, v, f)| { |
| 844 |
let v = v?; |
| 845 |
(v.kind == NodeKind::File && !f.is_some_and(|f| f.unchanged)) |
| 846 |
.then_some((p.as_str(), v.node)) |
| 847 |
}) |
| cc7a0056 Andy McCall ↶ |
848 |
.collect(); |
| d616faf5 Andy McCall ↶ |
849 |
let render: std::collections::HashSet<&str> = |
| 850 |
need.iter().map(|(p, _)| *p).collect(); |
| 851 |
let need: Vec<NodeId> = need.iter().map(|(_, n)| *n).collect(); |
| 852 |
drop(sets); |
| d0990c2b Andy McCall ↶ |
853 |
{ |
| 854 |
let _t = crate::timing::phase("snapshot: of which building graphs"); |
| 855 |
self.load_graphs_of(&mut base, &need)?; |
| 856 |
} |
| 857 |
let _t = crate::timing::phase("snapshot: of which rendering"); |
| d616faf5 Andy McCall ↶ |
858 |
base.materialize_only(&render) |
| cc7a0056 Andy McCall ↶ |
859 |
} else { |
| 860 |
self.load_all_graphs(&mut base)?; |
| 861 |
base.materialize() |
| 862 |
} |
| 0bdc3f91 Andy McCall ↶ |
863 |
}; |
| 614541c3 Andy McCall ↶ |
864 |
|
| 865 |
|
| 866 |
|
| 867 |
let reuse = added_node_paths(&old_wc, &base_snapshot); |
| c863c7a7 Andy McCall ↶ |
868 |
self.platform_normalize(&mut fs_entries, &base_snapshot); |
| 157d02e9 Andy McCall ↶ |
869 |
|
| 870 |
|
| 871 |
|
| 872 |
let resolve = self.resolutions_for(&base_snapshot)?; |
| 0bdc3f91 Andy McCall ↶ |
873 |
let built = { |
| 874 |
let _t = crate::timing::phase("snapshot: diff against base"); |
| 157d02e9 Andy McCall ↶ |
875 |
if !resolve.is_empty() { |
| 876 |
self.build_from_entries_resolving( |
| 877 |
&base, |
| 878 |
&base_snapshot, |
| 879 |
reuse, |
| 880 |
&fs_entries, |
| 881 |
&resolve, |
| 882 |
)? |
| 883 |
} else if partial && self.config.sparse_paths.is_empty() { |
| 23ff1288 Andy McCall ↶ |
884 |
#[cfg(test)] |
| 885 |
let full = |
| 886 |
self.build_from_entries(&base, &base_snapshot, reuse.clone(), &fs_entries)?; |
| 887 |
let built = self.build_changed_only(&base, &base_snapshot, reuse, &fs_entries)?; |
| 888 |
#[cfg(test)] |
| 889 |
{ |
| 890 |
|
| 891 |
|
| 892 |
let norm = |b: &BuiltChange| { |
| 893 |
let text = format!("{:?} {:?} {:?}", b.tree_ops, b.edits, b.statuses); |
| 894 |
let mut seen: Vec<String> = Vec::new(); |
| 895 |
let mut out = String::new(); |
| 896 |
let mut rest = text.as_str(); |
| 897 |
while let Some(i) = rest.find("NodeId(") { |
| 898 |
out.push_str(&rest[..i]); |
| 899 |
let end = i + rest[i..].find(')').unwrap() + 1; |
| 900 |
let id = rest[i..end].to_string(); |
| 901 |
let n = seen.iter().position(|x| *x == id).unwrap_or_else(|| { |
| 902 |
seen.push(id); |
| 903 |
seen.len() - 1 |
| 904 |
}); |
| 905 |
out.push_str(&format!("N{n}")); |
| 906 |
rest = &rest[end..]; |
| 907 |
} |
| 908 |
out.push_str(rest); |
| 909 |
out |
| 910 |
}; |
| 911 |
assert_eq!( |
| 912 |
norm(&built), |
| 913 |
norm(&full), |
| 914 |
"a changed-only build is the full one" |
| 915 |
); |
| 916 |
} |
| 917 |
built |
| 918 |
} else { |
| 919 |
self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)? |
| 920 |
} |
| 0bdc3f91 Andy McCall ↶ |
921 |
}; |
| cc7a0056 Andy McCall ↶ |
922 |
|
| 923 |
|
| 924 |
{ |
| 925 |
let changed: HashSet<&str> = built.statuses.iter().map(|s| s.path.as_str()).collect(); |
| 926 |
for (p, e) in fs_entries.iter_mut() { |
| 927 |
if e.unchanged || e.kind != NodeKind::File || changed.contains(p.as_str()) { |
| 928 |
continue; |
| 929 |
} |
| 930 |
e.graph_key = match base_snapshot.files.get(p).map(|f| &f.content) { |
| 931 |
Some(FileContent::Text { |
| 932 |
bytes, |
| 933 |
materialized, |
| 40a40bb7 Andy McCall ↶ |
934 |
}) if !materialized.is_conflicted() && *bytes == *e.content => { |
| 1966f5db Andy McCall ↶ |
935 |
keys.get(p).copied() |
| cc7a0056 Andy McCall ↶ |
936 |
} |
| 937 |
_ => None, |
| 938 |
}; |
| 939 |
} |
| 940 |
} |
| 614541c3 Andy McCall ↶ |
941 |
|
| 98286406 Andy McCall ↶ |
942 |
let mut wc = Change { |
| 6f8aeb99 Andy McCall ↶ |
943 |
version: crate::objects::CHANGE_VERSION, |
| 98286406 Andy McCall ↶ |
944 |
change_id: old_wc.change_id, |
| 945 |
deps: vec![], |
| 946 |
author: self.author(), |
| 947 |
hlc: old_wc.hlc, |
| 948 |
message: old_wc.message.clone(), |
| 614541c3 Andy McCall ↶ |
949 |
tree_ops: built.tree_ops, |
| 950 |
edits: built.edits, |
| 98286406 Andy McCall ↶ |
951 |
kind: ChangeKind::WorkingCopy, |
| 952 |
}; |
| 953 |
let base_heads = base.maximal(&old_wc.deps); |
| 954 |
let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect(); |
| 955 |
deps.extend(referenced_changes(&wc, &base)); |
| 614541c3 Andy McCall ↶ |
956 |
deps.extend(built.contributors.iter().copied()); |
| 98286406 Andy McCall ↶ |
957 |
wc.deps = deps.into_iter().collect(); |
| 958 |
wc.normalize(); |
| 614541c3 Andy McCall ↶ |
959 |
let resolves = built.resolves; |
| 960 |
let contributors = built.contributors; |
| 961 |
let statuses = built.statuses; |
| 98286406 Andy McCall ↶ |
962 |
|
| 8b276e01 Andy McCall ↶ |
963 |
let record = crate::timing::phase("snapshot: hash and record"); |
| 17f94b13 Andy McCall ↶ |
964 |
|
| 965 |
|
| 966 |
|
| 5b4c5130 Andy McCall ↶ |
967 |
|
| 968 |
|
| 969 |
|
| 970 |
let (new_hash, changed, encoded) = if for_commit { |
| 971 |
(old_hash, true, Vec::new()) |
| 972 |
} else { |
| 973 |
let encoded = wc.encode_streaming(|batch| self.store.put_texts(&batch))?; |
| 974 |
let new_hash = crate::hash::hash_object(crate::hash::ObjectType::Change, &encoded); |
| 975 |
(new_hash, new_hash != old_hash, encoded) |
| 976 |
}; |
| cd4eb55e Andy McCall ↶ |
977 |
if changed && !for_commit { |
| 17f94b13 Andy McCall ↶ |
978 |
self.store.put(crate::hash::ObjectType::Change, &encoded)?; |
| 98286406 Andy McCall ↶ |
979 |
let mut view = op.view.clone(); |
| 980 |
view.workspaces.insert(self.workspace, new_hash); |
| 981 |
self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?; |
| 982 |
} |
| 8b276e01 Andy McCall ↶ |
983 |
drop(record); |
| 135212de Andy McCall ↶ |
984 |
|
| 0bdc3f91 Andy McCall ↶ |
985 |
let _t = crate::timing::phase("snapshot: write manifest"); |
| 992467d3 Andy McCall ↶ |
986 |
let conflicted = base_snapshot.is_conflicted(); |
| 987 |
let uncommitted = !wc.tree_ops.is_empty() || !wc.edits.is_empty(); |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
let worth = match self.read_manifest()? { |
| cd4eb55e Andy McCall ↶ |
993 |
_ if for_commit => true, |
| 992467d3 Andy McCall ↶ |
994 |
None => true, |
| 995 |
Some((c, u, m)) => { |
| 996 |
c != conflicted |
| 997 |
|| u != uncommitted |
| 998 |
|| m.entries.len() != fs_entries.len() |
| 999 |
|| fs_entries.iter().any(|(p, e)| { |
| 1000 |
!e.unchanged && (e.graph_key.is_some() || !m.entries.contains_key(p)) |
| 1001 |
}) |
| 1002 |
|| built |
| 1003 |
.blobs |
| 1004 |
.iter() |
| 1005 |
.any(|(p, b)| m.entries.get(p).and_then(|e| e.blob) != Some(*b)) |
| 36041796 Andy McCall ↶ |
1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|| (uncommitted && statuses.iter().any(|st| { |
| 1010 |
let Some(e) = fs_entries.get(&st.path) else { |
| 1011 |
return m.entries.contains_key(&st.path); |
| 1012 |
}; |
| 1013 |
let Some(me) = m.entries.get(&st.path) else { |
| 1014 |
return true; |
| 1015 |
}; |
| 1016 |
let hash = e |
| 1017 |
.content_hash |
| 1018 |
.unwrap_or_else(|| *blake3::hash(&e.content).as_bytes()); |
| 1019 |
let stat = e.stat.or_else(|| { |
| 1020 |
fs::symlink_metadata(self.root.join(&st.path)) |
| 1021 |
.ok() |
| 1022 |
.map(|md| (md.len(), super::prompt::mtime_ns(&md))) |
| 1023 |
}); |
| e226a6cc Andy McCall ↶ |
1024 |
|
| 1025 |
|
| 1026 |
me.hash != hash || me.exec != e.exec || stat != Some((me.size, me.mtime_ns)) |
| 36041796 Andy McCall ↶ |
1027 |
})) |
| 992467d3 Andy McCall ↶ |
1028 |
} |
| 1029 |
}; |
| f9466021 Andy McCall ↶ |
1030 |
let attributes = crate::attributes::Attributes::from_files( |
| 1031 |
fs_entries |
| 1032 |
.iter() |
| 1033 |
.filter(|(p, _)| is_attributes(p)) |
| 1034 |
.map(|(p, e)| (p.as_str(), e.content.as_slice())), |
| 1035 |
); |
| 236f35c0 Andy McCall ↶ |
1036 |
let mut manifest = None; |
| 1037 |
if for_commit { |
| 1038 |
manifest = Some(PendingManifest { |
| 1039 |
entries: fs_entries, |
| 1040 |
blobs: built.blobs.clone(), |
| 1041 |
dirs: self.walked_dirs.take(), |
| 1042 |
written: super::prompt::now_ns(), |
| 1043 |
}); |
| 1044 |
} else { |
| 1045 |
if worth { |
| 1046 |
self.write_manifest_entries(conflicted, uncommitted, &fs_entries, &built.blobs)?; |
| 1047 |
} |
| 1048 |
self.write_status_cache((uncommitted && !conflicted).then_some(&new_hash), &statuses); |
| 1049 |
} |
| 75167672 Andy McCall |
1050 |
Ok(SnapshotResult { |
| 1051 |
wc_hash: new_hash, |
| 1052 |
wc, |
| 1053 |
base, |
| 1054 |
base_snapshot, |
| 1055 |
statuses, |
| 1056 |
changed, |
| 1057 |
resolves, |
| 1058 |
contributors, |
| f9466021 Andy McCall ↶ |
1059 |
attributes, |
| d4dbab97 Andy McCall ↶ |
1060 |
untracked_nested: self.untracked_nested.borrow().clone(), |
| 236f35c0 Andy McCall ↶ |
1061 |
manifest, |
| 75167672 Andy McCall |
1062 |
}) |
| 98286406 Andy McCall ↶ |
1063 |
} |
| 1064 |
|
| 50a046e4 Andy McCall ↶ |
1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
pub fn status_json(&self, snap: &SnapshotResult) -> Result<String> { |
| 1069 |
let heads = self.bookmark_heads()?; |
| 1070 |
let base_snapshot = &snap.base_snapshot; |
| 1071 |
let by_node = base_snapshot.tree.by_node(); |
| 1072 |
let path_of = |n: &NodeId| by_node.get(n).map(|e| e.path.clone()); |
| 75167672 Andy McCall |
1073 |
let id_of = |h: &ChangeHash| { |
| 1074 |
snap.base |
| 1075 |
.get(h) |
| 1076 |
.map(|m| m.change_id.to_hex()) |
| 1077 |
.unwrap_or_else(|| h.to_hex()) |
| 1078 |
}; |
| 1079 |
let paths = json_array(snap.statuses.iter().map(|s| { |
| 1080 |
format!( |
| 1081 |
"{{\"path\":{},\"status\":{}}}", |
| 1082 |
json_str(&s.path), |
| 1083 |
json_str(&format!("{:?}", s.status)) |
| 1084 |
) |
| 1085 |
})); |
| 50a046e4 Andy McCall ↶ |
1086 |
let mut conflicts: Vec<String> = Vec::new(); |
| 1087 |
for (p, f) in &base_snapshot.files { |
| 1088 |
if !f.is_conflicted() { |
| 1089 |
continue; |
| 1090 |
} |
| 1091 |
let (kind, changes) = match &f.content { |
| 75167672 Andy McCall |
1092 |
FileContent::Text { .. } => { |
| 1093 |
("text", snap.base.conflict_regions(&f.node).contributors) |
| 1094 |
} |
| 1095 |
FileContent::Opaque { blobs } => { |
| 1096 |
("replace", snap.base.blob_contributors(&f.node, blobs)) |
| 1097 |
} |
| 50a046e4 Andy McCall ↶ |
1098 |
FileContent::Dir => continue, |
| 1099 |
}; |
| 75167672 Andy McCall |
1100 |
let resolved = match snap |
| 1101 |
.statuses |
| 1102 |
.iter() |
| 1103 |
.find(|s| s.path == *p) |
| 1104 |
.map(|s| s.status) |
| 1105 |
{ |
| 50a046e4 Andy McCall ↶ |
1106 |
Some(ChangeStatus::Resolved) => "true", |
| 1107 |
Some(ChangeStatus::ConflictEdited) => "\"markers\"", |
| 1108 |
_ => "false", |
| 1109 |
}; |
| 1110 |
conflicts.push(format!( |
| 1111 |
"{{\"kind\":{},\"path\":{},\"node\":{},\"changes\":{},\"resolved\":{resolved}}}", |
| 1112 |
json_str(kind), |
| 1113 |
json_str(p), |
| 1114 |
json_str(&f.node.to_hex()), |
| 1115 |
json_array(changes.iter().map(|h| json_str(&id_of(h)))) |
| 1116 |
)); |
| 1117 |
} |
| 1118 |
for c in &base_snapshot.tree.conflicts { |
| 1119 |
let (kind, nodes): (&str, Vec<NodeId>) = match c { |
| 1120 |
crate::tree::TreeConflict::Name { nodes, .. } => ("name", nodes.clone()), |
| 75167672 Andy McCall |
1121 |
crate::tree::TreeConflict::MultiPlacement { node, .. } => { |
| 1122 |
("multi-placement", vec![*node]) |
| 1123 |
} |
| 50a046e4 Andy McCall ↶ |
1124 |
crate::tree::TreeConflict::Orphan { node, .. } => ("orphan", vec![*node]), |
| 1125 |
crate::tree::TreeConflict::Cycle { node } => ("cycle", vec![*node]), |
| 1126 |
crate::tree::TreeConflict::Mode { node, .. } => ("mode", vec![*node]), |
| 1127 |
}; |
| 1128 |
let detail = match c { |
| 1129 |
crate::tree::TreeConflict::Name { name, parent, .. } => { |
| 75167672 Andy McCall |
1130 |
let dir = if *parent == NodeId::ROOT { |
| 1131 |
String::new() |
| 1132 |
} else { |
| 1133 |
path_of(parent).unwrap_or_default() |
| 1134 |
}; |
| 1135 |
format!( |
| 1136 |
",\"path\":{}", |
| 1137 |
json_str(&if dir.is_empty() { |
| 1138 |
name.clone() |
| 1139 |
} else { |
| 1140 |
format!("{dir}/{name}") |
| 1141 |
}) |
| 1142 |
) |
| 50a046e4 Andy McCall ↶ |
1143 |
} |
| 75167672 Andy McCall |
1144 |
_ => nodes |
| 1145 |
.first() |
| 1146 |
.and_then(path_of) |
| 1147 |
.map(|p| format!(",\"path\":{}", json_str(&p))) |
| 1148 |
.unwrap_or_default(), |
| 50a046e4 Andy McCall ↶ |
1149 |
}; |
| 1150 |
conflicts.push(format!( |
| 1151 |
"{{\"kind\":{},\"nodes\":{}{detail}}}", |
| 1152 |
json_str(kind), |
| 1153 |
json_array(nodes.iter().map(|n| json_str(&n.to_hex()))) |
| 1154 |
)); |
| 1155 |
} |
| 1156 |
Ok(format!( |
| b689779e Andy McCall ↶ |
1157 |
"{{\"bookmark\":{},\"working_copy\":{},\"heads\":{},\"clean\":{},\"conflicted\":{},\"paths\":{paths},\"conflicts\":{},\"untracked_nested\":{},\"skipped_large\":{}}}", |
| 50a046e4 Andy McCall ↶ |
1158 |
json_str(&self.config.bookmark), |
| 1159 |
json_str(&snap.wc.change_id.to_hex()), |
| 1160 |
heads.len(), |
| 1161 |
snap.statuses.is_empty(), |
| 1162 |
base_snapshot.is_conflicted(), |
| d4dbab97 Andy McCall ↶ |
1163 |
json_array(conflicts), |
| b689779e Andy McCall ↶ |
1164 |
json_array(snap.untracked_nested.iter().map(|p| json_str(p))), |
| 1165 |
json_array( |
| 1166 |
self.skipped_large |
| 1167 |
.borrow() |
| 1168 |
.iter() |
| 1169 |
.map(|(p, n)| format!("{{\"path\":{},\"size\":{n}}}", json_str(p))) |
| 1170 |
) |
| 50a046e4 Andy McCall ↶ |
1171 |
)) |
| 1172 |
} |
| 1173 |
|
| 98286406 Andy McCall ↶ |
1174 |
|
| 1175 |
pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> { |
| 957fee30 Andy McCall ↶ |
1176 |
self.commit_selection(message, extra_deps, &Selection::All) |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
pub fn hunks(&self) -> Result<(SnapshotResult, Vec<Hunk>)> { |
| 8ab265dc Andy McCall ↶ |
1182 |
let snap = self.snapshot_status("oxy commit")?; |
| 957fee30 Andy McCall ↶ |
1183 |
let hunks = render_hunks(&snap.wc, &snap.base, &snap.base_snapshot); |
| 1184 |
Ok((snap, hunks)) |
| 1185 |
} |
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| f9466021 Andy McCall ↶ |
1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
fn committed_graph_keys( |
| 1198 |
&self, |
| 1199 |
st: &State, |
| 1200 |
paths: &[String], |
| 1201 |
attrs: &crate::attributes::Attributes, |
| 1202 |
) -> HashMap<String, [u8; 32]> { |
| 86e1824f Andy McCall ↶ |
1203 |
let _t = crate::timing::phase("commit: graph keys"); |
| c2e1d090 Andy McCall |
1204 |
let view = st.view(); |
| f9466021 Andy McCall ↶ |
1205 |
paths |
| 86e1824f Andy McCall ↶ |
1206 |
.iter() |
| f9466021 Andy McCall ↶ |
1207 |
.filter_map(|p| { |
| 1208 |
let e = view.entries.get(p)?; |
| 1209 |
(e.kind == NodeKind::File && !e.mode.opaque && attrs.lookup(p).eol.is_none()) |
| 1210 |
.then_some(())?; |
| 1211 |
Some((p.clone(), st.graph_key(&e.node)?)) |
| 86e1824f Andy McCall ↶ |
1212 |
}) |
| 8457a6cc Andy McCall ↶ |
1213 |
.collect() |
| 86e1824f Andy McCall ↶ |
1214 |
} |
| 1215 |
|
| 75167672 Andy McCall |
1216 |
pub fn commit_selection( |
| 1217 |
&self, |
| 1218 |
message: &str, |
| 1219 |
extra_deps: &[ChangeHash], |
| 1220 |
selection: &Selection, |
| 1221 |
) -> Result<ChangeHash> { |
| 75051941 Andy McCall ↶ |
1222 |
Ok(self |
| 1223 |
.commit_selection_partial(message, extra_deps, selection)? |
| 1224 |
.0) |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
pub fn commit_selection_partial( |
| 1231 |
&self, |
| 1232 |
message: &str, |
| 1233 |
extra_deps: &[ChangeHash], |
| 1234 |
selection: &Selection, |
| 1235 |
) -> Result<(ChangeHash, bool)> { |
| b30a880f Andy McCall ↶ |
1236 |
|
| 1237 |
|
| 1238 |
let batch = self.store.begin_batch()?; |
| 1239 |
let out = self.commit_selection_batched(message, extra_deps, selection); |
| 1240 |
batch.finish()?; |
| 1241 |
out |
| 1242 |
} |
| 1243 |
|
| 236f35c0 Andy McCall ↶ |
1244 |
|
| 1245 |
|
| 1246 |
fn write_pending_manifest( |
| 1247 |
&self, |
| 1248 |
mut m: PendingManifest, |
| 1249 |
conflicted: bool, |
| 1250 |
keys: HashMap<String, [u8; 32]>, |
| 1251 |
) -> Result<()> { |
| 1252 |
for (path, k) in keys { |
| 1253 |
if let Some(e) = m.entries.get_mut(&path) { |
| 1254 |
e.graph_key = Some(k); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
self.write_manifest_entries_as_of( |
| 1258 |
conflicted, |
| 1259 |
false, |
| 1260 |
&m.entries, |
| 1261 |
&m.blobs, |
| 1262 |
m.written, |
| 1263 |
m.dirs.unwrap_or_default(), |
| 1264 |
) |
| 1265 |
} |
| 1266 |
|
| b30a880f Andy McCall ↶ |
1267 |
fn commit_selection_batched( |
| 1268 |
&self, |
| 1269 |
message: &str, |
| 1270 |
extra_deps: &[ChangeHash], |
| 1271 |
selection: &Selection, |
| 75051941 Andy McCall ↶ |
1272 |
) -> Result<(ChangeHash, bool)> { |
| 6caacfff Andy McCall ↶ |
1273 |
let _wc = self.lock_working_copy()?; |
| e226a6cc Andy McCall ↶ |
1274 |
let mut snap = match self.snapshot_from_cache()? { |
| 1275 |
Some(snap) => snap, |
| 1276 |
None => self.snapshot_as("oxy commit", true, true)?, |
| 1277 |
}; |
| 236f35c0 Andy McCall ↶ |
1278 |
let pending = snap.manifest.take(); |
| 98286406 Andy McCall ↶ |
1279 |
if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() { |
| 236f35c0 Andy McCall ↶ |
1280 |
if let Some(m) = pending { |
| 1281 |
let conflicted = snap.base_snapshot.is_conflicted(); |
| 1282 |
self.write_pending_manifest(m, conflicted, HashMap::new())?; |
| 1283 |
} |
| 98286406 Andy McCall ↶ |
1284 |
return fail("nothing to commit: the working copy matches its base"); |
| 1285 |
} |
| 75167672 Andy McCall |
1286 |
if let Some(s) = snap |
| 1287 |
.statuses |
| 1288 |
.iter() |
| 1289 |
.find(|s| s.status == ChangeStatus::ConflictEdited) |
| 1290 |
{ |
| 98286406 Andy McCall ↶ |
1291 |
return fail(format!("{} still contains conflict markers", s.path)); |
| 1292 |
} |
| 1293 |
if message.trim().is_empty() { |
| 1294 |
return fail("a commit message is required"); |
| 1295 |
} |
| 192fad1e Andy McCall ↶ |
1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
let wc_refs = referenced_changes(&snap.wc, &snap.base); |
| f9466021 Andy McCall ↶ |
1300 |
let changed_paths: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 957fee30 Andy McCall ↶ |
1301 |
let (tree_ops, edits, partial) = match selection { |
| 192fad1e Andy McCall ↶ |
1302 |
Selection::All => ( |
| 1303 |
std::mem::take(&mut snap.wc.tree_ops), |
| 1304 |
std::mem::take(&mut snap.wc.edits), |
| 1305 |
false, |
| 1306 |
), |
| 957fee30 Andy McCall ↶ |
1307 |
Selection::Paths(paths) => { |
| 1308 |
let (o, e) = select_paths(&snap.wc, &snap.base_snapshot, paths); |
| 1309 |
let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len(); |
| 1310 |
(o, e, partial) |
| 1311 |
} |
| 1312 |
Selection::Hunks(indices) => { |
| c2e1d090 Andy McCall |
1313 |
let (o, e) = select_hunks(&snap.wc, indices, &snap.base_snapshot.tree.unified); |
| 957fee30 Andy McCall ↶ |
1314 |
let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len(); |
| 1315 |
(o, e, partial) |
| 1316 |
} |
| 1317 |
}; |
| 1318 |
if tree_ops.is_empty() && edits.is_empty() { |
| 1319 |
return fail("nothing selected to commit"); |
| 1320 |
} |
| 69953b50 Andy McCall ↶ |
1321 |
|
| 1322 |
|
| 957fee30 Andy McCall ↶ |
1323 |
if partial && snap.resolves { |
| 69953b50 Andy McCall ↶ |
1324 |
let resolved = snap |
| 1325 |
.statuses |
| 1326 |
.iter() |
| 1327 |
.filter(|s| s.status == ChangeStatus::Resolved) |
| 1328 |
.map(|s| s.path.as_str()); |
| 1329 |
let missing: Vec<&str> = match selection { |
| 1330 |
Selection::Paths(paths) => resolved |
| 1331 |
.filter(|p| { |
| 1332 |
!paths.iter().any(|x| { |
| 1333 |
p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/'))) |
| 1334 |
}) |
| 1335 |
}) |
| 1336 |
.collect(), |
| 1337 |
_ => resolved.collect(), |
| 1338 |
}; |
| 1339 |
if !missing.is_empty() { |
| 1340 |
return fail(format!( |
| 1341 |
"a conflict resolution takes every file it resolves, whole: commit by paths, including {}", |
| 1342 |
missing.join(", ") |
| 1343 |
)); |
| 1344 |
} |
| 957fee30 Andy McCall ↶ |
1345 |
} |
| 6d3eefbc Andy McCall ↶ |
1346 |
|
| 1347 |
self.run_hook( |
| 1348 |
"pre-commit", |
| 1349 |
&format!( |
| 1350 |
"{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}", |
| 1351 |
json_str(message), |
| 75167672 Andy McCall |
1352 |
json_array(snap.statuses.iter().map(|s| format!( |
| 1353 |
"{{\"path\":{},\"status\":{}}}", |
| 1354 |
json_str(&s.path), |
| 1355 |
json_str(&format!("{:?}", s.status)) |
| 1356 |
))) |
| 6d3eefbc Andy McCall ↶ |
1357 |
), |
| 1358 |
)?; |
| 98286406 Andy McCall ↶ |
1359 |
let (op_ref, op) = self.head_op()?; |
| 192fad1e Andy McCall ↶ |
1360 |
let mut deps: BTreeSet<ChangeHash> = wc_refs; |
| 6d3eefbc Andy McCall ↶ |
1361 |
|
| 1362 |
if self.config.semantic_deps { |
| 1363 |
let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 1364 |
let by_node = snap.base_snapshot.tree.by_node(); |
| 1365 |
let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new(); |
| 1366 |
let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 1367 |
for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) { |
| 1368 |
if !seen.insert(*h) { |
| 1369 |
continue; |
| 1370 |
} |
| 1371 |
let c = self.store.get_change(h)?; |
| 1372 |
let mut paths: BTreeSet<String> = BTreeSet::new(); |
| 75167672 Andy McCall |
1373 |
for n in c |
| 1374 |
.tree_ops |
| 1375 |
.iter() |
| 1376 |
.map(|o| o.node()) |
| 1377 |
.chain(c.edits.iter().map(|e| e.node())) |
| 1378 |
{ |
| 6d3eefbc Andy McCall ↶ |
1379 |
if let Some(e) = by_node.get(&n) { |
| 1380 |
paths.insert(e.path.clone()); |
| 1381 |
} |
| 1382 |
} |
| 1383 |
candidates.push((c.change_id, *h, paths.into_iter().collect())); |
| 1384 |
} |
| 1385 |
for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? { |
| 1386 |
if snap.base.contains(&d) { |
| 1387 |
deps.insert(d); |
| 1388 |
} |
| 1389 |
} |
| 1390 |
} |
| 98286406 Andy McCall ↶ |
1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
deps.extend(snap.contributors.iter().copied()); |
| 1395 |
for d in extra_deps { |
| 1396 |
if !snap.base.contains(d) { |
| 75167672 Andy McCall |
1397 |
return fail(format!( |
| 1398 |
"--depends-on {d}: not in the working copy's base state" |
| 1399 |
)); |
| 98286406 Andy McCall ↶ |
1400 |
} |
| 1401 |
deps.insert(*d); |
| 1402 |
} |
| 1403 |
let mut change = Change { |
| 6f8aeb99 Andy McCall ↶ |
1404 |
version: crate::objects::CHANGE_VERSION, |
| 75167672 Andy McCall |
1405 |
change_id: if partial { |
| 1406 |
ChangeId::random() |
| 1407 |
} else { |
| 1408 |
snap.wc.change_id |
| 1409 |
}, |
| 957fee30 Andy McCall ↶ |
1410 |
deps: vec![], |
| 98286406 Andy McCall ↶ |
1411 |
author: self.author(), |
| 1412 |
hlc: Hlc::now(), |
| 1413 |
message: message.trim_end().to_string(), |
| 957fee30 Andy McCall ↶ |
1414 |
tree_ops, |
| 1415 |
edits, |
| 75167672 Andy McCall |
1416 |
kind: if snap.resolves { |
| 1417 |
ChangeKind::Resolution |
| 1418 |
} else { |
| 1419 |
ChangeKind::Normal |
| 1420 |
}, |
| 98286406 Andy McCall ↶ |
1421 |
}; |
| 957fee30 Andy McCall ↶ |
1422 |
if partial { |
| 1423 |
|
| 1424 |
|
| 1425 |
deps = referenced_changes(&change, &snap.base); |
| 1426 |
for d in extra_deps { |
| 1427 |
deps.insert(*d); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
change.deps = deps.into_iter().collect(); |
| 98286406 Andy McCall ↶ |
1431 |
change.normalize(); |
| f9466021 Andy McCall ↶ |
1432 |
let signing = crate::timing::phase("commit: sign and store"); |
| 614541c3 Andy McCall ↶ |
1433 |
let hash = self.store_signed(&change)?; |
| f9466021 Andy McCall ↶ |
1434 |
drop(signing); |
| 1435 |
let extending = crate::timing::phase("commit: extend the state"); |
| 98286406 Andy McCall ↶ |
1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
let old_heads = self.heads_of(&op.view, &self.config.bookmark)?; |
| 1442 |
let old_base = snap.base.maximal(&snap.wc.deps); |
| 1443 |
let mut all: Vec<ChangeHash> = old_heads.clone(); |
| 1444 |
all.extend(old_base.iter().copied()); |
| 1445 |
all.push(hash); |
| 07eb99be Andy McCall ↶ |
1446 |
|
| 1447 |
|
| 1448 |
let mut st = if old_heads.iter().all(|h| snap.base.contains(h)) { |
| 1449 |
snap.base |
| 1450 |
} else { |
| 1451 |
self.load_state(&all)? |
| 1452 |
}; |
| 192fad1e Andy McCall ↶ |
1453 |
st.add_hashed(hash, change)?; |
| 98286406 Andy McCall ↶ |
1454 |
let mut with_new = old_heads.clone(); |
| 1455 |
with_new.push(hash); |
| 1456 |
let new_heads = st.maximal(&with_new); |
| 252e8cb2 Andy McCall ↶ |
1457 |
|
| 75167672 Andy McCall |
1458 |
let since = self |
| 1459 |
.latest_checkpoint(&self.config.bookmark)? |
| 1460 |
.map(|(_, cp)| cp.count) |
| 1461 |
.unwrap_or(0); |
| 1462 |
if self.config.checkpoint_interval > 0 |
| 1463 |
&& (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval |
| 1464 |
{ |
| 252e8cb2 Andy McCall ↶ |
1465 |
let cp_state = self.load_state_full(&new_heads)?; |
| 1466 |
self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?; |
| 1467 |
} |
| 98286406 Andy McCall ↶ |
1468 |
let mut wc_base = old_base.clone(); |
| 1469 |
wc_base.push(hash); |
| 1470 |
let wc_base = st.maximal(&wc_base); |
| f9466021 Andy McCall ↶ |
1471 |
drop(extending); |
| 98286406 Andy McCall ↶ |
1472 |
|
| 8457a6cc Andy McCall ↶ |
1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 236f35c0 Andy McCall ↶ |
1477 |
let keying = crate::timing::phase("commit: graph keys of the files"); |
| f9466021 Andy McCall ↶ |
1478 |
let committed_keys = |
| 1479 |
(!partial).then(|| self.committed_graph_keys(&st, &changed_paths, &snap.attributes)); |
| 236f35c0 Andy McCall ↶ |
1480 |
drop(keying); |
| f9466021 Andy McCall ↶ |
1481 |
let recording = crate::timing::phase("commit: record"); |
| 98286406 Andy McCall ↶ |
1482 |
let wc = self.new_working_copy(wc_base, ""); |
| 1483 |
let wc_hash = self.store.put_change(&wc)?; |
| 1484 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
1485 |
view.bookmarks.insert( |
| 1486 |
self.config.bookmark.clone(), |
| 1487 |
self.store.put_state(&StateObject::new(new_heads))?, |
| 1488 |
); |
| 98286406 Andy McCall ↶ |
1489 |
view.workspaces.insert(self.workspace, wc_hash); |
| 1490 |
self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?; |
| f9466021 Andy McCall ↶ |
1491 |
drop(recording); |
| 236f35c0 Andy McCall ↶ |
1492 |
let rewriting = crate::timing::phase("commit: manifest"); |
| 957fee30 Andy McCall ↶ |
1493 |
if partial { |
| 1494 |
|
| 1495 |
self.drop_manifest(); |
| 1496 |
} else { |
| 86e1824f Andy McCall ↶ |
1497 |
let conflicted = snap.base_snapshot.is_conflicted() && !snap.resolves; |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
let keys = if conflicted { |
| 1502 |
HashMap::new() |
| 1503 |
} else { |
| 8457a6cc Andy McCall ↶ |
1504 |
committed_keys.unwrap_or_default() |
| 86e1824f Andy McCall ↶ |
1505 |
}; |
| 236f35c0 Andy McCall ↶ |
1506 |
match pending { |
| 1507 |
Some(m) => self.write_pending_manifest(m, conflicted, keys)?, |
| 1508 |
None => self.rewrite_manifest(conflicted, false, &keys)?, |
| 1509 |
} |
| 957fee30 Andy McCall ↶ |
1510 |
} |
| 236f35c0 Andy McCall ↶ |
1511 |
drop(rewriting); |
| 1512 |
let packing = crate::timing::phase("commit: autopack"); |
| d02e545d Andy McCall ↶ |
1513 |
self.maybe_autopack()?; |
| 236f35c0 Andy McCall ↶ |
1514 |
drop(packing); |
| 1515 |
let _hook = crate::timing::phase("commit: hook"); |
| 75167672 Andy McCall |
1516 |
let _ = self.run_hook( |
| 1517 |
"post-commit", |
| 1518 |
&format!( |
| 1519 |
"{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}", |
| 1520 |
json_str(&snap.wc.change_id.to_hex()), |
| 1521 |
json_str(&hash.to_hex()), |
| 1522 |
json_str(message) |
| 1523 |
), |
| 1524 |
); |
| 75051941 Andy McCall ↶ |
1525 |
Ok((hash, partial)) |
| 98286406 Andy McCall ↶ |
1526 |
} |
| 1527 |
} |
| 1528 |
|
| 957fee30 Andy McCall ↶ |
1529 |
|
| 1530 |
#[derive(Clone, Debug)] |
| 1531 |
pub enum Selection { |
| 1532 |
All, |
| 1533 |
|
| 1534 |
Paths(Vec<String>), |
| 1535 |
|
| 1536 |
Hunks(Vec<usize>), |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
#[derive(Clone, Debug)] |
| 1541 |
pub struct Hunk { |
| 1542 |
pub index: usize, |
| 1543 |
pub path: String, |
| 1544 |
|
| 1545 |
pub text: String, |
| 1546 |
} |
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 75167672 Andy McCall |
1550 |
fn select_paths( |
| 1551 |
wc: &Change, |
| 1552 |
base_snapshot: &Snapshot, |
| 1553 |
paths: &[String], |
| 1554 |
) -> (Vec<TreeOp>, Vec<Edit>) { |
| 1555 |
let selected = |p: &str| { |
| 1556 |
paths |
| 1557 |
.iter() |
| 1558 |
.any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))) |
| 1559 |
}; |
| 957fee30 Andy McCall ↶ |
1560 |
let by_node = base_snapshot.tree.by_node(); |
| 1561 |
let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 1562 |
for op in &wc.tree_ops { |
| 75167672 Andy McCall |
1563 |
if let TreeOp::AddNode { |
| 1564 |
node, |
| 1565 |
parent, |
| 1566 |
name, |
| 1567 |
kind, |
| 1568 |
.. |
| 1569 |
} = op |
| 1570 |
{ |
| 957fee30 Andy McCall ↶ |
1571 |
new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 1572 |
} |
| 1573 |
} |
| 1574 |
let mut nodes: BTreeSet<NodeId> = BTreeSet::new(); |
| 75167672 Andy McCall |
1575 |
for n in wc |
| 1576 |
.tree_ops |
| 1577 |
.iter() |
| 1578 |
.map(|o| o.node()) |
| 1579 |
.chain(wc.edits.iter().map(|e| e.node())) |
| 1580 |
{ |
| 957fee30 Andy McCall ↶ |
1581 |
if path_of_node(&n, &by_node, &new_names).is_some_and(|p| selected(&p)) { |
| 1582 |
nodes.insert(n); |
| 1583 |
} |
| 1584 |
} |
| c2e1d090 Andy McCall |
1585 |
|
| 1586 |
for (dup, shown) in &base_snapshot.tree.unified { |
| 1587 |
if nodes.contains(shown) { |
| 1588 |
nodes.insert(*dup); |
| 1589 |
} |
| 1590 |
} |
| 957fee30 Andy McCall ↶ |
1591 |
|
| 1592 |
let mut changed = true; |
| 1593 |
while changed { |
| 1594 |
changed = false; |
| 1595 |
for (n, (parent, _, _)) in &new_names { |
| 1596 |
if nodes.contains(n) && new_names.contains_key(parent) && nodes.insert(*parent) { |
| 1597 |
changed = true; |
| 1598 |
} |
| 1599 |
} |
| 1600 |
} |
| 75167672 Andy McCall |
1601 |
let ops = wc |
| 1602 |
.tree_ops |
| 1603 |
.iter() |
| 1604 |
.filter(|o| nodes.contains(&o.node())) |
| 1605 |
.cloned() |
| 1606 |
.collect(); |
| 1607 |
let edits = wc |
| 1608 |
.edits |
| 1609 |
.iter() |
| 1610 |
.filter(|e| nodes.contains(&e.node())) |
| 1611 |
.cloned() |
| 1612 |
.collect(); |
| 957fee30 Andy McCall ↶ |
1613 |
(ops, edits) |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| c2e1d090 Andy McCall |
1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
fn select_hunks( |
| 1623 |
wc: &Change, |
| 1624 |
indices: &[usize], |
| 1625 |
unified: &BTreeMap<NodeId, NodeId>, |
| 1626 |
) -> (Vec<TreeOp>, Vec<Edit>) { |
| 957fee30 Andy McCall ↶ |
1627 |
let n_ops = wc.tree_ops.len(); |
| 1628 |
let mut ops_sel = vec![false; n_ops]; |
| 1629 |
let mut edits_sel = vec![false; wc.edits.len()]; |
| 1630 |
for &i in indices { |
| 1631 |
if i < n_ops { |
| 1632 |
ops_sel[i] = true; |
| 1633 |
} else if i - n_ops < wc.edits.len() { |
| 1634 |
edits_sel[i - n_ops] = true; |
| 1635 |
} |
| 1636 |
} |
| 1637 |
|
| 1638 |
let mut per_node: HashMap<NodeId, u32> = HashMap::new(); |
| 1639 |
let mut insert_ranges: Vec<Option<(NodeId, u32, u32)>> = Vec::new(); |
| 1640 |
for e in &wc.edits { |
| 1641 |
insert_ranges.push(match e { |
| 1642 |
Edit::Insert { node, lines, .. } => { |
| 1643 |
let start = per_node.entry(*node).or_default(); |
| 1644 |
let r = (*node, *start, *start + lines.len() as u32); |
| 1645 |
*start += lines.len() as u32; |
| 1646 |
Some(r) |
| 1647 |
} |
| 1648 |
_ => None, |
| 1649 |
}); |
| 1650 |
} |
| 75167672 Andy McCall |
1651 |
let add_index: HashMap<NodeId, usize> = wc |
| 1652 |
.tree_ops |
| 1653 |
.iter() |
| 1654 |
.enumerate() |
| 1655 |
.filter_map(|(i, o)| { |
| 1656 |
if let TreeOp::AddNode { node, .. } = o { |
| 1657 |
Some((*node, i)) |
| 1658 |
} else { |
| 1659 |
None |
| 1660 |
} |
| 1661 |
}) |
| 1662 |
.collect(); |
| 1663 |
let insert_for = |node: &NodeId, index: u32| -> Option<usize> { |
| 1664 |
insert_ranges |
| 1665 |
.iter() |
| 1666 |
.position(|r| matches!(r, Some((n, a, b)) if n == node && *a <= index && index < *b)) |
| 1667 |
}; |
| 957fee30 Andy McCall ↶ |
1668 |
|
| 1669 |
let mut changed = true; |
| 1670 |
while changed { |
| 1671 |
changed = false; |
| 1672 |
for i in 0..n_ops { |
| 1673 |
if !ops_sel[i] { |
| 1674 |
continue; |
| 1675 |
} |
| d8c086ca Andy McCall ↶ |
1676 |
if let TreeOp::AddNode { node, parent, .. } = &wc.tree_ops[i] { |
| 957fee30 Andy McCall ↶ |
1677 |
if let Some(&p) = add_index.get(parent) { |
| 1678 |
if !ops_sel[p] { |
| 1679 |
ops_sel[p] = true; |
| 1680 |
changed = true; |
| 1681 |
} |
| 1682 |
} |
| d8c086ca Andy McCall ↶ |
1683 |
|
| 1684 |
|
| 1685 |
for (j, e) in wc.edits.iter().enumerate() { |
| 1686 |
if e.node() == *node && !edits_sel[j] { |
| 1687 |
edits_sel[j] = true; |
| 1688 |
changed = true; |
| 1689 |
} |
| 1690 |
} |
| 957fee30 Andy McCall ↶ |
1691 |
} |
| 1692 |
} |
| 1693 |
for j in 0..wc.edits.len() { |
| 1694 |
if !edits_sel[j] { |
| 1695 |
continue; |
| 1696 |
} |
| 1697 |
let e = &wc.edits[j]; |
| 1698 |
if let Some(&a) = add_index.get(&e.node()) { |
| 1699 |
if !ops_sel[a] { |
| 1700 |
ops_sel[a] = true; |
| 1701 |
changed = true; |
| 1702 |
} |
| 1703 |
} |
| 75167672 Andy McCall |
1704 |
if let Edit::Insert { |
| 1705 |
node, |
| 1706 |
after, |
| 1707 |
before, |
| 1708 |
.. |
| 1709 |
} = e |
| 1710 |
{ |
| 957fee30 Andy McCall ↶ |
1711 |
for r in [after, before] { |
| 1712 |
if let AtomRef::Atom { change, index } = r { |
| 1713 |
if change.is_zero() { |
| 1714 |
if let Some(k) = insert_for(node, *index) { |
| 1715 |
if !edits_sel[k] { |
| 1716 |
edits_sel[k] = true; |
| 1717 |
changed = true; |
| 1718 |
} |
| 1719 |
} |
| 1720 |
} |
| 1721 |
} |
| c2e1d090 Andy McCall |
1722 |
} |
| 1723 |
} |
| 1724 |
} |
| 1725 |
} |
| 1726 |
|
| 1727 |
if !unified.is_empty() { |
| 1728 |
let chosen: HashSet<NodeId> = (0..n_ops) |
| 1729 |
.filter(|i| ops_sel[*i]) |
| 1730 |
.map(|i| wc.tree_ops[i].node()) |
| 1731 |
.chain( |
| 1732 |
(0..wc.edits.len()) |
| 1733 |
.filter(|j| edits_sel[*j]) |
| 1734 |
.map(|j| wc.edits[j].node()), |
| 1735 |
) |
| 1736 |
.collect(); |
| 1737 |
for (i, o) in wc.tree_ops.iter().enumerate() { |
| 1738 |
if let TreeOp::RemoveNode { node, .. } = o { |
| 1739 |
if unified.get(node).is_some_and(|s| chosen.contains(s)) { |
| 1740 |
ops_sel[i] = true; |
| 957fee30 Andy McCall ↶ |
1741 |
} |
| 1742 |
} |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
let mut remap: HashMap<(NodeId, u32), u32> = HashMap::new(); |
| 1747 |
let mut new_counts: HashMap<NodeId, u32> = HashMap::new(); |
| 1748 |
for (j, r) in insert_ranges.iter().enumerate() { |
| 1749 |
if let (true, Some((node, a, b))) = (edits_sel[j], r) { |
| 1750 |
let next = new_counts.entry(*node).or_default(); |
| 1751 |
for old in *a..*b { |
| 1752 |
remap.insert((*node, old), *next); |
| 1753 |
*next += 1; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
} |
| 1757 |
let map_ref = |r: AtomRef, node: &NodeId| -> AtomRef { |
| 1758 |
match r { |
| 75167672 Andy McCall |
1759 |
AtomRef::Atom { change, index } if change.is_zero() => AtomRef::Atom { |
| 1760 |
change, |
| 1761 |
index: remap.get(&(*node, index)).copied().unwrap_or(index), |
| 1762 |
}, |
| 957fee30 Andy McCall ↶ |
1763 |
other => other, |
| 1764 |
} |
| 1765 |
}; |
| 75167672 Andy McCall |
1766 |
let ops = wc |
| 1767 |
.tree_ops |
| 1768 |
.iter() |
| 1769 |
.enumerate() |
| 1770 |
.filter(|(i, _)| ops_sel[*i]) |
| 1771 |
.map(|(_, o)| o.clone()) |
| 1772 |
.collect(); |
| 957fee30 Andy McCall ↶ |
1773 |
let edits = wc |
| 1774 |
.edits |
| 1775 |
.iter() |
| 1776 |
.enumerate() |
| 1777 |
.filter(|(j, _)| edits_sel[*j]) |
| 1778 |
.map(|(_, e)| match e { |
| 75167672 Andy McCall |
1779 |
Edit::Insert { |
| 1780 |
node, |
| 1781 |
after, |
| 1782 |
before, |
| 1783 |
lines, |
| 1784 |
} => Edit::Insert { |
| 1785 |
node: *node, |
| 1786 |
after: map_ref(*after, node), |
| 1787 |
before: map_ref(*before, node), |
| 1788 |
lines: lines.clone(), |
| 1789 |
}, |
| 957fee30 Andy McCall ↶ |
1790 |
other => other.clone(), |
| 1791 |
}) |
| 1792 |
.collect(); |
| 1793 |
(ops, edits) |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
pub fn render_hunks(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<Hunk> { |
| 1798 |
let by_node = base_snapshot.tree.by_node(); |
| 1799 |
let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 1800 |
for op in &change.tree_ops { |
| 75167672 Andy McCall |
1801 |
if let TreeOp::AddNode { |
| 1802 |
node, |
| 1803 |
parent, |
| 1804 |
name, |
| 1805 |
kind, |
| 1806 |
.. |
| 1807 |
} = op |
| 1808 |
{ |
| 957fee30 Andy McCall ↶ |
1809 |
new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 1810 |
} |
| 1811 |
} |
| 1812 |
let mut out = Vec::new(); |
| 1813 |
for (i, op) in change.tree_ops.iter().enumerate() { |
| c2e1d090 Andy McCall |
1814 |
|
| 1815 |
if is_unified_retirement(op, base_snapshot) { |
| 1816 |
continue; |
| 1817 |
} |
| 75167672 Andy McCall |
1818 |
let one = Change { |
| 1819 |
tree_ops: vec![op.clone()], |
| 1820 |
edits: vec![], |
| 1821 |
..change.clone() |
| 1822 |
}; |
| 957fee30 Andy McCall ↶ |
1823 |
let path = path_of_node(&op.node(), &by_node, &new_names).unwrap_or_default(); |
| 75167672 Andy McCall |
1824 |
out.push(Hunk { |
| 1825 |
index: i, |
| 1826 |
path, |
| 1827 |
text: render_change(&one, base, base_snapshot), |
| 1828 |
}); |
| 957fee30 Andy McCall ↶ |
1829 |
} |
| 1830 |
for (j, e) in change.edits.iter().enumerate() { |
| 75167672 Andy McCall |
1831 |
let one = Change { |
| 1832 |
tree_ops: vec![], |
| 1833 |
edits: vec![e.clone()], |
| 1834 |
..change.clone() |
| 1835 |
}; |
| 957fee30 Andy McCall ↶ |
1836 |
let path = path_of_node(&e.node(), &by_node, &new_names).unwrap_or_default(); |
| 75167672 Andy McCall |
1837 |
out.push(Hunk { |
| 1838 |
index: change.tree_ops.len() + j, |
| 1839 |
path, |
| 1840 |
text: render_change(&one, base, base_snapshot), |
| 1841 |
}); |
| 957fee30 Andy McCall ↶ |
1842 |
} |
| 1843 |
out |
| 1844 |
} |
| 1845 |
|
| c6aa5bd9 Andy McCall ↶ |
1846 |
|
| 1847 |
#[derive(Clone, Debug)] |
| 1848 |
pub struct FileDiff { |
| 1849 |
pub path: String, |
| 1850 |
|
| 1851 |
pub kind: String, |
| 1852 |
pub hunks: Vec<DiffHunk>, |
| 1853 |
|
| 1854 |
pub from: Option<String>, |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
#[derive(Clone, Debug, Default)] |
| 1860 |
pub struct DiffHunk { |
| 1861 |
pub base_line: u32, |
| 1862 |
pub added: Vec<Vec<u8>>, |
| 1863 |
pub removed: Vec<(u32, Vec<u8>)>, |
| 1864 |
} |
| 1865 |
|
| 1866 |
impl Repo { |
| 1867 |
|
| 1868 |
pub fn file_at(&self, path: &str, heads: Option<&[ChangeHash]>) -> Result<Vec<u8>> { |
| 19303b4a Andy McCall ↶ |
1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
let base = if self.is_bare() { |
| 1874 |
None |
| 1875 |
} else { |
| 1876 |
Some(self.working_copy()?.1.deps) |
| 1877 |
}; |
| 1878 |
let same = |a: &[ChangeHash], b: &[ChangeHash]| { |
| 1879 |
let (mut a, mut b) = (a.to_vec(), b.to_vec()); |
| 1880 |
a.sort(); |
| 1881 |
b.sort(); |
| 1882 |
a == b |
| 1883 |
}; |
| 1884 |
let mut state = match (heads, base) { |
| 1885 |
(Some(h), Some(b)) if same(h, &b) => self.load_state_indexed(&b)?, |
| 1886 |
(Some(h), _) => self.load_state_history(h)?, |
| 1887 |
(None, Some(b)) => self.load_state_indexed(&b)?, |
| 1888 |
(None, None) => return fail("this is a bare repository: it has no working copy"), |
| 1889 |
}; |
| c2e1d090 Andy McCall |
1890 |
let view = self.tree_view(&mut state)?; |
| 19303b4a Andy McCall ↶ |
1891 |
let Some(node) = view.entries.get(path).map(|e| e.node) else { |
| 1892 |
return fail(format!("{path} is not in that state")); |
| c6aa5bd9 Andy McCall ↶ |
1893 |
}; |
| 19303b4a Andy McCall ↶ |
1894 |
self.load_graphs_of(&mut state, &[node])?; |
| 1895 |
let snapshot = state.materialize_paths(view, [path]); |
| 75167672 Andy McCall |
1896 |
let Some(f) = snapshot.files.get(path) else { |
| 1897 |
return fail(format!("{path} is not in that state")); |
| 1898 |
}; |
| c6aa5bd9 Andy McCall ↶ |
1899 |
match &f.content { |
| 1900 |
FileContent::Dir => fail(format!("{path} is a directory")), |
| 1901 |
FileContent::Text { bytes, .. } => Ok(bytes.clone()), |
| 1902 |
FileContent::Opaque { blobs } => match blobs.first() { |
| 1903 |
Some(b) => self.read_blob(b), |
| 1904 |
None => Ok(vec![]), |
| 1905 |
}, |
| 1906 |
} |
| 1907 |
} |
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
pub fn discard(&self, paths: &[String]) -> Result<usize> { |
| 6caacfff Andy McCall ↶ |
1913 |
let _wc = self.lock_working_copy()?; |
| 8ab265dc Andy McCall ↶ |
1914 |
let snap = self.snapshot_status("oxy discard")?; |
| 75167672 Andy McCall |
1915 |
let under = |p: &str| { |
| 1916 |
paths |
| 1917 |
.iter() |
| 1918 |
.any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))) |
| 1919 |
}; |
| c6aa5bd9 Andy McCall ↶ |
1920 |
let attrs = Repo::state_attributes(&snap.base_snapshot); |
| 1921 |
let mut n = 0; |
| 1922 |
for s in &snap.statuses { |
| 1923 |
if !under(&s.path) { |
| 1924 |
continue; |
| 1925 |
} |
| 1926 |
match snap.base_snapshot.files.get(&s.path) { |
| 1927 |
None => { |
| 1928 |
|
| 1929 |
let full = self.root.join(&s.path); |
| 1930 |
if full.is_dir() { |
| 1931 |
let _ = fs::remove_dir_all(&full); |
| 1932 |
} else { |
| 1933 |
let _ = fs::remove_file(&full); |
| 1934 |
} |
| 1935 |
n += 1; |
| 1936 |
} |
| 1937 |
Some(f) => { |
| 1938 |
match &f.content { |
| 1939 |
FileContent::Dir => fs::create_dir_all(self.root.join(&s.path))?, |
| 1940 |
FileContent::Text { bytes, .. } => { |
| 75167672 Andy McCall |
1941 |
let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&s.path).eol |
| 1942 |
== Some(crate::attributes::Eol::Crlf) |
| 1943 |
{ |
| 1944 |
crate::attributes::to_crlf(bytes).into() |
| 1945 |
} else { |
| 1946 |
bytes.as_slice().into() |
| 1947 |
}; |
| c6aa5bd9 Andy McCall ↶ |
1948 |
self.write_file(&s.path, &on_disk, f.mode.exec)?; |
| 1949 |
} |
| 1950 |
FileContent::Opaque { blobs } => { |
| 1951 |
let bytes = match blobs.first() { |
| 1952 |
Some(b) => self.read_blob(b)?, |
| 1953 |
None => vec![], |
| 1954 |
}; |
| 1955 |
if f.kind == NodeKind::Symlink { |
| 1956 |
self.write_symlink(&s.path, &bytes)?; |
| 1957 |
} else if f.kind == NodeKind::File { |
| 1958 |
self.write_file(&s.path, &bytes, f.mode.exec)?; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
} |
| 1962 |
n += 1; |
| 1963 |
} |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
for (p, f) in &snap.base_snapshot.files { |
| 75167672 Andy McCall |
1968 |
if under(p) |
| 1969 |
&& !self.root.join(p).exists() |
| 1970 |
&& f.kind != NodeKind::Dir |
| 1971 |
&& f.kind != NodeKind::Subrepo |
| 1972 |
&& !snap.statuses.iter().any(|s| s.path == *p) |
| 1973 |
{ |
| c6aa5bd9 Andy McCall ↶ |
1974 |
let bytes = match &f.content { |
| 1975 |
FileContent::Text { bytes, .. } => bytes.clone(), |
| 75167672 Andy McCall |
1976 |
FileContent::Opaque { blobs } => blobs |
| 1977 |
.first() |
| 1978 |
.map(|b| self.read_blob(b)) |
| 1979 |
.transpose()? |
| 1980 |
.unwrap_or_default(), |
| c6aa5bd9 Andy McCall ↶ |
1981 |
FileContent::Dir => continue, |
| 1982 |
}; |
| 75167672 Andy McCall |
1983 |
if f.kind == NodeKind::Symlink { |
| 1984 |
self.write_symlink(p, &bytes)? |
| 1985 |
} else { |
| 1986 |
self.write_file(p, &bytes, f.mode.exec)? |
| 1987 |
} |
| c6aa5bd9 Andy McCall ↶ |
1988 |
n += 1; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
self.drop_manifest(); |
| 1992 |
Ok(n) |
| 1993 |
} |
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
pub fn discard_hunks(&self, indices: &[usize]) -> Result<usize> { |
| 6caacfff Andy McCall ↶ |
1999 |
let _wc = self.lock_working_copy()?; |
| c6aa5bd9 Andy McCall ↶ |
2000 |
let (snap, hunks) = self.hunks()?; |
| 75167672 Andy McCall |
2001 |
let keep: Vec<usize> = hunks |
| 2002 |
.iter() |
| 2003 |
.map(|h| h.index) |
| 2004 |
.filter(|i| !indices.contains(i)) |
| 2005 |
.collect(); |
| c2e1d090 Andy McCall |
2006 |
let (ops, edits) = select_hunks(&snap.wc, &keep, &snap.base_snapshot.tree.unified); |
| c6aa5bd9 Andy McCall ↶ |
2007 |
let dropped_nodes: BTreeSet<NodeId> = hunks |
| 2008 |
.iter() |
| 2009 |
.filter(|h| indices.contains(&h.index)) |
| 75167672 Andy McCall |
2010 |
.map(|h| { |
| 2011 |
if h.index < snap.wc.tree_ops.len() { |
| 2012 |
snap.wc.tree_ops[h.index].node() |
| 2013 |
} else { |
| 2014 |
snap.wc.edits[h.index - snap.wc.tree_ops.len()].node() |
| 2015 |
} |
| 2016 |
}) |
| c6aa5bd9 Andy McCall ↶ |
2017 |
.collect(); |
| 2018 |
let mut partial = snap.wc.clone(); |
| 2019 |
partial.tree_ops = ops; |
| 2020 |
partial.edits = edits; |
| 2021 |
partial.normalize(); |
| 2022 |
let mut st = snap.base.clone(); |
| 2023 |
st.add_unchecked(partial); |
| 8ab265dc Andy McCall ↶ |
2024 |
|
| 2025 |
|
| 2026 |
self.load_all_graphs(&mut st)?; |
| c6aa5bd9 Andy McCall ↶ |
2027 |
let target = st.materialize(); |
| 2028 |
let by_node_target = target.tree.by_node(); |
| 2029 |
let by_node_base = snap.base_snapshot.tree.by_node(); |
| 2030 |
let attrs = Repo::state_attributes(&target); |
| 2031 |
let mut n = 0; |
| 2032 |
for node in dropped_nodes { |
| 2033 |
match by_node_target.get(&node) { |
| 2034 |
Some(e) => { |
| 2035 |
let f = &target.files[&e.path]; |
| 2036 |
match &f.content { |
| 2037 |
FileContent::Dir => {} |
| 2038 |
FileContent::Text { bytes, .. } => { |
| 75167672 Andy McCall |
2039 |
let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&e.path).eol |
| 2040 |
== Some(crate::attributes::Eol::Crlf) |
| 2041 |
{ |
| 2042 |
crate::attributes::to_crlf(bytes).into() |
| 2043 |
} else { |
| 2044 |
bytes.as_slice().into() |
| 2045 |
}; |
| c6aa5bd9 Andy McCall ↶ |
2046 |
self.write_file(&e.path, &on_disk, f.mode.exec)?; |
| 2047 |
} |
| 2048 |
FileContent::Opaque { blobs } => { |
| 75167672 Andy McCall |
2049 |
let bytes = blobs |
| 2050 |
.first() |
| 2051 |
.map(|b| self.read_blob(b)) |
| 2052 |
.transpose()? |
| 2053 |
.unwrap_or_default(); |
| 2054 |
if f.kind == NodeKind::Symlink { |
| 2055 |
self.write_symlink(&e.path, &bytes)? |
| 2056 |
} else { |
| 2057 |
self.write_file(&e.path, &bytes, f.mode.exec)? |
| 2058 |
} |
| c6aa5bd9 Andy McCall ↶ |
2059 |
} |
| 2060 |
} |
| 2061 |
|
| 75167672 Andy McCall |
2062 |
if let Some(old_path) = snap |
| 2063 |
.statuses |
| 2064 |
.iter() |
| 2065 |
.find(|s| { |
| 2066 |
s.status == ChangeStatus::Renamed && !target.files.contains_key(&s.path) |
| 2067 |
}) |
| 2068 |
.map(|s| s.path.clone()) |
| 2069 |
{ |
| c6aa5bd9 Andy McCall ↶ |
2070 |
let _ = fs::remove_file(self.root.join(old_path)); |
| 2071 |
} |
| 2072 |
n += 1; |
| 2073 |
} |
| 2074 |
None => { |
| 2075 |
|
| 75167672 Andy McCall |
2076 |
if let Some(added) = snap.statuses.iter().find(|s| { |
| 2077 |
s.status == ChangeStatus::Added |
| 2078 |
&& !target.files.contains_key(&s.path) |
| 2079 |
&& !by_node_base.contains_key(&node) |
| 2080 |
}) { |
| c6aa5bd9 Andy McCall ↶ |
2081 |
let full = self.root.join(&added.path); |
| 75167672 Andy McCall |
2082 |
if full.is_dir() { |
| 2083 |
let _ = fs::remove_dir_all(&full); |
| 2084 |
} else { |
| 2085 |
let _ = fs::remove_file(&full); |
| 2086 |
} |
| c6aa5bd9 Andy McCall ↶ |
2087 |
n += 1; |
| 2088 |
} |
| 2089 |
} |
| 2090 |
} |
| 2091 |
} |
| 2092 |
self.drop_manifest(); |
| 2093 |
Ok(n) |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
pub fn diff_files(&self, change: Option<&ChangeHash>) -> Result<Vec<FileDiff>> { |
| 8ab265dc Andy McCall ↶ |
2099 |
let (c, base, base_snapshot) = match change { |
| c6aa5bd9 Andy McCall ↶ |
2100 |
Some(h) => { |
| 2101 |
let c = self.store.get_change(h)?; |
| 8ab265dc Andy McCall ↶ |
2102 |
let (base, snapshot) = self.change_base(&c)?; |
| 2103 |
(c, base, snapshot) |
| c6aa5bd9 Andy McCall ↶ |
2104 |
} |
| 74b4a3c6 Andy McCall ↶ |
2105 |
None => match self.working_copy_with_base_unless_unchanged()? { |
| 2106 |
Some(all) => all, |
| 2107 |
None => return Ok(Vec::new()), |
| 2108 |
}, |
| c6aa5bd9 Andy McCall ↶ |
2109 |
}; |
| 2110 |
Ok(diff_files_of(&c, &base, &base_snapshot)) |
| e226a6cc Andy McCall ↶ |
2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
pub(crate) fn snapshot_from_cache(&self) -> Result<Option<SnapshotResult>> { |
| 2121 |
let Some(statuses) = self.status_by_manifest()? else { |
| 2122 |
return Ok(None); |
| 2123 |
}; |
| 2124 |
if statuses.is_empty() { |
| 2125 |
return Ok(None); |
| 2126 |
} |
| 2127 |
let _t = crate::timing::phase("snapshot: from the status cache"); |
| 2128 |
let (wc_hash, wc) = self.working_copy()?; |
| 2129 |
let base = self.load_state_indexed(&wc.deps)?; |
| 2130 |
let (base, base_snapshot) = self.render_base_for(&wc, base)?; |
| 2131 |
let attribute_files: Vec<(String, Vec<u8>)> = match self.read_manifest()? { |
| 2132 |
Some((_, _, m)) => m |
| 2133 |
.entries |
| 2134 |
.keys() |
| 2135 |
.filter(|p| is_attributes(p)) |
| 2136 |
.filter_map(|p| fs::read(self.root.join(p)).ok().map(|b| (p.clone(), b))) |
| 2137 |
.collect(), |
| 2138 |
None => Vec::new(), |
| 2139 |
}; |
| 2140 |
let attributes = crate::attributes::Attributes::from_files( |
| 2141 |
attribute_files |
| 2142 |
.iter() |
| 2143 |
.map(|(p, b)| (p.as_str(), b.as_slice())), |
| 2144 |
); |
| 2145 |
Ok(Some(SnapshotResult { |
| 2146 |
wc_hash, |
| 2147 |
wc, |
| 2148 |
base, |
| 2149 |
base_snapshot, |
| 2150 |
statuses, |
| 2151 |
changed: false, |
| 2152 |
resolves: false, |
| 2153 |
contributors: Vec::new(), |
| 2154 |
attributes, |
| 2155 |
untracked_nested: self.untracked_nested.borrow().clone(), |
| 236f35c0 Andy McCall ↶ |
2156 |
manifest: None, |
| e226a6cc Andy McCall ↶ |
2157 |
})) |
| 77445673 Andy McCall ↶ |
2158 |
} |
| 2159 |
|
| dd6147dc Andy McCall ↶ |
2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 77445673 Andy McCall ↶ |
2166 |
pub fn working_copy_with_base(&self) -> Result<(Change, State, Snapshot)> { |
| 74b4a3c6 Andy McCall ↶ |
2167 |
match self.working_copy_with_base_unless_unchanged()? { |
| 2168 |
Some(all) => Ok(all), |
| 2169 |
None => { |
| 2170 |
let (_, wc) = self.working_copy()?; |
| 2171 |
let base = self.load_state_indexed(&wc.deps)?; |
| 2172 |
let (base, snapshot) = self.tree_base_for(&wc, base)?; |
| 2173 |
Ok((wc, base, snapshot)) |
| 2174 |
} |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
|
| 2179 |
|
| 2180 |
|
| 2181 |
pub fn working_copy_with_base_unless_unchanged( |
| 2182 |
&self, |
| 2183 |
) -> Result<Option<(Change, State, Snapshot)>> { |
| 77445673 Andy McCall ↶ |
2184 |
if self.status_by_manifest()?.is_some() { |
| 2185 |
let (_, wc) = self.working_copy()?; |
| 74b4a3c6 Andy McCall ↶ |
2186 |
if wc.tree_ops.is_empty() && wc.edits.is_empty() { |
| 2187 |
return Ok(None); |
| 2188 |
} |
| 77445673 Andy McCall ↶ |
2189 |
let base = self.load_state_indexed(&wc.deps)?; |
| dd6147dc Andy McCall ↶ |
2190 |
let (base, snapshot) = self.tree_base_for(&wc, base)?; |
| 74b4a3c6 Andy McCall ↶ |
2191 |
return Ok(Some((wc, base, snapshot))); |
| 77445673 Andy McCall ↶ |
2192 |
} |
| 2193 |
let snap = self.snapshot_status("oxy diff")?; |
| 74b4a3c6 Andy McCall ↶ |
2194 |
Ok(Some((snap.wc, snap.base, snap.base_snapshot))) |
| c6aa5bd9 Andy McCall ↶ |
2195 |
} |
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
pub fn log_for_path(&self, path: &str) -> Result<Vec<LogEntry>> { |
| e02bf0cd Andy McCall ↶ |
2200 |
let heads = self.bookmark_heads()?; |
| 2201 |
if self.is_bare() { |
| 2202 |
return self.log_for_path_in(path, &heads); |
| 2203 |
} |
| c6aa5bd9 Andy McCall ↶ |
2204 |
let entries = self.log()?; |
| 2205 |
let (_, wc) = self.working_copy()?; |
| 170bb436 Andy McCall ↶ |
2206 |
|
| 2207 |
|
| 2208 |
|
| c2e1d090 Andy McCall |
2209 |
let node_in = |mut state: State| -> Result<Option<HashSet<NodeId>>> { |
| 2210 |
Ok(nodes_at(&self.tree_view(&mut state)?, path)) |
| 2211 |
}; |
| 2212 |
let nodes = match node_in(self.load_state_indexed(&wc.deps)?)? { |
| 170bb436 Andy McCall ↶ |
2213 |
Some(n) => Some(n), |
| c2e1d090 Andy McCall |
2214 |
None => node_in(self.load_state_history(&heads)?)?, |
| 170bb436 Andy McCall ↶ |
2215 |
}; |
| c2e1d090 Andy McCall |
2216 |
let Some(nodes) = nodes else { |
| 75167672 Andy McCall |
2217 |
return fail(format!("{path} is not tracked")); |
| 2218 |
}; |
| c2e1d090 Andy McCall |
2219 |
Ok(self.entries_touching_up_to(entries, nodes, None)) |
| e02bf0cd Andy McCall ↶ |
2220 |
} |
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
pub fn log_for_path_in(&self, path: &str, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> { |
| e8d38e47 Andy McCall ↶ |
2225 |
self.log_for_path_of(&self.load_state_history(heads)?, path, None) |
| bc489b6c Andy McCall ↶ |
2226 |
} |
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
pub fn log_for_path_of( |
| 2232 |
&self, |
| 2233 |
state: &State, |
| 2234 |
path: &str, |
| 2235 |
limit: Option<usize>, |
| 2236 |
) -> Result<Vec<LogEntry>> { |
| c2e1d090 Andy McCall |
2237 |
let Some(nodes) = nodes_at(&state.view(), path) else { |
| 75167672 Andy McCall |
2238 |
return fail(format!("{path} is not in that state")); |
| 2239 |
}; |
| c2e1d090 Andy McCall |
2240 |
Ok(self.entries_touching_up_to(self.log_of(state), nodes, limit)) |
| bc489b6c Andy McCall ↶ |
2241 |
} |
| 2242 |
|
| 2243 |
fn entries_touching_up_to( |
| 2244 |
&self, |
| 2245 |
entries: Vec<LogEntry>, |
| c2e1d090 Andy McCall |
2246 |
wanted: HashSet<NodeId>, |
| bc489b6c Andy McCall ↶ |
2247 |
limit: Option<usize>, |
| 2248 |
) -> Vec<LogEntry> { |
| 170bb436 Andy McCall ↶ |
2249 |
let mut touched = self.touched(); |
| 2250 |
if limit.is_none() { |
| 2251 |
let hashes: Vec<ChangeHash> = entries.iter().map(|e| e.hash).collect(); |
| 2252 |
touched.fill(self, &hashes); |
| 2253 |
} |
| c6aa5bd9 Andy McCall ↶ |
2254 |
let mut out = Vec::new(); |
| 2255 |
for e in entries { |
| bc489b6c Andy McCall ↶ |
2256 |
if limit.is_some_and(|n| out.len() >= n) { |
| 2257 |
break; |
| 2258 |
} |
| 170bb436 Andy McCall ↶ |
2259 |
if touched.touches(self, &e.hash, &wanted) { |
| c6aa5bd9 Andy McCall ↶ |
2260 |
out.push(e); |
| 2261 |
} |
| 2262 |
} |
| 170bb436 Andy McCall ↶ |
2263 |
touched.save(); |
| e02bf0cd Andy McCall ↶ |
2264 |
out |
| c6aa5bd9 Andy McCall ↶ |
2265 |
} |
| c2e1d090 Andy McCall |
2266 |
} |
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
fn nodes_at(view: &crate::tree::TreeView, path: &str) -> Option<HashSet<NodeId>> { |
| 2271 |
let node = view.entries.get(path)?.node; |
| 2272 |
let mut out = HashSet::from([node]); |
| 2273 |
out.extend( |
| 2274 |
view.unified |
| 2275 |
.iter() |
| 2276 |
.filter(|(_, shown)| **shown == node) |
| 2277 |
.map(|(dup, _)| *dup), |
| 2278 |
); |
| 2279 |
Some(out) |
| c6aa5bd9 Andy McCall ↶ |
2280 |
} |
| 2281 |
|
| 2282 |
|
| 2283 |
pub fn diff_files_of(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<FileDiff> { |
| 2284 |
let by_node = base_snapshot.tree.by_node(); |
| 2285 |
let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 2286 |
for op in &change.tree_ops { |
| 75167672 Andy McCall |
2287 |
if let TreeOp::AddNode { |
| 2288 |
node, |
| 2289 |
parent, |
| 2290 |
name, |
| 2291 |
kind, |
| 2292 |
.. |
| 2293 |
} = op |
| 2294 |
{ |
| c6aa5bd9 Andy McCall ↶ |
2295 |
new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 2296 |
} |
| 2297 |
} |
| 75167672 Andy McCall |
2298 |
let path_of = |n: &NodeId| -> String { |
| 2299 |
path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8))) |
| 2300 |
}; |
| c6aa5bd9 Andy McCall ↶ |
2301 |
let mut files: BTreeMap<NodeId, FileDiff> = BTreeMap::new(); |
| 2302 |
let mut order: Vec<NodeId> = Vec::new(); |
| 2303 |
let mut entry = |files: &mut BTreeMap<NodeId, FileDiff>, n: NodeId, kind: &str| { |
| 2304 |
if let std::collections::btree_map::Entry::Vacant(v) = files.entry(n) { |
| 2305 |
order.push(n); |
| 75167672 Andy McCall |
2306 |
v.insert(FileDiff { |
| 2307 |
path: path_of(&n), |
| 2308 |
kind: kind.to_string(), |
| 2309 |
hunks: vec![], |
| 2310 |
from: None, |
| 2311 |
}); |
| c6aa5bd9 Andy McCall ↶ |
2312 |
} |
| 2313 |
}; |
| c2e1d090 Andy McCall |
2314 |
for op in change |
| 2315 |
.tree_ops |
| 2316 |
.iter() |
| 2317 |
.filter(|o| !is_unified_retirement(o, base_snapshot)) |
| 2318 |
{ |
| c6aa5bd9 Andy McCall ↶ |
2319 |
match op { |
| 2320 |
TreeOp::AddNode { node, kind, .. } => { |
| 2321 |
if *kind != NodeKind::Dir { |
| 2322 |
entry(&mut files, *node, "added"); |
| 2323 |
} |
| 2324 |
} |
| 2325 |
TreeOp::RemoveNode { node, .. } => { |
| 2326 |
entry(&mut files, *node, "removed"); |
| 2327 |
if let Some(e) = by_node.get(node) { |
| 2328 |
files.get_mut(node).unwrap().path = e.path.clone(); |
| 2329 |
} |
| 2330 |
} |
| 75167672 Andy McCall |
2331 |
TreeOp::Move { |
| 2332 |
node, parent, name, .. |
| 2333 |
} => { |
| c6aa5bd9 Andy McCall ↶ |
2334 |
entry(&mut files, *node, "renamed"); |
| 2335 |
let f = files.get_mut(node).unwrap(); |
| 2336 |
f.from = by_node.get(node).map(|e| e.path.clone()); |
| 75167672 Andy McCall |
2337 |
let dir = if parent.is_root() { |
| 2338 |
String::new() |
| 2339 |
} else { |
| 2340 |
path_of_node(parent, &by_node, &new_names).unwrap_or_default() |
| 2341 |
}; |
| 2342 |
f.path = if dir.is_empty() { |
| 2343 |
name.clone() |
| 2344 |
} else { |
| 2345 |
format!("{dir}/{name}") |
| 2346 |
}; |
| c6aa5bd9 Andy McCall ↶ |
2347 |
} |
| 2348 |
TreeOp::SetMode { node, .. } => entry(&mut files, *node, "mode"), |
| 2349 |
} |
| 2350 |
} |
| 2351 |
|
| 2352 |
let mut line_of: HashMap<NodeId, HashMap<(ChangeHash, u32), u32>> = HashMap::new(); |
| 2353 |
let mut lines_for = |n: &NodeId| -> HashMap<(ChangeHash, u32), u32> { |
| 75167672 Andy McCall |
2354 |
line_of |
| 2355 |
.entry(*n) |
| 2356 |
.or_insert_with(|| { |
| 2357 |
base.live_lines(n) |
| 2358 |
.into_iter() |
| 2359 |
.enumerate() |
| 2360 |
.map(|(i, (k, _))| (k, i as u32 + 1)) |
| 2361 |
.collect() |
| 2362 |
}) |
| 2363 |
.clone() |
| c6aa5bd9 Andy McCall ↶ |
2364 |
}; |
| 2365 |
for e in &change.edits { |
| 2366 |
let n = e.node(); |
| 2367 |
let is_new = new_names.contains_key(&n); |
| 2368 |
entry(&mut files, n, if is_new { "added" } else { "modified" }); |
| 2369 |
let f = files.get_mut(&n).unwrap(); |
| 2370 |
match e { |
| 2371 |
Edit::Insert { after, lines, .. } => { |
| 2372 |
let map = lines_for(&n); |
| 2373 |
let base_line = match after { |
| 2374 |
AtomRef::Start => 0, |
| 2375 |
AtomRef::End => map.len() as u32, |
| 75167672 Andy McCall |
2376 |
AtomRef::Atom { change, index } if change.is_zero() => { |
| 2377 |
f.hunks.last().map(|h| h.base_line).unwrap_or(0) |
| 2378 |
} |
| 2379 |
AtomRef::Atom { change, index } => { |
| 2380 |
map.get(&(*change, *index)).copied().unwrap_or(0) |
| 2381 |
} |
| c6aa5bd9 Andy McCall ↶ |
2382 |
}; |
| 75167672 Andy McCall |
2383 |
f.hunks.push(DiffHunk { |
| 2384 |
base_line, |
| 01a47cd7 Andy McCall ↶ |
2385 |
added: lines.to_vec(), |
| 75167672 Andy McCall |
2386 |
removed: vec![], |
| 2387 |
}); |
| c6aa5bd9 Andy McCall ↶ |
2388 |
} |
| 2389 |
Edit::Delete { atoms, .. } => { |
| 2390 |
let map = lines_for(&n); |
| 2391 |
let mut removed = Vec::new(); |
| 2392 |
for r in atoms { |
| 2393 |
for i in r.from..r.to { |
| 2394 |
let line = map.get(&(r.change, i)).copied().unwrap_or(0); |
| 75167672 Andy McCall |
2395 |
let text = base |
| 2396 |
.file(&n) |
| 2397 |
.and_then(|g| g.content(&(r.change, i))) |
| 2398 |
.unwrap_or(b"") |
| 2399 |
.to_vec(); |
| c6aa5bd9 Andy McCall ↶ |
2400 |
removed.push((line, text)); |
| 2401 |
} |
| 2402 |
} |
| 75167672 Andy McCall |
2403 |
let base_line = removed |
| 2404 |
.first() |
| 2405 |
.map(|(l, _)| l.saturating_sub(1)) |
| 2406 |
.unwrap_or(0); |
| 2407 |
f.hunks.push(DiffHunk { |
| 2408 |
base_line, |
| 2409 |
added: vec![], |
| 2410 |
removed, |
| 2411 |
}); |
| c6aa5bd9 Andy McCall ↶ |
2412 |
} |
| 2413 |
Edit::Replace { .. } => { |
| 2414 |
if !is_new { |
| 2415 |
f.kind = "replaced".into(); |
| 2416 |
} |
| 2417 |
} |
| 2418 |
} |
| 2419 |
} |
| 2420 |
order.into_iter().filter_map(|n| files.remove(&n)).collect() |
| 2421 |
} |
| 2422 |
|
| 98286406 Andy McCall ↶ |
2423 |
|
| dfb23a3a Andy McCall ↶ |
2424 |
|
| 2425 |
|
| 2426 |
fn seeded_node(seed: &[u8; 32], path: &str, kind: NodeKind) -> NodeId { |
| 2427 |
let mut h = blake3::Hasher::new(); |
| 2428 |
h.update(b"oxy node 1\0"); |
| 2429 |
h.update(seed); |
| 2430 |
h.update(&[match kind { |
| 2431 |
NodeKind::File => 1, |
| 2432 |
NodeKind::Dir => 2, |
| 2433 |
NodeKind::Symlink => 3, |
| 2434 |
NodeKind::Subrepo => 4, |
| 2435 |
}]); |
| 2436 |
h.update(path.as_bytes()); |
| 2437 |
let mut id = [0u8; 16]; |
| 2438 |
id.copy_from_slice(&h.finalize().as_bytes()[..16]); |
| 2439 |
if id == [0; 16] { |
| 2440 |
id[15] = 1; |
| 2441 |
} |
| 2442 |
NodeId(id) |
| 2443 |
} |
| 2444 |
|
| 98286406 Andy McCall ↶ |
2445 |
struct ChangeBuilder<'a> { |
| 2446 |
base: &'a State, |
| 2447 |
base_snapshot: &'a Snapshot, |
| 2448 |
tree_ops: Vec<TreeOp>, |
| 2449 |
edits: Vec<Edit>, |
| 2450 |
statuses: Vec<PathStatus>, |
| 2451 |
dir_nodes: HashMap<String, NodeId>, |
| 2452 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 2453 |
|
| 2454 |
contributors: BTreeSet<ChangeHash>, |
| 2455 |
resolves: bool, |
| cdb5beba Andy McCall ↶ |
2456 |
attrs: crate::attributes::Attributes, |
| b7f102fc Andy McCall ↶ |
2457 |
blobs: HashMap<String, ObjectRef>, |
| f7510b63 Andy McCall ↶ |
2458 |
|
| 2459 |
resolve: HashSet<String>, |
| dfb23a3a Andy McCall ↶ |
2460 |
|
| 2461 |
node_seed: Option<[u8; 32]>, |
| a87c7b9d Andy McCall ↶ |
2462 |
|
| 2463 |
|
| 2464 |
dead_dirs: Option<HashMap<(NodeId, String), (NodeId, crate::tree::OpKey)>>, |
| 360aa74a Andy McCall ↶ |
2465 |
|
| 2466 |
|
| 2467 |
diffs: HashMap<String, Vec<Edit>>, |
| 98286406 Andy McCall ↶ |
2468 |
} |
| 2469 |
|
| 2470 |
impl<'a> ChangeBuilder<'a> { |
| 75167672 Andy McCall |
2471 |
fn new( |
| 2472 |
base: &'a State, |
| 2473 |
base_snapshot: &'a Snapshot, |
| 2474 |
reuse: HashMap<String, (NodeId, NodeKind)>, |
| 2475 |
) -> Self { |
| 98286406 Andy McCall ↶ |
2476 |
let mut dir_nodes: HashMap<String, NodeId> = HashMap::new(); |
| 2477 |
dir_nodes.insert(String::new(), NodeId::ROOT); |
| e83354cd Andy McCall ↶ |
2478 |
for (p, e) in base_snapshot.tree.entries.iter() { |
| 2306a4d2 Andy McCall ↶ |
2479 |
if e.kind == NodeKind::Dir { |
| 2480 |
dir_nodes.insert(p.clone(), e.node); |
| 98286406 Andy McCall ↶ |
2481 |
} |
| 2482 |
} |
| 75167672 Andy McCall |
2483 |
ChangeBuilder { |
| 2484 |
base, |
| 2485 |
base_snapshot, |
| 2486 |
tree_ops: vec![], |
| 2487 |
edits: vec![], |
| 2488 |
statuses: vec![], |
| 2489 |
dir_nodes, |
| 2490 |
reuse, |
| 2491 |
contributors: BTreeSet::new(), |
| 2492 |
resolves: false, |
| 2493 |
attrs: crate::attributes::Attributes::default(), |
| 2494 |
blobs: HashMap::new(), |
| f7510b63 Andy McCall ↶ |
2495 |
resolve: HashSet::new(), |
| dfb23a3a Andy McCall ↶ |
2496 |
node_seed: None, |
| a87c7b9d Andy McCall ↶ |
2497 |
dead_dirs: None, |
| 360aa74a Andy McCall ↶ |
2498 |
diffs: HashMap::new(), |
| 75167672 Andy McCall |
2499 |
} |
| 98286406 Andy McCall ↶ |
2500 |
} |
| 2501 |
|
| 360aa74a Andy McCall ↶ |
2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
fn diff_in_parallel(&mut self, entries: &BTreeMap<String, FsEntry>) { |
| 2506 |
use rayon::prelude::*; |
| 2507 |
let files = &self.base_snapshot.files; |
| 2508 |
let edited: Vec<(&String, &FsEntry, &crate::state::FileState)> = entries |
| 2509 |
.iter() |
| 2510 |
.filter_map(|(p, e)| { |
| 2511 |
let x = files.get(p)?; |
| 2512 |
(x.kind == e.kind && !e.unchanged && !self.opaque_for(p, e)).then_some((p, e, x)) |
| 2513 |
}) |
| 2514 |
.collect(); |
| 2515 |
|
| 2516 |
if edited.len() < 16 { |
| 2517 |
return; |
| 2518 |
} |
| 2519 |
let this = &*self; |
| 2520 |
let attrs = &this.attrs; |
| 2521 |
let diffs: Vec<(String, Vec<Edit>)> = edited |
| 2522 |
.par_iter() |
| 2523 |
.filter_map(|(p, e, x)| { |
| 2524 |
let FileContent::Text { |
| 2525 |
bytes, |
| 2526 |
materialized, |
| 2527 |
} = &x.content |
| 2528 |
else { |
| 2529 |
return None; |
| 2530 |
}; |
| 2531 |
if materialized.is_conflicted() { |
| 2532 |
return None; |
| 2533 |
} |
| 2534 |
let disk_text = if attrs.lookup(p).eol.is_some() { |
| 2535 |
std::sync::Arc::new(crate::attributes::normalize_eol(&e.content)) |
| 2536 |
} else { |
| 2537 |
e.content.clone() |
| 2538 |
}; |
| 2539 |
if *bytes == *disk_text { |
| 2540 |
return None; |
| 2541 |
} |
| 2542 |
let keys = materialized.live_atoms(); |
| 2543 |
let lines = crate::diff::split_lines(bytes); |
| 2544 |
(keys.len() == lines.len()).then(|| { |
| 2545 |
let edits = crate::diff::text_edits_of(x.node, &keys, &lines, &disk_text); |
| 2546 |
((*p).clone(), edits) |
| 2547 |
}) |
| 2548 |
}) |
| 2549 |
.collect(); |
| 2550 |
self.diffs = diffs.into_iter().collect(); |
| 2551 |
} |
| 2552 |
|
| cdb5beba Andy McCall ↶ |
2553 |
fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self { |
| 2554 |
self.attrs = attrs; |
| 2555 |
self |
| 2556 |
} |
| 2557 |
|
| 2558 |
|
| 2559 |
|
| b7f102fc Andy McCall ↶ |
2560 |
|
| 75167672 Andy McCall |
2561 |
fn blob_of( |
| 2562 |
&mut self, |
| 2563 |
repo: &Repo, |
| 2564 |
path: &str, |
| 2565 |
e: &FsEntry, |
| 2566 |
nocompress: bool, |
| 2567 |
) -> Result<ObjectRef> { |
| b7f102fc Andy McCall ↶ |
2568 |
let b = match e.blob { |
| 2569 |
Some(b) => b, |
| 2570 |
None => repo.store_blob_for(&e.content, nocompress)?, |
| 2571 |
}; |
| 2572 |
self.blobs.insert(path.to_string(), b); |
| 2573 |
Ok(b) |
| 2574 |
} |
| 2575 |
|
| cdb5beba Andy McCall ↶ |
2576 |
fn opaque_for(&self, path: &str, e: &FsEntry) -> bool { |
| b7f102fc Andy McCall ↶ |
2577 |
if e.blob.is_some() { |
| 2578 |
return true; |
| 2579 |
} |
| 4dee277c Andy McCall ↶ |
2580 |
if e.kind == NodeKind::Symlink || e.kind == NodeKind::Subrepo { |
| cdb5beba Andy McCall ↶ |
2581 |
return true; |
| 2582 |
} |
| 2583 |
match self.attrs.lookup(path).binary { |
| 2584 |
Some(true) => true, |
| 2585 |
Some(false) => is_binary(&e.content), |
| 2586 |
None => is_binary(&e.content), |
| 2587 |
} |
| 2588 |
} |
| 2589 |
|
| 2590 |
|
| 40a40bb7 Andy McCall ↶ |
2591 |
fn stored_text( |
| 2592 |
&self, |
| 2593 |
path: &str, |
| 2594 |
content: &std::sync::Arc<Vec<u8>>, |
| 2595 |
) -> std::sync::Arc<Vec<u8>> { |
| 75167672 Andy McCall |
2596 |
if self.attrs.lookup(path).eol.is_some() { |
| 40a40bb7 Andy McCall ↶ |
2597 |
std::sync::Arc::new(crate::attributes::normalize_eol(content)) |
| 75167672 Andy McCall |
2598 |
} else { |
| 40a40bb7 Andy McCall ↶ |
2599 |
content.clone() |
| dfb23a3a Andy McCall ↶ |
2600 |
} |
| 2601 |
} |
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
fn new_node(&self, path: &str, kind: NodeKind) -> NodeId { |
| 2606 |
if let Some(n) = self.reused_node(path, kind) { |
| 2607 |
return n; |
| 75167672 Andy McCall |
2608 |
} |
| dfb23a3a Andy McCall ↶ |
2609 |
match &self.node_seed { |
| 2610 |
Some(seed) => seeded_node(seed, path, kind), |
| 2611 |
None => NodeId::random(), |
| 2612 |
} |
| cdb5beba Andy McCall ↶ |
2613 |
} |
| 2614 |
|
| 98286406 Andy McCall ↶ |
2615 |
fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> { |
| 75167672 Andy McCall |
2616 |
self.reuse |
| 2617 |
.get(path) |
| 2618 |
.filter(|(_, k)| *k == kind) |
| 2619 |
.map(|(n, _)| *n) |
| 98286406 Andy McCall ↶ |
2620 |
} |
| 2621 |
|
| a87c7b9d Andy McCall ↶ |
2622 |
|
| 2623 |
fn revivable_dir( |
| 2624 |
&mut self, |
| 2625 |
parent: NodeId, |
| 2626 |
name: &str, |
| 2627 |
) -> Option<(NodeId, crate::tree::OpKey)> { |
| 2628 |
let base = self.base; |
| 2629 |
self.dead_dirs |
| 2630 |
.get_or_insert_with(|| base.tree().dead_dirs()) |
| 2631 |
.get(&(parent, name.to_string())) |
| 2632 |
.copied() |
| 2633 |
} |
| 2634 |
|
| 98286406 Andy McCall ↶ |
2635 |
fn ensure_dir(&mut self, path: &str) -> NodeId { |
| 2636 |
if let Some(n) = self.dir_nodes.get(path) { |
| 2637 |
return *n; |
| 2638 |
} |
| 2639 |
let mut parent = NodeId::ROOT; |
| 2640 |
let mut prefix = String::new(); |
| 2641 |
for comp in path.split('/') { |
| 2642 |
if !prefix.is_empty() { |
| 2643 |
prefix.push('/'); |
| 2644 |
} |
| 2645 |
prefix.push_str(comp); |
| a87c7b9d Andy McCall ↶ |
2646 |
let known = self.dir_nodes.get(&prefix).copied(); |
| 2647 |
let revive = match known { |
| 2648 |
Some(_) => None, |
| 2649 |
None if self.node_seed.is_some() => self.revivable_dir(parent, comp), |
| 2650 |
None => None, |
| 2651 |
}; |
| 2652 |
parent = match (known, revive) { |
| 2653 |
(Some(n), _) => n, |
| 2654 |
(None, Some((node, old))) => { |
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 2659 |
self.tree_ops.push(TreeOp::Move { |
| 2660 |
node, |
| 2661 |
old: OpRef { |
| 2662 |
change: old.0, |
| 2663 |
index: old.1, |
| 2664 |
}, |
| 2665 |
parent, |
| 2666 |
name: comp.to_string(), |
| 2667 |
}); |
| 2668 |
self.dir_nodes.insert(prefix.clone(), node); |
| 2669 |
node |
| 2670 |
} |
| 2671 |
(None, None) => { |
| dfb23a3a Andy McCall ↶ |
2672 |
let node = self.new_node(&prefix, NodeKind::Dir); |
| 75167672 Andy McCall |
2673 |
self.tree_ops.push(TreeOp::AddNode { |
| 2674 |
node, |
| 2675 |
kind: NodeKind::Dir, |
| 2676 |
mode: Mode::default(), |
| 2677 |
parent, |
| 2678 |
name: comp.to_string(), |
| 2679 |
}); |
| 98286406 Andy McCall ↶ |
2680 |
self.dir_nodes.insert(prefix.clone(), node); |
| 2681 |
node |
| 2682 |
} |
| 2683 |
}; |
| 2684 |
} |
| 2685 |
parent |
| 2686 |
} |
| 2687 |
|
| 2688 |
fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> { |
| 2689 |
let (dir, name) = split_path(path); |
| 2690 |
let parent = self.ensure_dir(dir); |
| dfb23a3a Andy McCall ↶ |
2691 |
let node = self.new_node(path, e.kind); |
| cdb5beba Andy McCall ↶ |
2692 |
let opaque = self.opaque_for(path, e); |
| 2693 |
let nocompress = self.attrs.lookup(path).nocompress; |
| 75167672 Andy McCall |
2694 |
self.tree_ops.push(TreeOp::AddNode { |
| 2695 |
node, |
| 2696 |
kind: e.kind, |
| 2697 |
mode: Mode { |
| 2698 |
exec: e.exec, |
| 2699 |
opaque, |
| 2700 |
}, |
| 2701 |
parent, |
| 2702 |
name: name.to_string(), |
| 2703 |
}); |
| 98286406 Andy McCall ↶ |
2704 |
if opaque { |
| b7f102fc Andy McCall ↶ |
2705 |
let blob = self.blob_of(repo, path, e, nocompress)?; |
| 75167672 Andy McCall |
2706 |
self.edits.push(Edit::Replace { |
| 2707 |
node, |
| 2708 |
old: None, |
| 2709 |
new: Some(blob), |
| 2710 |
}); |
| 98286406 Andy McCall ↶ |
2711 |
} else if !e.content.is_empty() { |
| cdb5beba Andy McCall ↶ |
2712 |
let content = self.stored_text(path, &e.content); |
| 98286406 Andy McCall ↶ |
2713 |
self.edits.push(Edit::Insert { |
| 2714 |
node, |
| 2715 |
after: crate::objects::AtomRef::Start, |
| 2716 |
before: crate::objects::AtomRef::End, |
| 40a40bb7 Andy McCall ↶ |
2717 |
lines: crate::lines::Lines::split_shared(content), |
| 98286406 Andy McCall ↶ |
2718 |
}); |
| 2719 |
} |
| 75167672 Andy McCall |
2720 |
self.statuses.push(PathStatus { |
| 2721 |
path: path.to_string(), |
| 2722 |
status: ChangeStatus::Added, |
| 2723 |
}); |
| 98286406 Andy McCall ↶ |
2724 |
Ok(()) |
| 2725 |
} |
| 2726 |
|
| 6e723b81 Andy McCall ↶ |
2727 |
|
| 75167672 Andy McCall |
2728 |
fn move_path( |
| 2729 |
&mut self, |
| 2730 |
repo: &Repo, |
| 2731 |
old: &str, |
| 2732 |
new: &str, |
| 2733 |
e: &FsEntry, |
| 2734 |
expected: &crate::state::FileState, |
| 2735 |
) -> Result<()> { |
| e8e6e0ca Andy McCall ↶ |
2736 |
let placement = self.base_snapshot.tree.entries[old].placement; |
| 6e723b81 Andy McCall ↶ |
2737 |
let (dir, name) = split_path(new); |
| 2738 |
let parent = self.ensure_dir(dir); |
| 75167672 Andy McCall |
2739 |
self.tree_ops.push(TreeOp::Move { |
| 2740 |
node: expected.node, |
| e8e6e0ca Andy McCall ↶ |
2741 |
old: op_ref(placement), |
| 75167672 Andy McCall |
2742 |
parent, |
| 2743 |
name: name.to_string(), |
| 2744 |
}); |
| e8e6e0ca Andy McCall ↶ |
2745 |
for k in self.same_place(&expected.node, placement) { |
| 2746 |
self.tree_ops.push(TreeOp::RemoveNode { |
| 2747 |
node: expected.node, |
| 2748 |
placement: op_ref(k), |
| 2749 |
}); |
| 2750 |
} |
| 6e723b81 Andy McCall ↶ |
2751 |
self.update_path(repo, old, e, expected)?; |
| 2752 |
|
| 2753 |
self.statuses.retain(|s| s.path != old); |
| 75167672 Andy McCall |
2754 |
self.statuses.push(PathStatus { |
| 2755 |
path: new.to_string(), |
| 2756 |
status: ChangeStatus::Renamed, |
| 2757 |
}); |
| 6e723b81 Andy McCall ↶ |
2758 |
Ok(()) |
| a87c7b9d Andy McCall ↶ |
2759 |
} |
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
fn retire_stray_placements(&mut self) { |
| 2767 |
let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new(); |
| 2768 |
let mut placed: BTreeSet<NodeId> = BTreeSet::new(); |
| 2769 |
for op in &self.tree_ops { |
| 2770 |
match op { |
| 2771 |
TreeOp::Move { node, old, .. } => { |
| 2772 |
retired.insert((old.change, old.index)); |
| 2773 |
placed.insert(*node); |
| 2774 |
} |
| 2775 |
TreeOp::RemoveNode { placement, .. } => { |
| 2776 |
retired.insert((placement.change, placement.index)); |
| 2777 |
} |
| 2778 |
TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {} |
| 2779 |
} |
| 2780 |
} |
| 2781 |
|
| 2782 |
|
| 2783 |
let mut kept: HashMap<NodeId, (ChangeHash, u32)> = HashMap::new(); |
| 2784 |
for e in self.base_snapshot.tree.entries.values() { |
| 2785 |
if !retired.contains(&e.placement) { |
| 2786 |
kept.insert(e.node, e.placement); |
| 2787 |
} |
| 2788 |
} |
| 2789 |
|
| 2790 |
|
| 2791 |
let multi: BTreeSet<NodeId> = self |
| 2792 |
.base_snapshot |
| 2793 |
.tree |
| 2794 |
.conflicts |
| 2795 |
.iter() |
| 2796 |
.filter_map(|c| match c { |
| 2797 |
crate::tree::TreeConflict::MultiPlacement { node, .. } => Some(*node), |
| 2798 |
_ => None, |
| 2799 |
}) |
| 2800 |
.collect(); |
| 2801 |
let mut extra = Vec::new(); |
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
for c in &self.base_snapshot.tree.conflicts { |
| 2806 |
let crate::tree::TreeConflict::Orphan { node, .. } = c else { |
| 2807 |
continue; |
| 2808 |
}; |
| 2809 |
if kept.contains_key(node) || placed.contains(node) { |
| 2810 |
continue; |
| 2811 |
} |
| 2812 |
for (k, _) in self.base.tree().live_placements(node) { |
| 2813 |
if retired.insert(k) { |
| 2814 |
extra.push(TreeOp::RemoveNode { |
| 2815 |
node: *node, |
| 2816 |
placement: op_ref(k), |
| 2817 |
}); |
| 2818 |
} |
| 2819 |
} |
| 2820 |
} |
| 2821 |
for node in placed |
| 2822 |
.iter() |
| 2823 |
.chain(multi.iter().filter(|n| kept.contains_key(*n))) |
| 2824 |
{ |
| 2825 |
for (k, _) in self.base.tree().live_placements(node) { |
| 2826 |
if retired.contains(&k) || kept.get(node) == Some(&k) { |
| 2827 |
continue; |
| 2828 |
} |
| 2829 |
retired.insert(k); |
| 2830 |
extra.push(TreeOp::RemoveNode { |
| 2831 |
node: *node, |
| 2832 |
placement: op_ref(k), |
| 2833 |
}); |
| 2834 |
} |
| 2835 |
} |
| 2836 |
if !extra.is_empty() { |
| 2837 |
self.resolves = true; |
| 2838 |
self.tree_ops.extend(extra); |
| 2839 |
} |
| 6e723b81 Andy McCall ↶ |
2840 |
} |
| 2841 |
|
| c2e1d090 Andy McCall |
2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
fn retire_unified(&mut self) { |
| 2848 |
let unified = &self.base_snapshot.tree.unified; |
| 2849 |
if unified.is_empty() { |
| 2850 |
return; |
| 2851 |
} |
| 2852 |
let touched: HashSet<NodeId> = self |
| 2853 |
.tree_ops |
| 2854 |
.iter() |
| 2855 |
.map(|o| o.node()) |
| 2856 |
.chain(self.edits.iter().map(|e| e.node())) |
| 2857 |
.collect(); |
| 2858 |
let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new(); |
| 2859 |
for op in &self.tree_ops { |
| 2860 |
match op { |
| 2861 |
TreeOp::Move { old, .. } => { |
| 2862 |
retired.insert((old.change, old.index)); |
| 2863 |
} |
| 2864 |
TreeOp::RemoveNode { placement, .. } => { |
| 2865 |
retired.insert((placement.change, placement.index)); |
| 2866 |
} |
| 2867 |
TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {} |
| 2868 |
} |
| 2869 |
} |
| 2870 |
let mut extra = Vec::new(); |
| 2871 |
for (dup, shown) in unified { |
| 2872 |
if !touched.contains(shown) { |
| 2873 |
continue; |
| 2874 |
} |
| 2875 |
for (k, _) in self.base.tree().live_placements(dup) { |
| 2876 |
if retired.insert(k) { |
| 2877 |
extra.push(TreeOp::RemoveNode { |
| 2878 |
node: *dup, |
| 2879 |
placement: op_ref(k), |
| 2880 |
}); |
| 2881 |
} |
| 2882 |
} |
| 2883 |
} |
| 2884 |
self.tree_ops.extend(extra); |
| 2885 |
} |
| 2886 |
|
| e8e6e0ca Andy McCall ↶ |
2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
fn same_place(&self, node: &NodeId, k: (ChangeHash, u32)) -> Vec<(ChangeHash, u32)> { |
| 2893 |
let tree = self.base.tree(); |
| 2894 |
let Some(shown) = tree.placement(&k) else { |
| 2895 |
return Vec::new(); |
| 2896 |
}; |
| 2897 |
tree.live_placements(node) |
| 2898 |
.into_iter() |
| 2899 |
.filter(|(o, p)| *o != k && p.parent == shown.parent && p.name == shown.name) |
| 2900 |
.map(|(o, _)| o) |
| 2901 |
.collect() |
| 2902 |
} |
| 2903 |
|
| 98286406 Andy McCall ↶ |
2904 |
fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) { |
| 2905 |
let entry = &self.base_snapshot.tree.entries[path]; |
| e8e6e0ca Andy McCall ↶ |
2906 |
let placement = entry.placement; |
| 75167672 Andy McCall |
2907 |
self.tree_ops.push(TreeOp::RemoveNode { |
| 2908 |
node: expected.node, |
| e8e6e0ca Andy McCall ↶ |
2909 |
placement: op_ref(placement), |
| 75167672 Andy McCall |
2910 |
}); |
| e8e6e0ca Andy McCall ↶ |
2911 |
for k in self.same_place(&expected.node, placement) { |
| 2912 |
self.tree_ops.push(TreeOp::RemoveNode { |
| 2913 |
node: expected.node, |
| 2914 |
placement: op_ref(k), |
| 2915 |
}); |
| 2916 |
} |
| 98286406 Andy McCall ↶ |
2917 |
if path.contains(".oxy-conflict-") || expected.is_conflicted() { |
| 2918 |
self.resolves = true; |
| 2919 |
} |
| 75167672 Andy McCall |
2920 |
self.statuses.push(PathStatus { |
| 2921 |
path: path.to_string(), |
| 2922 |
status: ChangeStatus::Removed, |
| 2923 |
}); |
| 98286406 Andy McCall ↶ |
2924 |
} |
| 2925 |
|
| 75167672 Andy McCall |
2926 |
fn update_path( |
| 2927 |
&mut self, |
| 2928 |
repo: &Repo, |
| 2929 |
path: &str, |
| 2930 |
e: &FsEntry, |
| 2931 |
expected: &crate::state::FileState, |
| 2932 |
) -> Result<()> { |
| 98286406 Andy McCall ↶ |
2933 |
if expected.kind != e.kind { |
| 2934 |
self.remove_path(path, expected); |
| 2935 |
self.statuses.pop(); |
| 2936 |
self.add_path(repo, path, e)?; |
| 2937 |
self.statuses.pop(); |
| 75167672 Andy McCall |
2938 |
self.statuses.push(PathStatus { |
| 2939 |
path: path.to_string(), |
| 2940 |
status: ChangeStatus::Modified, |
| 2941 |
}); |
| 98286406 Andy McCall ↶ |
2942 |
return Ok(()); |
| 2943 |
} |
| 2944 |
let entry = &self.base_snapshot.tree.entries[path]; |
| 2945 |
let node = expected.node; |
| 2946 |
let mode_atom = entry.mode_atom.map(op_ref); |
| cc7a0056 Andy McCall ↶ |
2947 |
if e.unchanged { |
| 2948 |
|
| 2949 |
|
| 2950 |
if e.exec != expected.mode.exec { |
| 2951 |
if let Some(old) = mode_atom { |
| 2952 |
self.tree_ops.push(TreeOp::SetMode { |
| 2953 |
node, |
| 2954 |
old, |
| 2955 |
mode: Mode { |
| 2956 |
exec: e.exec, |
| 2957 |
opaque: false, |
| 2958 |
}, |
| 2959 |
}); |
| 2960 |
self.statuses.push(PathStatus { |
| 2961 |
path: path.to_string(), |
| 2962 |
status: ChangeStatus::ModeChanged, |
| 2963 |
}); |
| 2964 |
} |
| 2965 |
} |
| 2966 |
return Ok(()); |
| 2967 |
} |
| 98286406 Andy McCall ↶ |
2968 |
let mut status: Option<ChangeStatus> = None; |
| cdb5beba Andy McCall ↶ |
2969 |
let wants_opaque = self.opaque_for(path, e); |
| 2970 |
let nocompress = self.attrs.lookup(path).nocompress; |
| 2971 |
let disk_text = self.stored_text(path, &e.content); |
| 98286406 Andy McCall ↶ |
2972 |
match &expected.content { |
| 2973 |
FileContent::Dir => {} |
| 75167672 Andy McCall |
2974 |
FileContent::Text { |
| 2975 |
bytes, |
| 2976 |
materialized, |
| 2977 |
} => { |
| 98286406 Andy McCall ↶ |
2978 |
if materialized.is_conflicted() { |
| 40a40bb7 Andy McCall ↶ |
2979 |
if bytes == &*disk_text { |
| 98286406 Andy McCall ↶ |
2980 |
return Ok(()); |
| 2981 |
} |
| 3652104a Andy McCall ↶ |
2982 |
let regions = self.base.conflict_regions(&node); |
| 2983 |
let plain: Vec<&[u8]> = |
| 2984 |
regions.plain.iter().map(|(_, l)| l.as_slice()).collect(); |
| 2985 |
if has_own_conflict_markers(&e.content, bytes, &plain) { |
| 75167672 Andy McCall |
2986 |
self.statuses.push(PathStatus { |
| 2987 |
path: path.to_string(), |
| 2988 |
status: ChangeStatus::ConflictEdited, |
| 2989 |
}); |
| 98286406 Andy McCall ↶ |
2990 |
return Ok(()); |
| 2991 |
} |
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| cdb5beba Andy McCall ↶ |
2996 |
let mut edits = text_edits(node, ®ions.plain, &disk_text); |
| 98286406 Andy McCall ↶ |
2997 |
if !regions.region_atoms.is_empty() { |
| 75167672 Andy McCall |
2998 |
edits.insert( |
| 2999 |
0, |
| 3000 |
Edit::Delete { |
| 3001 |
node, |
| 3002 |
atoms: ranges(®ions.region_atoms), |
| 3003 |
}, |
| 3004 |
); |
| 98286406 Andy McCall ↶ |
3005 |
} |
| 3006 |
self.edits.extend(edits); |
| 3007 |
self.contributors.extend(regions.contributors); |
| 3008 |
self.resolves = true; |
| 3009 |
if e.exec != expected.mode.exec { |
| 3010 |
if let Some(old) = mode_atom { |
| 75167672 Andy McCall |
3011 |
self.tree_ops.push(TreeOp::SetMode { |
| 3012 |
node, |
| 3013 |
old, |
| 3014 |
mode: Mode { |
| 3015 |
exec: e.exec, |
| 3016 |
opaque: false, |
| 3017 |
}, |
| 3018 |
}); |
| 98286406 Andy McCall ↶ |
3019 |
} |
| 3020 |
} |
| 75167672 Andy McCall |
3021 |
self.statuses.push(PathStatus { |
| 3022 |
path: path.to_string(), |
| 3023 |
status: ChangeStatus::Resolved, |
| 3024 |
}); |
| 98286406 Andy McCall ↶ |
3025 |
return Ok(()); |
| 3026 |
} |
| cdb5beba Andy McCall ↶ |
3027 |
if wants_opaque { |
| 3028 |
|
| 3029 |
if let Some(old) = mode_atom { |
| 75167672 Andy McCall |
3030 |
self.tree_ops.push(TreeOp::SetMode { |
| 3031 |
node, |
| 3032 |
old, |
| 3033 |
mode: Mode { |
| 3034 |
exec: e.exec, |
| 3035 |
opaque: true, |
| 3036 |
}, |
| 3037 |
}); |
| cdb5beba Andy McCall ↶ |
3038 |
} |
| 3039 |
let live: Vec<_> = repo_live_keys(self.base, &node); |
| 3040 |
if !live.is_empty() { |
| 75167672 Andy McCall |
3041 |
self.edits.push(Edit::Delete { |
| 3042 |
node, |
| 3043 |
atoms: ranges(&live), |
| 3044 |
}); |
| cdb5beba Andy McCall ↶ |
3045 |
} |
| b7f102fc Andy McCall ↶ |
3046 |
let blob = self.blob_of(repo, path, e, nocompress)?; |
| 75167672 Andy McCall |
3047 |
self.edits.push(Edit::Replace { |
| 3048 |
node, |
| 3049 |
old: None, |
| 3050 |
new: Some(blob), |
| 3051 |
}); |
| cdb5beba Andy McCall ↶ |
3052 |
status = Some(ChangeStatus::Modified); |
| 40a40bb7 Andy McCall ↶ |
3053 |
} else if bytes != &*disk_text { |
| 2306a4d2 Andy McCall ↶ |
3054 |
let mut laps = crate::timing::Laps::start(); |
| 3055 |
|
| 3056 |
|
| 360aa74a Andy McCall ↶ |
3057 |
let edits = match self.diffs.remove(path) { |
| 3058 |
Some(edits) => edits, |
| 3059 |
None => { |
| 3060 |
let keys = materialized.live_atoms(); |
| 3061 |
let lines = crate::diff::split_lines(bytes); |
| 3062 |
if keys.len() == lines.len() { |
| 3063 |
crate::diff::text_edits_of(node, &keys, &lines, &disk_text) |
| 3064 |
} else { |
| 3065 |
text_edits(node, &self.base.live_lines(&node), &disk_text) |
| 3066 |
} |
| 3067 |
} |
| 2306a4d2 Andy McCall ↶ |
3068 |
}; |
| 3069 |
laps.lap("build: text diff"); |
| cdb5beba Andy McCall ↶ |
3070 |
if !edits.is_empty() { |
| 3071 |
self.edits.extend(edits); |
| 98286406 Andy McCall ↶ |
3072 |
status = Some(ChangeStatus::Modified); |
| 3073 |
} |
| 3074 |
} |
| cdb5beba Andy McCall ↶ |
3075 |
let switched_to_opaque = wants_opaque; |
| 98286406 Andy McCall ↶ |
3076 |
if !switched_to_opaque && e.exec != expected.mode.exec { |
| 3077 |
if let Some(old) = mode_atom { |
| 75167672 Andy McCall |
3078 |
self.tree_ops.push(TreeOp::SetMode { |
| 3079 |
node, |
| 3080 |
old, |
| 3081 |
mode: Mode { |
| 3082 |
exec: e.exec, |
| 3083 |
opaque: false, |
| 3084 |
}, |
| 3085 |
}); |
| 98286406 Andy McCall ↶ |
3086 |
status.get_or_insert(ChangeStatus::ModeChanged); |
| 3087 |
} |
| 3088 |
} |
| 3089 |
} |
| 3090 |
FileContent::Opaque { blobs } => { |
| b7f102fc Andy McCall ↶ |
3091 |
let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)); |
| 3092 |
if e.kind == NodeKind::File && wants_opaque { |
| 3093 |
|
| 3094 |
self.blobs.insert(path.to_string(), new_ref); |
| f7510b63 Andy McCall ↶ |
3095 |
} |
| 3096 |
if blobs.len() > 1 && self.resolve.contains(path) && e.kind == expected.kind { |
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
let chosen = self.blob_of(repo, path, e, nocompress)?; |
| 3101 |
for b in blobs { |
| 3102 |
self.edits.push(Edit::Replace { |
| 3103 |
node, |
| 3104 |
old: Some(*b), |
| 3105 |
new: Some(chosen), |
| 3106 |
}); |
| 3107 |
} |
| 3108 |
self.contributors |
| 3109 |
.extend(self.base.blob_contributors(&node, blobs)); |
| 3110 |
self.resolves = true; |
| 3111 |
self.statuses.push(PathStatus { |
| 3112 |
path: path.to_string(), |
| 3113 |
status: ChangeStatus::Resolved, |
| 3114 |
}); |
| 3115 |
return Ok(()); |
| b7f102fc Andy McCall ↶ |
3116 |
} |
| 98286406 Andy McCall ↶ |
3117 |
if blobs.len() > 1 { |
| 3118 |
if !blobs.contains(&new_ref) { |
| 75167672 Andy McCall |
3119 |
self.statuses.push(PathStatus { |
| 3120 |
path: path.to_string(), |
| 3121 |
status: ChangeStatus::ConflictEdited, |
| 3122 |
}); |
| 98286406 Andy McCall ↶ |
3123 |
} |
| 3124 |
return Ok(()); |
| 3125 |
} |
| 3126 |
let current = blobs.first().copied(); |
| 3127 |
if current != Some(new_ref) { |
| cdb5beba Andy McCall ↶ |
3128 |
if e.kind == NodeKind::File && !wants_opaque { |
| 98286406 Andy McCall ↶ |
3129 |
|
| 3130 |
if let Some(old) = mode_atom { |
| 75167672 Andy McCall |
3131 |
self.tree_ops.push(TreeOp::SetMode { |
| 3132 |
node, |
| 3133 |
old, |
| 3134 |
mode: Mode { |
| 3135 |
exec: e.exec, |
| 3136 |
opaque: false, |
| 3137 |
}, |
| 3138 |
}); |
| 98286406 Andy McCall ↶ |
3139 |
} |
| 75167672 Andy McCall |
3140 |
self.edits.push(Edit::Replace { |
| 3141 |
node, |
| 3142 |
old: current, |
| 3143 |
new: None, |
| 3144 |
}); |
| 98286406 Andy McCall ↶ |
3145 |
if !e.content.is_empty() { |
| 3146 |
self.edits.push(Edit::Insert { |
| 3147 |
node, |
| 3148 |
after: crate::objects::AtomRef::Start, |
| 3149 |
before: crate::objects::AtomRef::End, |
| 01a47cd7 Andy McCall ↶ |
3150 |
lines: crate::lines::Lines::split(&e.content), |
| 98286406 Andy McCall ↶ |
3151 |
}); |
| 3152 |
} |
| 3153 |
} else { |
| b7f102fc Andy McCall ↶ |
3154 |
let blob = self.blob_of(repo, path, e, nocompress)?; |
| 75167672 Andy McCall |
3155 |
self.edits.push(Edit::Replace { |
| 3156 |
node, |
| 3157 |
old: current, |
| 3158 |
new: Some(blob), |
| 3159 |
}); |
| 6eae06b6 Andy McCall ↶ |
3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
if e.kind == NodeKind::File && e.exec != expected.mode.exec { |
| 3164 |
if let Some(old) = mode_atom { |
| 3165 |
self.tree_ops.push(TreeOp::SetMode { |
| 3166 |
node, |
| 3167 |
old, |
| 3168 |
mode: Mode { |
| 3169 |
exec: e.exec, |
| 3170 |
opaque: true, |
| 3171 |
}, |
| 3172 |
}); |
| 3173 |
} |
| 3174 |
} |
| 98286406 Andy McCall ↶ |
3175 |
} |
| 3176 |
status = Some(ChangeStatus::Modified); |
| 3177 |
} else if e.kind == NodeKind::File && e.exec != expected.mode.exec { |
| 3178 |
if let Some(old) = mode_atom { |
| 75167672 Andy McCall |
3179 |
self.tree_ops.push(TreeOp::SetMode { |
| 3180 |
node, |
| 3181 |
old, |
| 3182 |
mode: Mode { |
| 3183 |
exec: e.exec, |
| 3184 |
opaque: true, |
| 3185 |
}, |
| 3186 |
}); |
| 98286406 Andy McCall ↶ |
3187 |
status = Some(ChangeStatus::ModeChanged); |
| 3188 |
} |
| 3189 |
} |
| 3190 |
} |
| 3191 |
} |
| 3192 |
if let Some(s) = status { |
| 75167672 Andy McCall |
3193 |
self.statuses.push(PathStatus { |
| 3194 |
path: path.to_string(), |
| 3195 |
status: s, |
| 3196 |
}); |
| 98286406 Andy McCall ↶ |
3197 |
} |
| 3198 |
Ok(()) |
| 3199 |
} |
| 3200 |
} |
| 3201 |
|
| 3202 |
fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> { |
| 3203 |
base.live_lines(node).into_iter().map(|(k, _)| k).collect() |
| 3204 |
} |
| 3205 |
|
| 3206 |
|
| 3207 |
|
| 3208 |
pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> { |
| 3209 |
let mut out = BTreeSet::new(); |
| 3210 |
let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| { |
| 3211 |
if !r.change.is_zero() { |
| 3212 |
out.insert(r.change); |
| 3213 |
} |
| 3214 |
}; |
| 3215 |
let mut nodes: Vec<NodeId> = Vec::new(); |
| 3216 |
for op in &change.tree_ops { |
| 3217 |
match op { |
| 3218 |
TreeOp::AddNode { parent, .. } => nodes.push(*parent), |
| 3219 |
TreeOp::RemoveNode { node, placement } => { |
| 3220 |
nodes.push(*node); |
| 3221 |
opref(placement, &mut out); |
| 3222 |
} |
| 75167672 Andy McCall |
3223 |
TreeOp::Move { |
| 3224 |
node, old, parent, .. |
| 3225 |
} => { |
| 98286406 Andy McCall ↶ |
3226 |
nodes.push(*node); |
| 3227 |
nodes.push(*parent); |
| 3228 |
opref(old, &mut out); |
| 3229 |
} |
| 3230 |
TreeOp::SetMode { node, old, .. } => { |
| 3231 |
nodes.push(*node); |
| 3232 |
opref(old, &mut out); |
| 3233 |
} |
| 3234 |
} |
| 3235 |
} |
| 3236 |
for e in &change.edits { |
| 3237 |
nodes.push(e.node()); |
| 3238 |
match e { |
| 3239 |
Edit::Insert { after, before, .. } => { |
| 3240 |
for r in [after, before] { |
| 3241 |
if let crate::objects::AtomRef::Atom { change, .. } = r { |
| 3242 |
if !change.is_zero() { |
| 3243 |
out.insert(*change); |
| 3244 |
} |
| 3245 |
} |
| 3246 |
} |
| 3247 |
} |
| 3248 |
Edit::Delete { atoms, .. } => { |
| 62794ecc Andy McCall ↶ |
3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
|
| 3253 |
let graph = base.file(&e.node()); |
| 98286406 Andy McCall ↶ |
3254 |
for r in atoms { |
| 3255 |
if !r.change.is_zero() { |
| 3256 |
out.insert(r.change); |
| 3257 |
} |
| 62794ecc Andy McCall ↶ |
3258 |
if let Some(g) = graph { |
| 3259 |
for i in r.from..r.to { |
| 3260 |
out.extend( |
| 3261 |
g.live_successor_changes(&(r.change, i)) |
| 3262 |
.into_iter() |
| 3263 |
.filter(|c| !c.is_zero()), |
| 3264 |
); |
| 3265 |
} |
| 3266 |
} |
| 98286406 Andy McCall ↶ |
3267 |
} |
| 3268 |
} |
| 62794ecc Andy McCall ↶ |
3269 |
|
| 3270 |
|
| 3271 |
Edit::Replace { |
| 3272 |
node, |
| 3273 |
old: Some(old), |
| 3274 |
.. |
| 3275 |
} => { |
| 3276 |
out.extend( |
| 3277 |
base.live_replaces(node) |
| 3278 |
.into_iter() |
| 3279 |
.filter(|(c, b)| b == old && !c.is_zero()) |
| 3280 |
.map(|(c, _)| c), |
| 3281 |
); |
| 3282 |
} |
| 98286406 Andy McCall ↶ |
3283 |
Edit::Replace { .. } => {} |
| 3284 |
} |
| 3285 |
} |
| 3286 |
for n in nodes { |
| 3287 |
if let Some(h) = base.tree().introducer(&n) { |
| 3288 |
out.insert(h); |
| 3289 |
} |
| 3290 |
} |
| 3291 |
out |
| 3292 |
} |
| 3293 |
|
| 3294 |
|
| 75167672 Andy McCall |
3295 |
pub fn added_node_paths( |
| 3296 |
change: &Change, |
| 3297 |
base_snapshot: &Snapshot, |
| 3298 |
) -> HashMap<String, (NodeId, NodeKind)> { |
| 98286406 Andy McCall ↶ |
3299 |
let by_node = base_snapshot.tree.by_node(); |
| 3300 |
let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 3301 |
for op in &change.tree_ops { |
| 75167672 Andy McCall |
3302 |
if let TreeOp::AddNode { |
| 3303 |
node, |
| 3304 |
parent, |
| 3305 |
name, |
| 3306 |
kind, |
| 3307 |
.. |
| 3308 |
} = op |
| 3309 |
{ |
| 98286406 Andy McCall ↶ |
3310 |
new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 3311 |
} |
| 3312 |
} |
| 3313 |
let mut out = HashMap::new(); |
| 3314 |
for (node, (_, _, kind)) in &new_names { |
| 3315 |
if let Some(path) = path_of_node(node, &by_node, &new_names) { |
| 3316 |
out.insert(path, (*node, *kind)); |
| 3317 |
} |
| 3318 |
} |
| 3319 |
out |
| 3320 |
} |
| 3321 |
|
| c2e1d090 Andy McCall |
3322 |
|
| 3323 |
|
| 3324 |
fn is_unified_retirement(op: &TreeOp, base_snapshot: &Snapshot) -> bool { |
| 3325 |
matches!(op, TreeOp::RemoveNode { node, .. } if base_snapshot.tree.unified.contains_key(node)) |
| 3326 |
} |
| 3327 |
|
| 98286406 Andy McCall ↶ |
3328 |
fn path_of_node( |
| 3329 |
n: &NodeId, |
| 3330 |
by_node: &HashMap<NodeId, &crate::tree::TreeEntry>, |
| 3331 |
new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>, |
| 3332 |
) -> Option<String> { |
| 3333 |
if let Some(e) = by_node.get(n) { |
| 3334 |
return Some(e.path.clone()); |
| 3335 |
} |
| 3336 |
let mut parts = Vec::new(); |
| 3337 |
let mut cur = *n; |
| 3338 |
loop { |
| 3339 |
if cur.is_root() { |
| 3340 |
break; |
| 3341 |
} |
| 3342 |
if let Some(e) = by_node.get(&cur) { |
| 3343 |
if !e.path.is_empty() { |
| 3344 |
parts.push(e.path.clone()); |
| 3345 |
} |
| 3346 |
break; |
| 3347 |
} |
| 3348 |
let (parent, name, _) = new_names.get(&cur)?; |
| 3349 |
parts.push(name.clone()); |
| 3350 |
cur = *parent; |
| 3351 |
} |
| 3352 |
parts.reverse(); |
| 3353 |
Some(parts.join("/")) |
| 3354 |
} |
| 3355 |
|
| 3356 |
|
| 3357 |
pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String { |
| 3358 |
let by_node = base_snapshot.tree.by_node(); |
| 3359 |
let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 3360 |
for op in &change.tree_ops { |
| 75167672 Andy McCall |
3361 |
if let TreeOp::AddNode { |
| 3362 |
node, |
| 3363 |
parent, |
| 3364 |
name, |
| 3365 |
kind, |
| 3366 |
.. |
| 3367 |
} = op |
| 3368 |
{ |
| 98286406 Andy McCall ↶ |
3369 |
new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 3370 |
} |
| 3371 |
} |
| 75167672 Andy McCall |
3372 |
let path_of = |n: &NodeId| -> String { |
| 3373 |
path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8))) |
| 3374 |
}; |
| 98286406 Andy McCall ↶ |
3375 |
|
| 3376 |
let mut out = String::new(); |
| c2e1d090 Andy McCall |
3377 |
for op in change |
| 3378 |
.tree_ops |
| 3379 |
.iter() |
| 3380 |
.filter(|o| !is_unified_retirement(o, base_snapshot)) |
| 3381 |
{ |
| 98286406 Andy McCall ↶ |
3382 |
match op { |
| 75167672 Andy McCall |
3383 |
TreeOp::AddNode { |
| 3384 |
node, kind, mode, .. |
| 3385 |
} => { |
| 3386 |
out.push_str(&format!( |
| 3387 |
"A {} ({:?}{}{})\n", |
| 3388 |
path_of(node), |
| 3389 |
kind, |
| 3390 |
if mode.exec { ", exec" } else { "" }, |
| 3391 |
if mode.opaque { ", opaque" } else { "" } |
| 3392 |
)); |
| 98286406 Andy McCall ↶ |
3393 |
} |
| 3394 |
TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))), |
| 0912a033 Andy McCall ↶ |
3395 |
TreeOp::Move { |
| 3396 |
node, parent, name, .. |
| 3397 |
} => { |
| 3398 |
let to = if parent.is_root() { |
| 3399 |
name.clone() |
| 3400 |
} else { |
| 3401 |
format!("{}/{name}", path_of(parent)) |
| 3402 |
}; |
| 3403 |
out.push_str(&format!("R {} -> {to}\n", path_of(node))) |
| 75167672 Andy McCall |
3404 |
} |
| 98286406 Andy McCall ↶ |
3405 |
TreeOp::SetMode { node, mode, .. } => { |
| 75167672 Andy McCall |
3406 |
out.push_str(&format!( |
| 3407 |
"M {} mode: exec={} opaque={}\n", |
| 3408 |
path_of(node), |
| 3409 |
mode.exec, |
| 3410 |
mode.opaque |
| 3411 |
)); |
| 98286406 Andy McCall ↶ |
3412 |
} |
| 0912a033 Andy McCall ↶ |
3413 |
} |
| 3414 |
} |
| 3415 |
|
| 3416 |
|
| 3417 |
let with_op: HashSet<NodeId> = change.tree_ops.iter().map(|o| o.node()).collect(); |
| 3418 |
let mut listed: HashSet<NodeId> = HashSet::new(); |
| 3419 |
for e in &change.edits { |
| 3420 |
let n = e.node(); |
| 3421 |
if !with_op.contains(&n) && listed.insert(n) { |
| 3422 |
out.push_str(&format!("M {}\n", path_of(&n))); |
| 98286406 Andy McCall ↶ |
3423 |
} |
| 3424 |
} |
| 3425 |
let mut last_node: Option<NodeId> = None; |
| 3426 |
for e in &change.edits { |
| 3427 |
let n = e.node(); |
| 3428 |
if last_node != Some(n) { |
| 3429 |
out.push_str(&format!("--- {}\n", path_of(&n))); |
| 3430 |
last_node = Some(n); |
| 3431 |
} |
| 3432 |
match e { |
| 3433 |
Edit::Insert { lines, .. } => { |
| 01a47cd7 Andy McCall ↶ |
3434 |
for l in lines.iter() { |
| 98286406 Andy McCall ↶ |
3435 |
out.push('+'); |
| 3436 |
out.push_str(&String::from_utf8_lossy(l)); |
| 3437 |
if !l.ends_with(b"\n") { |
| 3438 |
out.push('\n'); |
| 3439 |
} |
| 3440 |
} |
| 3441 |
} |
| 3442 |
Edit::Delete { atoms, .. } => { |
| 3443 |
for r in atoms { |
| 3444 |
for i in r.from..r.to { |
| 75167672 Andy McCall |
3445 |
let content = base |
| 3446 |
.file(&n) |
| 3447 |
.and_then(|g| g.content(&(r.change, i))) |
| 3448 |
.unwrap_or(b"<unknown>\n"); |
| 98286406 Andy McCall ↶ |
3449 |
out.push('-'); |
| 3450 |
out.push_str(&String::from_utf8_lossy(content)); |
| 3451 |
if !content.ends_with(b"\n") { |
| 3452 |
out.push('\n'); |
| 3453 |
} |
| 3454 |
} |
| 3455 |
} |
| 3456 |
} |
| 3457 |
Edit::Replace { old, new, .. } => { |
| 3458 |
out.push_str(&format!( |
| 3459 |
" opaque content {} -> {}\n", |
| 3460 |
old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()), |
| 3461 |
new.map(|o| o.short(12)).unwrap_or_else(|| "none".into()) |
| 3462 |
)); |
| 3463 |
} |
| 3464 |
} |
| 3465 |
} |
| 3466 |
out |
| 3467 |
} |
| 6e723b81 Andy McCall ↶ |
3468 |
|
| 3469 |
|
| 3470 |
|
| 3471 |
|
| 3472 |
const RENAME_THRESHOLD: u32 = 50; |
| 3473 |
const RENAME_LIMIT: usize = 10_000; |
| 2306a4d2 Andy McCall ↶ |
3474 |
|
| 23ff1288 Andy McCall ↶ |
3475 |
|
| 3476 |
|
| 3477 |
|
| 3478 |
#[derive(Default)] |
| 3479 |
struct Scope<'a> { |
| 3480 |
seed: Option<[u8; 32]>, |
| 2306a4d2 Andy McCall ↶ |
3481 |
dir_needed: Option<&'a dyn Fn(&str) -> bool>, |
| 3482 |
} |
| 6e723b81 Andy McCall ↶ |
3483 |
|
| 75167672 Andy McCall |
3484 |
fn detect_renames( |
| 3485 |
base_snapshot: &Snapshot, |
| 3486 |
fs_entries: &BTreeMap<String, FsEntry>, |
| 3487 |
in_scope: impl Fn(&str) -> bool, |
| 3488 |
) -> Vec<(String, String)> { |
| 6e723b81 Andy McCall ↶ |
3489 |
let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot |
| 3490 |
.files |
| 3491 |
.iter() |
| 75167672 Andy McCall |
3492 |
.filter(|(p, f)| { |
| 3493 |
f.kind != NodeKind::Dir |
| 3494 |
&& !fs_entries.contains_key(*p) |
| 3495 |
&& in_scope(p) |
| 3496 |
&& !f.is_conflicted() |
| 3497 |
}) |
| 3498 |
.collect(); |
| 3499 |
let added: Vec<(&String, &FsEntry)> = fs_entries |
| 3500 |
.iter() |
| 3501 |
.filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p)) |
| 6e723b81 Andy McCall ↶ |
3502 |
.collect(); |
| 3503 |
if removed.is_empty() || added.is_empty() { |
| 3504 |
return vec![]; |
| 3505 |
} |
| 3506 |
let exact_only = removed.len() * added.len() > RENAME_LIMIT; |
| 3507 |
let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> { |
| 3508 |
match &f.content { |
| 3509 |
FileContent::Text { bytes, .. } => Some(bytes.clone()), |
| 3510 |
_ => None, |
| 3511 |
} |
| 3512 |
}; |
| 3513 |
let mut scored: Vec<(u32, usize, usize)> = Vec::new(); |
| 3514 |
for (ri, (_, f)) in removed.iter().enumerate() { |
| 3515 |
let old = old_content(f); |
| 3516 |
let old_blob = match &f.content { |
| 3517 |
FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]), |
| 3518 |
_ => None, |
| 3519 |
}; |
| 3520 |
for (ai, (_, e)) in added.iter().enumerate() { |
| 3521 |
if e.kind != f.kind { |
| 3522 |
continue; |
| 3523 |
} |
| 3524 |
let score = match (&old, old_blob) { |
| 3525 |
(Some(o), _) => { |
| 3526 |
if exact_only { |
| 40a40bb7 Andy McCall ↶ |
3527 |
if *o == *e.content { |
| 75167672 Andy McCall |
3528 |
100 |
| 3529 |
} else { |
| 3530 |
0 |
| 3531 |
} |
| 6e723b81 Andy McCall ↶ |
3532 |
} else { |
| 3533 |
similarity(o, &e.content) |
| 3534 |
} |
| 3535 |
} |
| b7f102fc Andy McCall ↶ |
3536 |
(None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100, |
| 6e723b81 Andy McCall ↶ |
3537 |
_ => 0, |
| 3538 |
}; |
| 3539 |
if score >= RENAME_THRESHOLD { |
| 3540 |
scored.push((score, ri, ai)); |
| 3541 |
} |
| 3542 |
} |
| 3543 |
} |
| 75167672 Andy McCall |
3544 |
scored.sort_by(|a, b| { |
| 3545 |
b.0.cmp(&a.0) |
| 3546 |
.then(removed[a.1].0.cmp(removed[b.1].0)) |
| 3547 |
.then(added[a.2].0.cmp(added[b.2].0)) |
| 3548 |
}); |
| 6e723b81 Andy McCall ↶ |
3549 |
let mut used_r = vec![false; removed.len()]; |
| 3550 |
let mut used_a = vec![false; added.len()]; |
| 3551 |
let mut out = Vec::new(); |
| 3552 |
for (_, ri, ai) in scored { |
| 3553 |
if used_r[ri] || used_a[ai] { |
| 3554 |
continue; |
| 3555 |
} |
| 3556 |
used_r[ri] = true; |
| 3557 |
used_a[ai] = true; |
| 3558 |
out.push((removed[ri].0.clone(), added[ai].0.clone())); |
| 3559 |
} |
| 3560 |
out |
| 3561 |
} |
| 3562 |
|
| 3563 |
|
| 3564 |
|
| 3565 |
pub fn similarity(a: &[u8], b: &[u8]) -> u32 { |
| 3566 |
if a == b { |
| 3567 |
return 100; |
| 3568 |
} |
| 3569 |
if a.is_empty() || b.is_empty() { |
| 3570 |
return 0; |
| 3571 |
} |
| 3572 |
let mut counts: HashMap<&[u8], usize> = HashMap::new(); |
| 3573 |
for l in split_lines(a) { |
| 3574 |
*counts.entry(l).or_default() += 1; |
| 3575 |
} |
| 3576 |
let mut shared = 0usize; |
| 3577 |
for l in split_lines(b) { |
| 3578 |
if let Some(n) = counts.get_mut(l) { |
| 3579 |
if *n > 0 { |
| 3580 |
*n -= 1; |
| 3581 |
shared += l.len(); |
| 3582 |
} |
| 3583 |
} |
| 3584 |
} |
| 3585 |
(shared * 100 / a.len().max(b.len())) as u32 |
| d616faf5 Andy McCall ↶ |
3586 |
} |
| 3587 |
|
| 3588 |
#[cfg(test)] |
| 3589 |
mod join_tests { |
| 3590 |
use super::*; |
| f57bf670 Andy McCall ↶ |
3591 |
|
| 3592 |
#[test] |
| 3593 |
fn missing_from_finds_what_the_other_lacks() { |
| 3594 |
let a: BTreeMap<String, u8> = [("a", 1), ("b", 2), ("c/d", 3), ("e", 4)] |
| 3595 |
.into_iter() |
| 3596 |
.map(|(k, v)| (k.to_string(), v)) |
| 3597 |
.collect(); |
| 3598 |
let b: BTreeMap<String, ()> = [("0", ()), ("b", ()), ("c", ()), ("e", ())] |
| 3599 |
.into_iter() |
| 3600 |
.map(|(k, v)| (k.to_string(), v)) |
| 3601 |
.collect(); |
| 3602 |
let got: Vec<&str> = missing_from(&a, &b) |
| 3603 |
.iter() |
| 3604 |
.map(|(k, _)| k.as_str()) |
| 3605 |
.collect(); |
| 3606 |
assert_eq!(got, vec!["a", "c/d"]); |
| 3607 |
} |
| d616faf5 Andy McCall ↶ |
3608 |
|
| 3609 |
#[test] |
| 3610 |
fn join_pairs_every_key_in_order() { |
| 3611 |
let a: BTreeMap<String, u8> = [("a", 1), ("b/c", 2), ("d", 3)] |
| 3612 |
.into_iter() |
| 3613 |
.map(|(k, v)| (k.to_string(), v)) |
| 3614 |
.collect(); |
| 3615 |
let b: BTreeMap<String, char> = [("b", 'x'), ("b/c", 'y'), ("e", 'z')] |
| 3616 |
.into_iter() |
| 3617 |
.map(|(k, v)| (k.to_string(), v)) |
| 3618 |
.collect(); |
| 3619 |
let got: Vec<(String, Option<u8>, Option<char>)> = join(&a, &b) |
| 3620 |
.map(|(k, x, y)| (k.clone(), x.copied(), y.copied())) |
| 3621 |
.collect(); |
| 3622 |
let want = vec![ |
| 3623 |
("a".to_string(), Some(1), None), |
| 3624 |
("b".to_string(), None, Some('x')), |
| 3625 |
("b/c".to_string(), Some(2), Some('y')), |
| 3626 |
("d".to_string(), Some(3), None), |
| 3627 |
("e".to_string(), None, Some('z')), |
| 3628 |
]; |
| 3629 |
assert_eq!(got, want); |
| 3630 |
} |
| 6e723b81 Andy McCall ↶ |
3631 |
} |