From b33762b4c8def82ac3432355ab6cc8678d115698 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 18 Nov 2026 11:54:13 +0800 Subject: [PATCH] fix(vcs): Compare Directories copies nothing before the overwrite answer, filters nested folders, keeps name columns readable (#4120) copy_merge without overwrite now finds the first conflict before writing anything, so declining the overwrite prompt leaves the destination untouched. A filter match below nested one-sided folders is attributed to the outermost one, the only row shown for that subtree. Name columns keep at least 96px; size and date shrink first and end in an ellipsis. Fixes #4084 Fixes #4085 Fixes #4086 --- crates/ide-vfs/src/compare.rs | 36 +++++++++++++++++++ src/features/vcs/DirectoryCompare.module.css | 17 +++++---- src/features/vcs/DirectoryCompare.tsx | 6 ++-- .../vcs/__tests__/directoryCompare.test.tsx | 7 ++++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/crates/ide-vfs/src/compare.rs b/crates/ide-vfs/src/compare.rs index 74d9a18bd..959c5af65 100644 --- a/crates/ide-vfs/src/compare.rs +++ b/crates/ide-vfs/src/compare.rs @@ -277,6 +277,25 @@ pub fn copy_merge( let target_base = project.filter(|p| to.starts_with(p)).or(Some(roots.1)); let rules = Rules::new(from, source_base, excludes); let target_rules = Rules::new(to, target_base, excludes); + // Find a conflict before writing anything, so declining to overwrite leaves the + // destination untouched. Nothing below a missing destination can conflict. + if !overwrite { + let mut pending = vec![(from.to_path_buf(), to.to_path_buf())]; + while let Some((src, dst)) = pending.pop() { + let meta = fs::symlink_metadata(&src)?; + if rules.excluded(&src, meta.is_dir()) || target_rules.excluded(&dst, meta.is_dir()) { + continue; + } + let Ok(existing) = fs::symlink_metadata(&dst) else { continue }; + if !(meta.is_dir() && existing.is_dir()) { + return Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, dst.display().to_string()).into()); + } + for entry in fs::read_dir(&src)? { + let entry = entry?; + pending.push((entry.path(), dst.join(entry.file_name()))); + } + } + } let mut pending = vec![(from.to_path_buf(), to.to_path_buf())]; while let Some((src, dst)) = pending.pop() { let meta = fs::symlink_metadata(&src)?; @@ -494,6 +513,23 @@ mod tests { } } + #[test] + fn a_declined_overwrite_writes_nothing() { + let tmp = tempfile::tempdir().unwrap(); + let left = tmp.path().join("left"); + let right = tmp.path().join("right"); + put(&left, "merge/a-clash.txt", b"source"); + put(&left, "merge/z-new.txt", b"source only"); + put(&left, "merge/deep/new.txt", b"new"); + put(&right, "merge/a-clash.txt", b"destination"); + let error = + copy_merge(&left.join("merge"), &right.join("merge"), false, None, &[], (&left, &right)).unwrap_err(); + assert_eq!(error.kind, ErrorKind::AlreadyExists); + assert_eq!(fs::read(right.join("merge/a-clash.txt")).unwrap(), b"destination"); + assert!(!right.join("merge/z-new.txt").exists()); + assert!(!right.join("merge/deep").exists()); + } + #[test] fn subtree_copy_keeps_excludes_anchored_to_each_comparison_root() { let tmp = tempfile::tempdir().unwrap(); diff --git a/src/features/vcs/DirectoryCompare.module.css b/src/features/vcs/DirectoryCompare.module.css index cee65d54c..f5040c5bb 100644 --- a/src/features/vcs/DirectoryCompare.module.css +++ b/src/features/vcs/DirectoryCompare.module.css @@ -4,7 +4,8 @@ height: 100%; min-height: 0; background: var(--bg-editor); - --compare-side: minmax(0, 1fr) 72px 132px; + /* Names keep a readable width; size and date give way first in a narrow tab. */ + --compare-side: minmax(96px, 1fr) minmax(0, 72px) minmax(0, 132px); --compare-columns: var(--compare-side) 28px var(--compare-side); } .paths { @@ -118,15 +119,19 @@ text-overflow: ellipsis; white-space: nowrap; } -.num { - justify-content: flex-end; - text-align: right; +/* Plain text cells: a narrow column ends in an ellipsis instead of clipping both edges. */ +.num, +.date { + display: block; + overflow: hidden; + text-overflow: ellipsis; font-variant-numeric: tabular-nums; } +.num { + text-align: right; +} .date { - justify-content: center; text-align: center; - font-variant-numeric: tabular-nums; } .marker { justify-content: center; diff --git a/src/features/vcs/DirectoryCompare.tsx b/src/features/vcs/DirectoryCompare.tsx index 2a07093c1..9b0b403d0 100644 --- a/src/features/vcs/DirectoryCompare.tsx +++ b/src/features/vcs/DirectoryCompare.tsx @@ -96,9 +96,11 @@ export function directoryRows( const both = (e: DirectoryCompareEntry) => e.leftKind === "dir" && e.rightKind === "dir"; const byPath = new Map(entries.map((e) => [e.path, e])); const oneSided = new Set(entries.filter((e) => directory(e) && (!e.leftKind || !e.rightKind)).map((e) => e.path)); + // The outermost one-sided folder: the only row shown for its whole subtree. const owner = (path: string) => { - for (let p = parentOf(path); p; p = parentOf(p)) if (oneSided.has(p)) return p; - return null; + let found: string | null = null; + for (let p = parentOf(path); p; p = parentOf(p)) if (oneSided.has(p)) found = p; + return found; }; const matched = new Set(); for (const e of entries) if (q && e.path.toLocaleLowerCase().includes(q)) matched.add(owner(e.path) ?? e.path); diff --git a/src/features/vcs/__tests__/directoryCompare.test.tsx b/src/features/vcs/__tests__/directoryCompare.test.tsx index df634aad5..cfd1cef19 100644 --- a/src/features/vcs/__tests__/directoryCompare.test.tsx +++ b/src/features/vcs/__tests__/directoryCompare.test.tsx @@ -116,6 +116,13 @@ describe("disk directory comparison", () => { "dir/sub/c.txt", ]); expect(directoryRows(entries, all, "inside").map(label)).toEqual(["gone"]); + // A match nested deeper inside a one-sided folder still shows that folder's row (#4085). + const nested = [ + row("only-left", "leftOnly", "dir", null), + row("only-left/sub", "leftOnly", "dir", null), + row("only-left/sub/nested.txt", "leftOnly", "file", null), + ]; + expect(directoryRows(nested, new Set(["leftOnly"]), "nested.txt").map(label)).toEqual(["only-left"]); expect(directoryRows(entries, new Set(["rightOnly"]), "").map(label)).toEqual(["", "dir/sub/c.txt"]); });