utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | 341013e85103211eb409fe405c8d6d61e2a34dd46af33f3e2ff222c0f85987b8 |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 22:56 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 22:56 +02:00 |
| Parents | a82d6e17a866 |
2 files changed
modified crates/wc/src/checkout.rs +28 −13
| @@ -75,20 +75,22 @@ | ||
| 75 | 75 | if from == to { |
| 76 | 76 | return Ok(plan); |
| 77 | 77 | } |
| 78 | - let mut olds = HashMap::new(); | |
| 78 | + // The diff the other way lists the same paths with what `from` has | |
| 79 | + // there: one walk over the difference instead of a lookup per path. | |
| 80 | + let olds: HashMap<Vec<u8>, ChangeOp> = if from == EMPTY_TREE { | |
| 81 | + HashMap::new() | |
| 82 | + } else { | |
| 83 | + diff(objects, to, from)? | |
| 84 | + .into_iter() | |
| 85 | + .filter(|change| !matches!(change.op, ChangeOp::Delete)) | |
| 86 | + .map(|change| (change.path, change.op)) | |
| 87 | + .collect() | |
| 88 | + }; | |
| 79 | 89 | for change in diff(objects, from, to)? { |
| 80 | - let old = if from == EMPTY_TREE { | |
| 81 | - None | |
| 82 | - } else { | |
| 83 | - entry_at(objects, from, &change.path)?.and_then(|kind| ChangeOp::from_entry(&kind)) | |
| 84 | - }; | |
| 85 | - if let Some(old) = &old { | |
| 86 | - olds.insert(change.path.clone(), old.clone()); | |
| 87 | - } | |
| 88 | 90 | match change.op { |
| 89 | 91 | ChangeOp::Delete => { |
| 90 | - if let Some(old) = old { | |
| 91 | - plan.removes.push((change.path, old)); | |
| 92 | + if let Some(old) = olds.get(&change.path) { | |
| 93 | + plan.removes.push((change.path, old.clone())); | |
| 92 | 94 | } |
| 93 | 95 | } |
| 94 | 96 | op => plan.writes.push((change.path, op)), |
| @@ -96,10 +98,19 @@ | ||
| 96 | 98 | } |
| 97 | 99 | |
| 98 | 100 | // Directories the removals may leave behind, deepest first. |
| 101 | + let mut is_dir: HashMap<&[u8], bool> = HashMap::new(); | |
| 99 | 102 | let mut gone = BTreeSet::new(); |
| 100 | 103 | for (path, _) in &plan.removes { |
| 101 | 104 | for dir in ancestors(path) { |
| 102 | - if !gone.contains(dir) && !is_dir_in(objects, to, dir)? { | |
| 105 | + let stays = match is_dir.get(dir) { | |
| 106 | + Some(stays) => *stays, | |
| 107 | + None => { | |
| 108 | + let stays = is_dir_in(objects, to, dir)?; | |
| 109 | + is_dir.insert(dir, stays); | |
| 110 | + stays | |
| 111 | + } | |
| 112 | + }; | |
| 113 | + if !stays { | |
| 103 | 114 | gone.insert(dir.to_vec()); |
| 104 | 115 | } |
| 105 | 116 | } |
| @@ -347,7 +358,7 @@ | ||
| 347 | 358 | } |
| 348 | 359 | |
| 349 | 360 | // Writes in path order, so a directory's files go out together. |
| 350 | - let mut by_dir: BTreeMap<&[u8], Vec<(&[u8], &[u8], &ChangeOp)>> = BTreeMap::new(); | |
| 361 | + let mut by_dir: BTreeMap<&[u8], Vec<Write<'_>>> = BTreeMap::new(); | |
| 351 | 362 | for (path, op) in &plan.writes { |
| 352 | 363 | let (parent, name) = split_last(path); |
| 353 | 364 | by_dir.entry(parent).or_default().push((path, name, op)); |
| @@ -385,6 +396,10 @@ | ||
| 385 | 396 | Ok(plan.len()) |
| 386 | 397 | } |
| 387 | 398 | |
| 399 | +/// One write of a directory: the full path, the name in the directory, and | |
| 400 | +/// what to write. | |
| 401 | +type Write<'p> = (&'p [u8], &'p [u8], &'p ChangeOp); | |
| 402 | + | |
| 388 | 403 | fn is_not_empty(err: &io::Error) -> bool { |
| 389 | 404 | // Some systems report a directory that is not empty as "exists". |
| 390 | 405 | err.kind() == io::ErrorKind::DirectoryNotEmpty || err.kind() == io::ErrorKind::AlreadyExists |
modified crates/wc/src/scan.rs +2 −2
| @@ -327,7 +327,7 @@ | ||
| 327 | 327 | |
| 328 | 328 | let first = scan(&root, &base, &Cache::default()).unwrap(); |
| 329 | 329 | assert_eq!(first.files[&b"a.txt".to_vec()], blob("one")); |
| 330 | - assert!(first.cache.entries.contains_key(&b"a.txt".to_vec())); | |
| 330 | + assert!(first.cache.entries.contains_key(b"a.txt".as_slice())); | |
| 331 | 331 | |
| 332 | 332 | // Rewrite the content in place behind the cache's back: same inode, |
| 333 | 333 | // same size, same modification time. The cache vouches for it, so |
| @@ -346,7 +346,7 @@ | ||
| 346 | 346 | let same_instant = UNIX_EPOCH + Duration::new(seconds as u64, nanos); |
| 347 | 347 | set_mtime(&path, same_instant); |
| 348 | 348 | let mut racy = second.cache.clone(); |
| 349 | - racy.entries.get_mut(&b"a.txt".to_vec()).unwrap().mtime = (seconds, nanos); | |
| 349 | + racy.entries.get_mut(b"a.txt".as_slice()).unwrap().mtime = (seconds, nanos); | |
| 350 | 350 | let third = scan(&root, &base, &racy).unwrap(); |
| 351 | 351 | assert_eq!(third.files[&b"a.txt".to_vec()], blob("two")); |
| 352 | 352 | } |