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