diff --git a/CLAUDE.md b/CLAUDE.md index d1c11493..bb2bde70 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -289,8 +289,8 @@ transport-adjacent. The rest, imperatively: the view. - **A merge, switch, cherry-pick, rebase, undo or sparse-spec change refuses, before any write, to lay a file or directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it - away and retry. On a sparse checkout the same holds for a path the result adds - outside the view (a later commit would take the user's file instead, #248). + away and retry. A directory holding only tracked files is not such a case: a file/directory change at one path crosses switch, undo, abort and `--continue`, and the emptied directories are cleared (#247). `materialize` checks every such directory before its first removal, so a refusal changes nothing and an abort stays retryable; a forward op also refuses a user's empty directory there (it cannot tell it from a husk), while `materialize` itself prunes empty ones. (On a case-insensitive checkout, replacing `foo/a` with a file `Foo` is refused: the check does not follow the case alias. Fail-closed.) On a sparse checkout the same holds for a path the result adds + outside the view (a later commit would take the user's file instead, #248); there any directory at the path refuses, tracked contents or not. Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded (except that a private stop refuses a `.theirs` sidecar that would land on an existing file). - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish diff --git a/crates/repo/src/abort_transitions.rs b/crates/repo/src/abort_transitions.rs new file mode 100644 index 00000000..1507d9e2 --- /dev/null +++ b/crates/repo/src/abort_transitions.rs @@ -0,0 +1,424 @@ +//! Tests for #247: aborting (or otherwise leaving) a tree where the other side +//! turned a file into a directory, or the reverse. The restore must replace +//! the stop's layout at that path, never fail with `IsADirectory` / +//! `NotADirectory`, and never delete anything the tree being replaced does not +//! track. + +use std::path::{Path, PathBuf}; + +use crate::error::Error; +use crate::repo::Repo; +use crate::RebaseResult; + +/// How the path `x` is laid out on a side. +#[derive(Clone, Copy, Debug)] +enum Shape { + File, + Dir, +} + +fn tmp_root(tag: &str) -> PathBuf { + let p = std::env::temp_dir().join(format!("scl-repo-abort-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&p); + std::fs::create_dir_all(&p).unwrap(); + p +} + +fn lay(root: &Path, shape: Shape) { + let _ = std::fs::remove_file(root.join("x")); + let _ = std::fs::remove_dir_all(root.join("x")); + match shape { + Shape::File => std::fs::write(root.join("x"), b"file\n").unwrap(), + Shape::Dir => { + std::fs::create_dir_all(root.join("x")).unwrap(); + std::fs::write(root.join("x/a"), b"a\n").unwrap(); + } + } +} + +fn assert_shape(root: &Path, shape: Shape) { + match shape { + Shape::File => assert_eq!(std::fs::read(root.join("x")).unwrap(), b"file\n"), + Shape::Dir => { + assert!(root.join("x").is_dir()); + assert_eq!(std::fs::read(root.join("x/a")).unwrap(), b"a\n"); + assert_eq!(std::fs::read_dir(root.join("x")).unwrap().count(), 1); + } + } +} + +/// `main` keeps `x` as `ours`; branch `other` turns it into `theirs` and both +/// edit `c.txt`'s first line, so combining them stops on a conflict. +fn fixture(tag: &str, ours: Shape, theirs: Shape) -> (PathBuf, Repo) { + let root = tmp_root(tag); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("c.txt"), b"1\n2\n3\n").unwrap(); + lay(&root, ours); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + std::fs::write(root.join("c.txt"), b"OURS\n2\n3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("other").unwrap(); + lay(&root, theirs); + std::fs::write(root.join("c.txt"), b"THEIRS\n2\n3\n").unwrap(); + repo.commit("me", "other").unwrap(); + repo.switch("main").unwrap(); + (root, repo) +} + +fn assert_restored(root: &Path, repo: &Repo, ours: Shape) { + assert_shape(root, ours); + assert_eq!(std::fs::read(root.join("c.txt")).unwrap(), b"OURS\n2\n3\n"); + let st = repo.status().unwrap(); + assert!(st.modified.is_empty() && st.deleted.is_empty(), "{st:?}"); +} + +fn finish(root: &Path, repo: Repo) { + drop(repo); + std::fs::remove_dir_all(root).unwrap(); + assert!( + !root.exists(), + "test root {} must be removed", + root.display() + ); +} + +const TRANSITIONS: [(&str, Shape, Shape); 2] = [ + ("f2d", Shape::File, Shape::Dir), + ("d2f", Shape::Dir, Shape::File), +]; + +#[test] +fn merge_abort_restores_a_file_dir_transition() { + for (tag, ours, theirs) in TRANSITIONS { + let (root, repo) = fixture(&format!("merge-{tag}"), ours, theirs); + assert!(matches!( + repo.merge("other", "me").unwrap_err(), + Error::MergeConflicts(_) + )); + assert_shape(&root, theirs); + repo.merge_abort().unwrap(); + assert_restored(&root, &repo, ours); + finish(&root, repo); + } +} + +#[test] +fn cherry_pick_abort_restores_a_file_dir_transition() { + for (tag, ours, theirs) in TRANSITIONS { + let (root, repo) = fixture(&format!("pick-{tag}"), ours, theirs); + let r = repo.cherry_pick("other", "me", None, None); + assert!(matches!(r, Err(Error::PickConflicts(_))), "{r:?}"); + assert_shape(&root, theirs); + repo.cherry_pick_abort().unwrap(); + assert_restored(&root, &repo, ours); + finish(&root, repo); + } +} + +#[test] +fn rebase_abort_restores_a_file_dir_transition() { + for (tag, ours, theirs) in TRANSITIONS { + let (root, repo) = fixture(&format!("rebase-{tag}"), ours, theirs); + let r = repo.rebase("other", "me", None).unwrap(); + assert!(matches!(r, RebaseResult::Stopped { .. }), "{r:?}"); + assert_shape(&root, theirs); + repo.rebase_abort().unwrap(); + assert_restored(&root, &repo, ours); + finish(&root, repo); + } +} + +/// A stopped merge with `x` a directory the abort must turn back into a +/// file. `add` puts something of the user's under `x/`; the abort must then +/// refuse having changed nothing, and succeed once `remove` takes it away. +fn refused_abort_changes_nothing(tag: &str, add: impl FnOnce(&Path), remove: impl FnOnce(&Path)) { + let (root, repo) = fixture(tag, Shape::File, Shape::Dir); + assert!(matches!( + repo.merge("other", "me").unwrap_err(), + Error::MergeConflicts(_) + )); + let markers = std::fs::read(root.join("c.txt")).unwrap(); + assert!(String::from_utf8_lossy(&markers).contains("<<<<<<<")); + add(&root.join("x")); + assert!(repo.merge_abort().is_err()); + assert!(repo.merge_in_progress(), "the merge is still open"); + assert_eq!(std::fs::read(root.join("c.txt")).unwrap(), markers); + assert_eq!(std::fs::read(root.join("x/a")).unwrap(), b"a\n"); + remove(&root.join("x")); + repo.merge_abort().unwrap(); + assert_restored(&root, &repo, Shape::File); + finish(&root, repo); +} + +/// An untracked file inside the directory the abort must replace with a file +/// is the user's: the abort refuses before the first write and stays retryable. +#[test] +fn merge_abort_keeps_untracked_content_in_the_obstructing_directory() { + refused_abort_changes_nothing( + "merge-untracked", + |x| std::fs::write(x.join("mine.txt"), b"mine\n").unwrap(), + |x| std::fs::remove_file(x.join("mine.txt")).unwrap(), + ); +} + +#[test] +fn abort_refuses_an_ignored_file_and_a_dotfile_in_the_directory() { + refused_abort_changes_nothing( + "merge-ignored", + |x| { + std::fs::write(x.join("../.scignore"), b"*.tmp\n").unwrap(); + std::fs::write(x.join("scratch.tmp"), b"t\n").unwrap(); + }, + |x| std::fs::remove_file(x.join("scratch.tmp")).unwrap(), + ); + refused_abort_changes_nothing( + "merge-dotfile", + |x| std::fs::write(x.join(".hidden"), b"h\n").unwrap(), + |x| std::fs::remove_file(x.join(".hidden")).unwrap(), + ); +} + +/// A symlink to a directory outside the repository, directly or nested, is +/// neither followed nor removed. +#[cfg(unix)] +#[test] +fn abort_refuses_a_symlink_to_an_outside_directory() { + let outside = tmp_root("symlink-outside"); + std::fs::create_dir(outside.join("keep")).unwrap(); + for nested in [false, true] { + let target = outside.clone(); + refused_abort_changes_nothing( + &format!("merge-symlink-{nested}"), + |x| { + let dir = if nested { + std::fs::create_dir(x.join("sub")).unwrap(); + x.join("sub") + } else { + x.to_path_buf() + }; + std::os::unix::fs::symlink(&target, dir.join("link")).unwrap(); + }, + |x| { + let dir = if nested { + x.join("sub") + } else { + x.to_path_buf() + }; + std::fs::remove_file(dir.join("link")).unwrap(); + }, + ); + assert!(outside.join("keep").is_dir(), "the link was followed"); + } + std::fs::remove_dir_all(&outside).unwrap(); +} + +/// A subdirectory that cannot be read is treated as holding the user's +/// content (skipped when the process can read it anyway, as root does). +#[cfg(unix)] +#[test] +fn abort_refuses_an_unreadable_subdirectory() { + use std::os::unix::fs::PermissionsExt; + let probe = tmp_root("unreadable-probe"); + std::fs::set_permissions(&probe, std::fs::Permissions::from_mode(0o000)).unwrap(); + let readable = std::fs::read_dir(&probe).is_ok(); + std::fs::set_permissions(&probe, std::fs::Permissions::from_mode(0o755)).unwrap(); + std::fs::remove_dir_all(&probe).unwrap(); + if readable { + return; + } + refused_abort_changes_nothing( + "merge-unreadable", + |x| { + std::fs::create_dir(x.join("locked")).unwrap(); + std::fs::set_permissions(x.join("locked"), std::fs::Permissions::from_mode(0o000)) + .unwrap(); + }, + |x| { + std::fs::set_permissions(x.join("locked"), std::fs::Permissions::from_mode(0o755)) + .unwrap(); + std::fs::remove_dir(x.join("locked")).unwrap(); + }, + ); +} + +/// `switch` and `undo` across a file/directory change at one path. +#[test] +fn switch_and_undo_cross_a_file_dir_transition() { + for (tag, ours, theirs) in TRANSITIONS { + let root = tmp_root(&format!("switch-{tag}")); + let repo = Repo::init(&root).unwrap(); + lay(&root, ours); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + repo.switch("other").unwrap(); + lay(&root, theirs); + repo.commit("me", "other").unwrap(); + repo.switch("main").unwrap(); + assert_shape(&root, ours); + repo.switch("other").unwrap(); + assert_shape(&root, theirs); + repo.switch("main").unwrap(); + assert_shape(&root, ours); + lay(&root, theirs); + repo.commit("me", "flip").unwrap(); + repo.undo().unwrap(); + assert_shape(&root, ours); + finish(&root, repo); + } +} + +/// `rebase --continue` lays a later commit's file/directory change over the +/// resolved stop. +#[test] +fn rebase_continue_crosses_a_file_dir_transition() { + for (tag, from, to) in TRANSITIONS { + let root = tmp_root(&format!("continue-{tag}")); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("c.txt"), b"1\n2\n3\n").unwrap(); + lay(&root, from); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + std::fs::write(root.join("c.txt"), b"OURS\n2\n3\n").unwrap(); + repo.commit("me", "a").unwrap(); + lay(&root, to); + repo.commit("me", "b").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("c.txt"), b"THEIRS\n2\n3\n").unwrap(); + repo.commit("me", "other").unwrap(); + repo.switch("main").unwrap(); + assert!(matches!( + repo.rebase("other", "me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + std::fs::write(root.join("c.txt"), b"RESOLVED\n2\n3\n").unwrap(); + let r = repo.rebase_continue("me", None).unwrap(); + assert!(matches!(r, RebaseResult::Rebased { .. }), "{r:?}"); + assert_shape(&root, to); + finish(&root, repo); + } +} + +/// The same abort on a `--filter src/` partial clone, with `src/x` changing +/// between file and directory on the other side. +#[test] +fn merge_abort_restores_a_file_dir_transition_on_a_partial_clone() { + use crate::partial_merge::{clone_edit_src, Fx}; + for (tag, ours, theirs) in TRANSITIONS { + let fx = Fx::origin(&format!("abort-fd-{tag}"), |root| { + lay(&root.join("src"), ours); + }); + fx.origin.branch("b1").unwrap(); + fx.origin.switch("b1").unwrap(); + lay(&fx.origin_root.join("src"), theirs); + std::fs::write(fx.origin_root.join("src/a.txt"), b"ONE\ntwo\nthree\n").unwrap(); + fx.origin.commit("t", "b1").unwrap(); + fx.origin.switch("main").unwrap(); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, b"uno\ntwo\nthree\n"); + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(_) + )); + assert_shape(&fx.clone_root.join("src"), theirs); + clone.merge_abort().unwrap(); + assert_shape(&fx.clone_root.join("src"), ours); + let st = clone.status().unwrap(); + assert!(st.modified.is_empty() && st.deleted.is_empty(), "{st:?}"); + drop(clone); + fx.cleanup(); + } +} + +/// A forward `switch` onto a file where a directory with an untracked file +/// stands still refuses, and deletes nothing. +#[test] +fn switch_refuses_a_file_over_a_directory_holding_untracked_content() { + let root = tmp_root("switch-untracked"); + let repo = Repo::init(&root).unwrap(); + lay(&root, Shape::Dir); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + repo.switch("other").unwrap(); + lay(&root, Shape::File); + repo.commit("me", "other").unwrap(); + repo.switch("main").unwrap(); + std::fs::write(root.join("x/mine.txt"), b"mine\n").unwrap(); + let err = repo.switch("other").unwrap_err(); + assert!(matches!(err, Error::InvalidArgument(_)), "{err:?}"); + assert_eq!(std::fs::read(root.join("x/mine.txt")).unwrap(), b"mine\n"); + assert_eq!(std::fs::read(root.join("x/a")).unwrap(), b"a\n"); + finish(&root, repo); +} + +/// A user's empty directory is not evidence of tracked content: a forward +/// `switch` onto a file at its path refuses and keeps it. +#[test] +fn switch_keeps_an_untracked_empty_directory_where_a_file_must_go() { + let root = tmp_root("switch-empty-dir"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("c.txt"), b"c\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + repo.switch("other").unwrap(); + lay(&root, Shape::File); + repo.commit("me", "other").unwrap(); + repo.switch("main").unwrap(); + std::fs::create_dir(root.join("x")).unwrap(); + assert!(repo.switch("other").is_err()); + assert!(root.join("x").is_dir()); + finish(&root, repo); +} + +/// An abort that cannot replace a directory because of an untracked file +/// leaves the user's empty subdirectories alone too. +#[test] +fn merge_abort_refusal_does_not_prune_empty_user_directories() { + let (root, repo) = fixture("merge-prune", Shape::File, Shape::Dir); + assert!(repo.merge("other", "me").is_err()); + std::fs::write(root.join("x/mine.txt"), b"mine\n").unwrap(); + std::fs::create_dir(root.join("x/empty")).unwrap(); + assert!(repo.merge_abort().is_err()); + assert!(root.join("x/empty").is_dir()); + assert!(root.join("x/mine.txt").exists()); + finish(&root, repo); +} + +/// Switching onto a private branch across a directory-to-file change. +#[test] +fn private_switch_crosses_a_directory_to_file_transition() { + let root = tmp_root("private"); + let repo = Repo::init(&root).unwrap(); + let (sk, _pk) = scl_crypto::generate_keypair(); + lay(&root, Shape::Dir); + repo.commit("me", "base").unwrap(); + repo.branch_private("fix", &sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&sk)).unwrap(); + lay(&root, Shape::File); + repo.commit_private("me", "flip", Some(&sk)).unwrap(); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + assert_shape(&root, Shape::Dir); + repo.switch_with_identity("fix", Some(&sk)).unwrap(); + assert_shape(&root, Shape::File); + finish(&root, repo); +} + +/// Adopting a branch into an unborn one has no old tree and no untracked +/// guard: an empty directory of the user's where a file must go is kept, and +/// the merge fails rather than deleting it. +#[test] +fn unborn_adoption_keeps_an_empty_directory_where_a_file_must_go() { + let src = tmp_root("unborn-src"); + let origin = Repo::init(&src).unwrap(); + std::fs::write(src.join("x"), b"file\n").unwrap(); + origin.commit("me", "base").unwrap(); + let dst = tmp_root("unborn-dst"); + let repo = Repo::init(&dst).unwrap(); + repo.remote_add("origin", src.to_str().unwrap()).unwrap(); + repo.fetch("origin").unwrap(); + std::fs::create_dir(dst.join("x")).unwrap(); + assert!(repo.merge("origin/main", "me").is_err()); + assert!(dst.join("x").is_dir(), "the user's directory must survive"); + finish(&dst, repo); + finish(&src, origin); +} diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index c09d4936..18c4202d 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -1,6 +1,8 @@ //! `scl-repo` — the durable on-disk repository: `.sc/` layout, refs/HEAD, //! named branches, a git-like working tree, and commit/secret orchestration. +#[cfg(test)] +mod abort_transitions; pub mod bucket_transport; pub(crate) mod cache; pub(crate) mod conflict_plaintext; diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 9ebffdab..aba30038 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -412,6 +412,7 @@ impl Repo { // envelope, decrypted via the inner snapshot's wraps. match crate::protect::decrypt_with(&bytes, blob_id, &[protection], identity, path) { Ok(pt) => { + worktree::clear_dir_obstruction(&full, path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } @@ -421,6 +422,7 @@ impl Repo { // KEK recipient without path access: skip, never // leave stale plaintext behind (same rule as // `worktree::materialize`). + worktree::clear_dir_obstruction(&full, path)?; match std::fs::remove_file(&full) { Ok(()) => {} Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} @@ -430,6 +432,7 @@ impl Repo { } } } else { + worktree::clear_dir_obstruction(&full, path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 6c8cf585..e5b35b67 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1915,7 +1915,22 @@ impl Repo { .iter() .map(|(p, ..)| p.as_str()) .chain(sidecars.iter().map(|(r, _)| r.as_str())); + let sidecar_rels: std::collections::BTreeSet<&str> = + sidecars.iter().map(|(r, _)| r.as_str()).collect(); for rel in targets { + // Only a plaintext target clears a tracked directory husk; a + // sidecar is written with a plain write, so any directory at + // its path blocks. + if sidecar_rels.contains(rel) + && worktree::safe_join(&self.layout.root, rel)? + .symlink_metadata() + .is_ok_and(|m| m.is_dir()) + { + return Err(Error::InvalidArgument(format!( + "{op} has conflicts, and its sidecar {rel} would land on a directory; \ + move it away, then retry" + ))); + } // Every target is in the view (gated above; a sidecar is // never tracked, checked above). A failed lookup counts as // untracked. @@ -1984,6 +1999,7 @@ impl Repo { )?; for (path, bytes, mode, _recipients) in to_encrypt { let full = worktree::safe_join(&self.layout.root, path)?; + worktree::clear_dir_obstruction(&full, path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } @@ -2247,9 +2263,9 @@ impl Repo { ) -> Result<()> { // `rel` is not in `old`, and a tree holds no file at an ancestor of // another of its paths, so nothing on the way is a tracked file. - // Relies on `stop_write_blocker` treating whatever stands at `rel` (any - // directory included) as the user's: nothing outside the view is laid - // out, so no tracked file can be on disk there. + // Nothing outside the view is laid out, so no tracked file can be on + // disk there and any directory at `rel` is the user's. `stop_write_blocker` + // accepts a directory of tracked files (#247), so refuse one here first. // Only a case alias of a path `old` laid out, so inside the view (a // case-only rename on a case-insensitive filesystem), is the tracked // file; an out-of-view old path was never on disk. @@ -2264,7 +2280,10 @@ impl Repo { .iter() .filter(|p| !sparse.matches(p) && !old.contains(*p)) { - if stop_write_blocker(&self.layout.root, rel, &mut is_tracked)?.is_some() { + let is_dir = worktree::safe_join(&self.layout.root, rel)? + .symlink_metadata() + .is_ok_and(|m| m.is_dir()); + if is_dir || stop_write_blocker(&self.layout.root, rel, &mut is_tracked)?.is_some() { return Err(Error::InvalidArgument(format!( "{op} would add {rel} (outside your sparse checkout), where an untracked or \ ignored file or directory already stands; a later commit would take that \ @@ -2829,6 +2848,40 @@ fn is_tracked_case_alias( false } +/// True when the directory `full` (path `rel`) holds at least one tracked file +/// and nothing else of substance: no symlink, and no file `tracked` does not +/// name. An empty directory has no tracked file to show it is the old tree's, +/// and a read failure counts as not tracked. +fn dir_holds_only_tracked( + full: &std::path::Path, + rel: &str, + tracked: &mut impl FnMut(&str) -> bool, +) -> bool { + // `Some(n)`: only tracked files, `n` of them. + fn count( + full: &std::path::Path, + rel: &str, + tracked: &mut impl FnMut(&str) -> bool, + ) -> Option { + let mut n = 0; + for e in std::fs::read_dir(full).ok()? { + let e = e.ok()?; + // A name that is not UTF-8 cannot be a tracked path. + let child = format!("{rel}/{}", e.file_name().to_str()?); + let t = e.file_type().ok()?; + if t.is_dir() { + n += count(&e.path(), &child, tracked)?; + } else if t.is_file() && tracked(&child) { + n += 1; + } else { + return None; + } + } + Some(n) + } + count(full, rel, tracked).is_some_and(|n| n > 0) +} + /// What stands in the way of a conflict stop's direct write to `rel`. #[derive(Debug, PartialEq, Eq)] enum StopBlocker { @@ -2845,7 +2898,8 @@ enum StopBlocker { /// tracks `path` as a file that is on disk. A tracked file standing where a /// parent directory must be is fine: the stop's materialize removes it before /// the write, as a merge that replaces a file with a directory does. A -/// directory at `rel` is never fine, tracked or not: the write would fail. +/// directory at `rel` is fine only when everything under it is tracked (the +/// reverse replacement, #247); any other content in it is the user's. fn stop_write_blocker( root: &std::path::Path, rel: &str, @@ -2867,7 +2921,13 @@ fn stop_write_blocker( let full = worktree::safe_join(root, rel)?; match full.symlink_metadata() { Err(_) => Ok(None), - Ok(m) if m.is_dir() => Ok(Some(StopBlocker::Occupied)), + // A directory is fine when it holds only tracked files: the write + // removes them first and `worktree::clear_dir_obstruction` the husk. + Ok(m) if m.is_dir() => Ok(if dir_holds_only_tracked(&full, rel, tracked) { + None + } else { + Some(StopBlocker::Occupied) + }), // A symlink is never the tracked file: a write would follow it to // whatever it points at, outside the repository. Ok(m) if m.file_type().is_symlink() => Ok(Some(StopBlocker::Occupied)), @@ -7159,13 +7219,22 @@ mod stop_blocker_tests { } #[test] - fn a_directory_at_the_target_blocks_even_when_tracked() { + fn a_directory_at_the_target_blocks_unless_everything_in_it_is_tracked() { let root = tmp("dir-at-target"); std::fs::create_dir_all(root.join("x/y")).unwrap(); - let tracked = |_: &str| true; + std::fs::write(root.join("x/y/a"), b"a\n").unwrap(); + // Only `x/y/a` is tracked: the directory holds nothing else. + let tracked = |p: &str| p == "x/y/a"; assert_eq!( stop_write_blocker(&root, "x/y", &mut { tracked }).unwrap(), - Some(StopBlocker::Occupied) + None, + "the stop removes the tracked files, then the empty directories" + ); + std::fs::write(root.join("x/y/mine"), b"mine\n").unwrap(); + assert_eq!( + stop_write_blocker(&root, "x/y", &mut { tracked }).unwrap(), + Some(StopBlocker::Occupied), + "an untracked file in it is the user's" ); std::fs::remove_dir_all(&root).unwrap(); } diff --git a/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs index c77e9cb6..220e0736 100644 --- a/crates/repo/src/sparse_completion.rs +++ b/crates/repo/src/sparse_completion.rs @@ -409,6 +409,30 @@ fn a_stop_refuses_an_untracked_file_where_the_other_side_adds_one_out_of_view() } } +/// Any directory where the other side adds an out-of-view file is the user's, +/// empty or not: `stop_write_blocker` accepts a directory of tracked files +/// (#247), but nothing out of view is laid out, so none is tracked here. +#[test] +fn a_stop_refuses_any_directory_where_the_other_side_adds_a_file_out_of_view() { + for op in ops() { + for with_file in [false, true] { + let (root, repo) = collision_setup(&tag(op, &format!("dir-{with_file}")), op); + let dir = root.join("docs/new.txt"); + std::fs::create_dir_all(&dir).unwrap(); + if with_file { + std::fs::write(dir.join("mine"), b"MINE\n").unwrap(); + } + let err = start(&repo, op).unwrap_err().to_string(); + assert!(err.contains("docs/new.txt"), "{op:?}: {err}"); + assert!(!in_progress(&repo), "{op:?}: refusal left state behind"); + assert!(dir.is_dir(), "{op:?}: the directory is untouched"); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "temp repo must be removed"); + } + } +} + /// A file standing where the other side needs a parent directory out of view. #[test] fn a_stop_refuses_an_untracked_file_where_an_out_of_view_directory_must_be() { diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 551c293d..d1c31169 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -888,6 +888,7 @@ pub(crate) fn lay_out_added_view( continue; }; let full = safe_join(&layout.root, &path)?; + clear_dir_obstruction(&full, &path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } @@ -897,6 +898,61 @@ pub(crate) fn lay_out_added_view( Ok(protected) } +/// Make room for a file at `full` (tracked path `path`) where a directory +/// stands: the removal pass has deleted the tracked files under it, so what is +/// left is a husk of empty directories, which goes. A directory that still +/// holds anything (an untracked or ignored file of the user's) is never +/// touched: the error names it, and the caller's state stays retryable (#247). +pub(crate) fn clear_dir_obstruction(full: &Path, path: &str) -> Result<()> { + // Bottom-up `remove_dir`, never `remove_dir_all`: a file created after the + // check makes a directory non-empty and the removal fail, not vanish. + fn prune(dir: &Path) -> std::io::Result<()> { + for e in std::fs::read_dir(dir)? { + let e = e?; + // A child swapped for a symlink since the check is never followed. + if !e.file_type()?.is_dir() { + return Err(std::io::ErrorKind::DirectoryNotEmpty.into()); + } + prune(&e.path())?; + } + std::fs::remove_dir(dir) + } + let is_dir = full.symlink_metadata().is_ok_and(|m| m.is_dir()); + // Empty directories go whoever made them: they hold nothing to lose. (A + // forward op refuses a user's empty directory up front instead, because + // before its removal pass it cannot tell one from a husk.) + if is_dir && !(dir_clearable(full, path, &|_| false) && prune(full).is_ok()) { + return Err(Error::InvalidArgument(format!( + "cannot write {path}: a directory with untracked or ignored content stands there; \ + move it away, then retry" + ))); + } + Ok(()) +} + +/// Whether the directory `dir` (tracked path `rel`) holds only directories and +/// regular files that `removable` names, all the way down: no symlink, no +/// other file, no unreadable subdirectory, no name that is not UTF-8. Reads +/// only; `materialize` runs it for every directory a target file replaces +/// before its first removal, so a refusal leaves the working tree untouched. +fn dir_clearable(dir: &Path, rel: &str, removable: &dyn Fn(&str) -> bool) -> bool { + let Ok(entries) = std::fs::read_dir(dir) else { + return false; + }; + entries.into_iter().all(|e| { + let Ok(e) = e else { return false }; + let Some(name) = e.file_name().to_str().map(str::to_owned) else { + return false; + }; + let child = format!("{rel}/{name}"); + match e.file_type() { + Ok(t) if t.is_dir() => dir_clearable(&e.path(), &child, removable), + Ok(t) if t.is_file() => removable(&child), + _ => false, + } + }) +} + /// Materialize a snapshot's file tree into the working dir. /// /// `sparse` filters the disk view: a target entry outside `sparse` is never @@ -966,6 +1022,15 @@ pub(crate) fn materialize_with_old_view( // (partial clones only; ADR-0048). The check lives here so every caller // is covered and none can forget it. preflight_view(layout, store, target_root, sparse)?; + // With no old tree there is no removal pass, so an empty directory at a + // target path is the user's, not a husk: leave it, and let the write fail. + let clear = |full: &Path, path: &str| { + if old_root.is_some() { + clear_dir_obstruction(full, path) + } else { + Ok(()) + } + }; // Gap-tolerant when sparse is active (P27 Task 4): a promisor-filtered // clone never fetched out-of-filter subtrees at all, so an unfiltered // walk here would `NotFound` on them. @@ -1003,8 +1068,24 @@ pub(crate) fn materialize_with_old_view( } else { tree_file_ids_sparse(store, old, &old_walk_bound)? }; + let gone = |p: &str| !target.contains_key(p) || !sparse.matches(p); + // Before the first removal: a directory a target file must replace + // may hold only directories and files this pass is about to delete. + // Anything else is the user's, and refusing now (rather than at the + // write, after the pass and earlier writes) changes nothing (#247). + for path in target.keys().filter(|p| sparse.matches(p)) { + let full = safe_join(&layout.root, path)?; + if full.symlink_metadata().is_ok_and(|m| m.is_dir()) + && !dir_clearable(&full, path, &|c| old_files.contains_key(c) && gone(c)) + { + return Err(Error::InvalidArgument(format!( + "cannot write {path}: a directory with untracked or ignored content stands \ + there; move it away, then retry" + ))); + } + } for p in old_files.keys() { - if !target.contains_key(p) || !sparse.matches(p) { + if gone(p) { let full = safe_join(&layout.root, p)?; let _ = std::fs::remove_file(full); } @@ -1036,6 +1117,7 @@ pub(crate) fn materialize_with_old_view( })(); match decrypted { Some(pt) => { + clear(&full, path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } @@ -1061,6 +1143,7 @@ pub(crate) fn materialize_with_old_view( // can't linger when a path becomes protected across a switch for // a non-recipient (confidentiality). A failed removal must // surface — swallowing it leaves plaintext on disk. + clear(&full, path)?; match std::fs::remove_file(&full) { Ok(()) => {} Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} @@ -1070,6 +1153,7 @@ pub(crate) fn materialize_with_old_view( } } } else { + clear(&full, path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; }