Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(remove): drain the trash with one background process per repository (#4516)
Maximilian Roos25d0c44b0bda
diff --git a/Cargo.toml b/Cargo.tomlindex 185179baa..2178e6fb2 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -331,8 +331,9 @@ open = { version = "5.3", optional = true }# Gating keeps both out of the Windows dependency graph.nix = { version = "0.31.3", default-features = false, features = ["event", "fs", "poll", "process", "signal"] }signal-hook = "0.4.1"-# Safe FIONREAD snapshots bound cancellation drains even with continuous writers.-rustix = "1.1.4"+# Safe FIONREAD snapshots bound cancellation drains even with continuous writers;+# directory-relative `*at` calls let tree removal never follow a symlink.+rustix = { version = "1.1.4", features = ["fs"] }[target.'cfg(windows)'.dependencies]# The localized Documents folder that holds PowerShell profiles.diff --git a/docs/src/content/docs/faq.md b/docs/src/content/docs/faq.mdindex fa87006cd..08b7f2195 100644--- a/docs/src/content/docs/faq.md+++ b/docs/src/content/docs/faq.md@@ -150,6 +150,7 @@ Worktrunk stores repository state, caches, and logs under `.git/`:| `.git/wt/logs/subprocess.log` | Raw uncapped subprocess stdout/stderr (may be multi-MB) | Running with `-vv` || `.git/wt/logs/diagnostic.md` | Diagnostic report for issue reporting (leads with the performance profile) | Running with `-vv` || `.git/wt/trash/<name>-<timestamp>` | Staged worktree contents pending background deletion | `wt remove` |+| `.git/wt/trash.lock` | Lock for background trash deletion | `wt remove` |None of this is tracked by git or pushed to remotes.@@ -215,7 +216,7 @@ A branch checked out in a second worktree is retained regardless, `-D` included.- `wt merge` / `wt step push` — the target branch's checked-out worktree is updated to the merged commits, so a file those commits delete disappears from it, and an ignored file at a path they track is overwritten — the same result a `git merge` run in that worktree would produce. Uncommitted changes at paths the merge doesn't touch stay in place, staged or not; one at a path it does touch refuses the merge upfront, naming the file- `wt remove` / `wt step prune` — unregister a stale worktree (`⊟` in `wt list`) by deleting its `.git/worktrees/<id>` entry, as `git worktree prune` does, but for that worktree only. Files still in its directory stay. An entry whose index holds staged changes, or with a rebase, merge, or other operation in progress, is kept unless `wt remove --force` names it, since `git worktree repair` can restore it only while the entry exists-- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and sweeps `.git/wt/trash/` entries older than 24 hours along with fsmonitor daemons whose worktree no longer exists+- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and resumes deleting `.git/wt/trash/` entries an interrupted removal left behind, along with fsmonitor daemons whose worktree no longer exists- `wt config state clear` — clears config keys, caches, markers, hints, variables, logs, and stale trash- `wt config shell install` — replaces an existing fish or Nushell wrapper file whole, and removes one an older version installed at a previous location (fish `conf.d/wt.fish`, Nushell `<config-dir>/vendor/autoload/wt.nu`), printing each removal; [Files created](/shell-integration/#files-created) names the file each shell gets- `wt config shell uninstall` — removes the lines that run `wt config shell init` from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Every removed line is printeddiff --git a/plugins/worktrunk/skills/worktrunk/reference/faq.md b/plugins/worktrunk/skills/worktrunk/reference/faq.mdindex e0d298cc2..a10528385 100644--- a/plugins/worktrunk/skills/worktrunk/reference/faq.md+++ b/plugins/worktrunk/skills/worktrunk/reference/faq.md@@ -146,6 +146,7 @@ Worktrunk stores repository state, caches, and logs under `.git/`:| `.git/wt/logs/subprocess.log` | Raw uncapped subprocess stdout/stderr (may be multi-MB) | Running with `-vv` || `.git/wt/logs/diagnostic.md` | Diagnostic report for issue reporting (leads with the performance profile) | Running with `-vv` || `.git/wt/trash/<name>-<timestamp>` | Staged worktree contents pending background deletion | `wt remove` |+| `.git/wt/trash.lock` | Lock for background trash deletion | `wt remove` |None of this is tracked by git or pushed to remotes.@@ -211,7 +212,7 @@ A branch checked out in a second worktree is retained regardless, `-D` included.- `wt merge` / `wt step push` — the target branch's checked-out worktree is updated to the merged commits, so a file those commits delete disappears from it, and an ignored file at a path they track is overwritten — the same result a `git merge` run in that worktree would produce. Uncommitted changes at paths the merge doesn't touch stay in place, staged or not; one at a path it does touch refuses the merge upfront, naming the file- `wt remove` / `wt step prune` — unregister a stale worktree (`⊟` in `wt list`) by deleting its `.git/worktrees/<id>` entry, as `git worktree prune` does, but for that worktree only. Files still in its directory stay. An entry whose index holds staged changes, or with a rebase, merge, or other operation in progress, is kept unless `wt remove --force` names it, since `git worktree repair` can restore it only while the entry exists-- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and sweeps `.git/wt/trash/` entries older than 24 hours along with fsmonitor daemons whose worktree no longer exists+- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and resumes deleting `.git/wt/trash/` entries an interrupted removal left behind, along with fsmonitor daemons whose worktree no longer exists- `wt config state clear` — clears config keys, caches, markers, hints, variables, logs, and stale trash- `wt config shell install` — replaces an existing fish or Nushell wrapper file whole, and removes one an older version installed at a previous location (fish `conf.d/wt.fish`, Nushell `<config-dir>/vendor/autoload/wt.nu`), printing each removal; [Files created](https://worktrunk.dev/shell-integration/#files-created) names the file each shell gets- `wt config shell uninstall` — removes the lines that run `wt config shell init` from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Every removed line is printeddiff --git a/skills/worktrunk/reference/faq.md b/skills/worktrunk/reference/faq.mdindex e0d298cc2..a10528385 100644--- a/skills/worktrunk/reference/faq.md+++ b/skills/worktrunk/reference/faq.md@@ -146,6 +146,7 @@ Worktrunk stores repository state, caches, and logs under `.git/`:| `.git/wt/logs/subprocess.log` | Raw uncapped subprocess stdout/stderr (may be multi-MB) | Running with `-vv` || `.git/wt/logs/diagnostic.md` | Diagnostic report for issue reporting (leads with the performance profile) | Running with `-vv` || `.git/wt/trash/<name>-<timestamp>` | Staged worktree contents pending background deletion | `wt remove` |+| `.git/wt/trash.lock` | Lock for background trash deletion | `wt remove` |None of this is tracked by git or pushed to remotes.@@ -211,7 +212,7 @@ A branch checked out in a second worktree is retained regardless, `-D` included.- `wt merge` / `wt step push` — the target branch's checked-out worktree is updated to the merged commits, so a file those commits delete disappears from it, and an ignored file at a path they track is overwritten — the same result a `git merge` run in that worktree would produce. Uncommitted changes at paths the merge doesn't touch stay in place, staged or not; one at a path it does touch refuses the merge upfront, naming the file- `wt remove` / `wt step prune` — unregister a stale worktree (`⊟` in `wt list`) by deleting its `.git/worktrees/<id>` entry, as `git worktree prune` does, but for that worktree only. Files still in its directory stay. An entry whose index holds staged changes, or with a rebase, merge, or other operation in progress, is kept unless `wt remove --force` names it, since `git worktree repair` can restore it only while the entry exists-- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and sweeps `.git/wt/trash/` entries older than 24 hours along with fsmonitor daemons whose worktree no longer exists+- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and resumes deleting `.git/wt/trash/` entries an interrupted removal left behind, along with fsmonitor daemons whose worktree no longer exists- `wt config state clear` — clears config keys, caches, markers, hints, variables, logs, and stale trash- `wt config shell install` — replaces an existing fish or Nushell wrapper file whole, and removes one an older version installed at a previous location (fish `conf.d/wt.fish`, Nushell `<config-dir>/vendor/autoload/wt.nu`), printing each removal; [Files created](https://worktrunk.dev/shell-integration/#files-created) names the file each shell gets- `wt config shell uninstall` — removes the lines that run `wt config shell init` from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Every removed line is printeddiff --git a/src/cli/mod.rs b/src/cli/mod.rsindex 2251e95e1..187a463c3 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -2710,6 +2710,10 @@ Hooks, aliases and `step.copy-ignored.exclude` accumulate rather than replace, sforce_delete: bool,},+ /// Delete the worktrees staged in a trash directory.+ #[command(name = "_drain-trash", hide = true)]+ InternalDrainTrash { trash_dir: std::path::PathBuf },+/// Run a custom `wt-<name>` command found on PATH.////// Captured by clap when the first positional argument doesn't match anydiff --git a/src/commands/config/state.rs b/src/commands/config/state.rsindex fb5b1a124..4abde760e 100644--- a/src/commands/config/state.rs+++ b/src/commands/config/state.rs@@ -79,6 +79,8 @@ use color_print::cformat;use path_slash::PathExt as _;use worktrunk::git::{BranchRef, CommandError, Repository, resolve_input_path, sha_cache};use worktrunk::path::{format_path_for_display, sanitize_for_filename};+use worktrunk::progress::Progress;+use worktrunk::remove_dir::remove_dir_with_progress;use worktrunk::styling::{eprintln, format_heading, format_with_gutter, hint_message, info_message, println,success_message, warning_message,@@ -207,7 +209,7 @@ fn sort_hook_entries(entries: &mut [HookOutputEntry]) {/// A top-level entry staged under `wt_trash_dir()`.////// Worktree removal renames directories into `.git/wt/trash/<name>-<timestamp>`-/// and a background `rm -rf` cleans them up; entries still present here are+/// and a background trash drain deletes them; entries still present here are/// awaiting (or escaped) that sweep.struct TrashEntry {/// Filename, e.g. `myproject.feature-1234567890`.@@ -222,7 +224,7 @@ struct TrashEntry {/// Only the first level matters — each entry is one staged worktree (a/// directory) or a stray file. Sorted by mtime (newest first) with name as/// tie-breaker. Individual dirent/metadata failures are skipped: `state get`-/// is a read-only inspector and can race with the background `rm -rf`, so a+/// is a read-only inspector and can race with the background trash drain, so a/// partial listing is more useful than a hard failure.fn list_trash_entries(repo: &Repository) -> anyhow::Result<Vec<TrashEntry>> {let trash_dir = repo.wt_trash_dir();@@ -252,7 +254,7 @@ fn list_trash_entries(repo: &Repository) -> anyhow::Result<Vec<TrashEntry>> {/// Clear stale entries from the wt/trash directory.////// Worktree removal renames directories into `.git/wt/trash/` for instant UX,-/// then deletes them in a background process. If the background `rm -rf` fails+/// then deletes them in a background process. If the background drain fails/// or is killed, entries accumulate. This cleans them up.fn clear_trash(repo: &Repository) -> anyhow::Result<usize> {let trash_dir = repo.wt_trash_dir();@@ -263,13 +265,12 @@ fn clear_trash(repo: &Repository) -> anyhow::Result<usize> {let mut cleared = 0;for entry in std::fs::read_dir(&trash_dir)? {- let entry = entry?;- let path = entry.path();- if path.is_dir() {- std::fs::remove_dir_all(&path)?;- } else {- std::fs::remove_file(&path)?;- }+ let path = entry?.path();+ anyhow::ensure!(+ remove_dir_with_progress(&path, &Progress::disabled()),+ "Failed to remove {}",+ format_path_for_display(&path)+ );cleared += 1;}diff --git a/src/commands/process.rs b/src/commands/process.rsindex 6dd65eda1..84dafdd10 100644--- a/src/commands/process.rs+++ b/src/commands/process.rs@@ -7,7 +7,6 @@ use std::process::Command;use std::process::Stdio;use worktrunk::git::{HookType, Repository};use worktrunk::path::{format_path_for_display, sanitize_for_filename};-use worktrunk::utils::epoch_now;use worktrunk::config::HookSource;@@ -342,8 +341,8 @@ fn spawn_detached_exec_windows(////// Steps:///-/// 1. [`sweep_stale_trash`] — delete stale `.git/wt/trash/` entries left by-/// an interrupted background removal.+/// 1. [`sweep_trash`] — restart deletion of `.git/wt/trash/` entries left+/// by an interrupted background removal./// 2. [`worktrunk::git::fsmonitor::reap_orphan_fsmonitor_daemons`] — terminate/// `git fsmonitor--daemon` processes whose worktree no longer exists./// Defense-in-depth for daemons orphaned by paths that bypass `wt remove`@@ -351,93 +350,60 @@ fn spawn_detached_exec_windows(/// `wt remove` source itself stops the daemon synchronously.pub fn run_internal_sweep(repo: &Repository) {let _span = worktrunk::trace::Span::new("internal-sweep");- sweep_stale_trash(repo);+ sweep_trash(repo);worktrunk::git::fsmonitor::reap_orphan_fsmonitor_daemons(repo);}-/// How old a `.git/wt/trash/` entry must be before [`sweep_stale_trash`] deletes it.-pub const TRASH_STALE_THRESHOLD_SECS: u64 = 24 * 60 * 60;--/// Fire-and-forget cleanup of stale entries in `.git/wt/trash/`.+/// Fire-and-forget restart of trash deletion.///-/// Worktree removal uses a fast path that renames the worktree into-/// `.git/wt/trash/<name>-<timestamp>/` and deletes it in a detached background-/// process. If that process is interrupted (SIGKILL, reboot, disk full), the-/// renamed directory is orphaned. `wt remove` calls this function after its-/// primary user-visible output — so the sweep never delays the progress or-/// success message — to provide eventual cleanup: entries older than-/// [`TRASH_STALE_THRESHOLD_SECS`] are removed by a single detached `rm -rf`.+/// Worktree removal renames the worktree into+/// `.git/wt/trash/<name>-<timestamp>/` and starts a detached+/// [`worktrunk::git::drain_trash`] to delete it. If that process is+/// interrupted (SIGKILL, reboot, disk full), its entries stay behind. `wt+/// remove` calls this function after its primary user-visible output — so the+/// sweep never delays the progress or success message — and starts another+/// drain when the trash holds anything and no drain is running.///-/// Best effort: directory read failures and spawn failures are logged at debug-/// level and otherwise ignored. The sweep is purely additive — the primary-/// `wt remove` operation proceeds regardless of outcome.-pub fn sweep_stale_trash(repo: &Repository) {+/// Best effort: spawn failures are logged at debug level and otherwise+/// ignored. The sweep is purely additive — the primary `wt remove` operation+/// proceeds regardless of outcome.+pub fn sweep_trash(repo: &Repository) {let trash_dir = repo.wt_trash_dir();- let stale = collect_stale_trash_entries(&trash_dir, epoch_now(), TRASH_STALE_THRESHOLD_SECS);- if stale.is_empty() {+ if worktrunk::git::staged_trash_entries(&trash_dir).is_empty()+ || worktrunk::git::trash_drain_running(&trash_dir)+ {return;}- let command = build_remove_paths_command(&stale);-// The sweep is repo-wide (not branch-scoped), so it logs to a top-level// shared file alongside `commands.jsonl` and `trace.log`. The branch// argument is ignored for the `Shared` variant.- if let Err(e) = spawn_detached(- repo,- &repo.wt_dir(),- &command,- "",- &HookLog::Shared(InternalOp::TrashSweep),- ) {+ let spawned = std::env::current_exe()+ .context("Failed to locate Worktrunk executable")+ .and_then(|executable| {+ spawn_detached(+ repo,+ &repo.wt_dir(),+ &build_drain_trash_command(executable, &trash_dir),+ "",+ &HookLog::Shared(InternalOp::TrashSweep),+ )+ });+ if let Err(e) = spawned {tracing::debug!(error = %format!("{e:#}"), "Failed to spawn stale trash sweep");}}-/// Shell argv: a fixed script, its `$0`, then native path arguments.-/// Paths are never interpolated into shell source.-pub fn build_remove_paths_command(- paths: impl IntoIterator<Item = impl AsRef<OsStr>>,-) -> Vec<OsString> {- let mut args = vec![- OsString::from(r#"exec rm -rf -- "$@""#),+/// Shell argv that runs `wt _drain-trash <trash_dir>`: a fixed script, its+/// `$0`, then native arguments. Paths are never interpolated into shell source.+pub fn build_drain_trash_command(executable: PathBuf, trash_dir: &Path) -> Vec<OsString> {+ vec![+ OsString::from(r#"exec "$@""#),OsString::from("wt"),- ];- args.extend(paths.into_iter().map(|path| path.as_ref().to_os_string()));- args-}--/// Collect paths in `trash_dir` whose embedded timestamp is older than-/// `threshold_secs` relative to `now`.-///-/// Entries whose filename can't be parsed as `<name>-<timestamp>` are skipped —-/// the sweep only touches directories worktrunk created via-/// [`worktrunk::git::remove::stage_worktree_removal`].-fn collect_stale_trash_entries(trash_dir: &Path, now: u64, threshold_secs: u64) -> Vec<PathBuf> {- let Ok(read_dir) = fs::read_dir(trash_dir) else {- return Vec::new();- };-- read_dir- .filter_map(|entry| entry.ok())- .filter_map(|entry| {- let name = entry.file_name();- let timestamp = parse_trash_entry_timestamp(name.to_str()?)?;- let age = now.saturating_sub(timestamp);- (age >= threshold_secs).then(|| entry.path())- })- .collect()-}--/// Extract the Unix timestamp suffix from a trash entry filename.-///-/// Filenames produced by [`worktrunk::git::remove::stage_worktree_removal`]-/// have the form `<name>-<timestamp>`, where timestamp is a bare unsigned-/// integer in Unix epoch seconds. The worktree name may contain hyphens, so-/// splitting from the right and parsing the tail is unambiguous.-fn parse_trash_entry_timestamp(name: &str) -> Option<u64> {- let (_, suffix) = name.rsplit_once('-')?;- suffix.parse::<u64>().ok()+ executable.into_os_string(),+ OsString::from("_drain-trash"),+ trash_dir.as_os_str().to_os_string(),+ ]}/// Remove the empty shell-PWD placeholder after the wrapper consumes its cd directive.@@ -536,25 +502,12 @@ mod tests {}#[test]- fn removal_arguments_do_not_interpret_paths() {- let tmp = tempfile::tempdir().unwrap();- let name = if cfg!(windows) {- "with space-$(echo PWNED)"- } else {- "with space-'$(echo PWNED)\n"- };- let path = tmp.path().join(name);- fs::create_dir(&path).unwrap();- fs::write(path.join("payload"), "keep exact target").unwrap();- let args = build_remove_paths_command([&path]);- let shell = worktrunk::shell_exec::ShellConfig::get().unwrap();- let output = worktrunk::shell_exec::Cmd::new(shell.executable.to_str().unwrap())- .args(&shell.args)- .args(&args)- .run()- .unwrap();- assert!(output.status.success(), "{output:?}");- assert!(!path.exists());+ fn drain_command_passes_trash_dir_as_its_own_argument() {+ let trash_dir = Path::new("/repo/with space-'$(echo PWNED)\n/.git/wt/trash");+ let args = build_drain_trash_command(PathBuf::from("/bin/wt"), trash_dir);+ assert_eq!(args[0], r#"exec "$@""#);+ assert_eq!(args[3], "_drain-trash");+ assert_eq!(args[4], trash_dir.as_os_str());}#[test]@@ -600,62 +553,4 @@ mod tests {@"/repo/.git/wt/logs/internal-trash-sweep.log");}-- #[test]- fn test_parse_trash_entry_timestamp() {- // Simple name with trailing timestamp- assert_eq!(- parse_trash_entry_timestamp("feature-1700000000"),- Some(1700000000)- );- // Worktree name containing hyphens — split from the right- assert_eq!(- parse_trash_entry_timestamp("my-project.feature-branch-1700000000"),- Some(1700000000)- );- // Missing separator or non-numeric suffix → None (sweep leaves it alone)- assert_eq!(parse_trash_entry_timestamp("no-timestamp"), None);- assert_eq!(parse_trash_entry_timestamp("notimestamp"), None);- assert_eq!(parse_trash_entry_timestamp(""), None);- }-- #[test]- fn test_collect_stale_trash_entries() {- let trash = tempfile::tempdir().unwrap();- let now: u64 = 1_700_000_000;- let day = TRASH_STALE_THRESHOLD_SECS;-- // Stale: 2 days old- let stale = trash.path().join(format!("feature-old-{}", now - 2 * day));- fs::create_dir(&stale).unwrap();- // Fresh: 1 hour old- let fresh = trash.path().join(format!("feature-new-{}", now - 3600));- fs::create_dir(&fresh).unwrap();- // Exactly at threshold: 1 day old (inclusive)- let boundary = trash.path().join(format!("feature-edge-{}", now - day));- fs::create_dir(&boundary).unwrap();- // Unparsable: no timestamp suffix — sweep ignores it- let foreign = trash.path().join("random-folder");- fs::create_dir(&foreign).unwrap();-- let mut collected = collect_stale_trash_entries(trash.path(), now, day);- collected.sort();- let mut expected = vec![stale, boundary];- expected.sort();- assert_eq!(collected, expected);- assert!(- fresh.exists(),- "fresh entries must not appear in stale list"- );- assert!(foreign.exists(), "unparsable entries must be left alone");- }-- #[test]- fn test_collect_stale_trash_entries_missing_dir() {- let missing = std::path::PathBuf::from("/nonexistent/wt/trash/path");- assert!(- collect_stale_trash_entries(&missing, 1_700_000_000, TRASH_STALE_THRESHOLD_SECS)- .is_empty()- );- }}diff --git a/src/commands/remove.rs b/src/commands/remove.rsindex dea1d9075..ed597db7a 100644--- a/src/commands/remove.rs+++ b/src/commands/remove.rs@@ -363,13 +363,12 @@ fn maybe_reap_result(result: &RemovalPlan, reap_enabled: bool) {/// `.git/wt/trash/<name>-<timestamp>/` → prune metadata → delete branch/// → synchronous `remove_dir_all` on the staged directory./// - **Background** (default): stop fsmonitor → rename + prune +-/// synchronous branch delete → spawn detached `rm -rf` on the staged-/// directory. Cross-filesystem or locked worktrees fall back to+/// synchronous branch delete → spawn a detached drain of the trash+/// directory (`wt _drain-trash`). Cross-filesystem or locked worktrees fall back to/// `git worktree remove` in the detached process./// 4. **Post-remove hooks** run in the background after dispatch.-/// 5. **Internal sweep** (fire-and-forget, after primary output): stale-/// `.git/wt/trash/` entries older than 24 hours are removed by a detached-/// `rm -rf`, and orphaned `git fsmonitor--daemon` processes (worktree gone)+/// 5. **Internal sweep** (fire-and-forget, after primary output): entries+/// left in `.git/wt/trash/` start another detached drain, and orphaned `git fsmonitor--daemon` processes (worktree gone)/// are terminated. Runs last so it never delays the user-visible progress/// or success message. See [`super::process::run_internal_sweep`].pub fn handle_remove_command(args: RemoveArgs, yes: bool) -> anyhow::Result<()> {diff --git a/src/git/mod.rs b/src/git/mod.rsindex aebc9848a..592257686 100644--- a/src/git/mod.rs+++ b/src/git/mod.rs@@ -138,8 +138,8 @@ pub use parse::{parse_porcelain_z, parse_untracked_files};pub use recover::{current_or_recover, cwd_removed_hint};pub use remove::{BranchDeletionMode, BranchDeletionOutcome, BranchDeletionResult, RemovalOutput, RemoveOptions,- delete_branch_if_safe, execute_branch_deletion, remove_worktree_with_cleanup,- stage_worktree_removal, stop_fsmonitor_daemon,+ delete_branch_if_safe, drain_trash, execute_branch_deletion, remove_worktree_with_cleanup,+ stage_worktree_removal, staged_trash_entries, stop_fsmonitor_daemon, trash_drain_running,};pub use repository::sha_cache;pub use repository::{diff --git a/src/git/remove.rs b/src/git/remove.rsindex dc4bd5cba..00752b4c6 100644--- a/src/git/remove.rs+++ b/src/git/remove.rs@@ -11,9 +11,9 @@//! fallback and branch deletion, run to completion synchronously.//!//! The split exists because the default path defers its deletion: `wt remove`-//! and `wt merge --remove` stage the worktree here, then hand the `rm -rf` to-//! a detached process so the command returns as soon as the workspace is-//! clear. They build that tail themselves rather than calling+//! and `wt merge --remove` stage the worktree here, then hand the deletion to+//! a detached [`drain_trash`] so the command returns as soon as the workspace+//! is clear. They build that tail themselves rather than calling//! [`remove_worktree_with_cleanup`]. The callers that run to completion —//! `--foreground` removals, the TUI picker, and external tooling (e.g.//! `worktrunk-sync`) — call it and get the fallback and integration-checked@@ -40,7 +40,8 @@//! `<git-common-dir>/wt/trash/<name>-<timestamp>/`. Same-filesystem renames//! are instant metadata operations, so the user's workspace clears//! immediately. The caller is responsible for eventually removing the-//! staged path — either synchronously or via a background process.+//! staged path — either synchronously or via [`drain_trash`] in a+//! background process.//! 4. **Fallback removal.** If the rename fails (cross-filesystem, permission//! denied, Windows file locks), the code falls back to `git worktree remove`//! (optionally with `--force`), which deletes files directly.@@ -98,6 +99,8 @@//! # Ok::<(), anyhow::Error>(())//! ```+use std::collections::HashSet;+use std::fs::{self, TryLockError};use std::path::{Path, PathBuf};use std::time::Duration;@@ -106,6 +109,8 @@ use anyhow::Context as _;use crate::git::repository::WorkingTree;use crate::git::{ErrorExt, GitError, IntegrationReason, Repository, WorktreeInfo, path_dir_name};use crate::path::format_path_for_display;+use crate::progress::Progress;+use crate::remove_dir::remove_dir_with_progress;use crate::shell_exec::Cmd;use crate::styling::{eprintln, format_with_gutter, warning_message};use crate::utils::epoch_now;@@ -139,8 +144,8 @@ const FSMONITOR_LSOF_TIMEOUT: Duration = Duration::from_secs(2);/// while the worktree path still exists** (the socket lives under the/// per-worktree git dir and is needed to resolve the owning PID), so its one/// caller is [`stage_worktree_removal`], which every removal path runs in the-/// foreground before the directory is staged or pruned. The detached `rm -rf`-/// background process never touches the daemon; keeping daemon management in+/// foreground before the directory is staged or pruned. The detached trash drain+/// never touches the daemon; keeping daemon management in/// the Rust foreground avoids reimplementing socket/PID resolution and signal/// escalation as a shell string.///@@ -346,7 +351,7 @@ pub struct RemoveOptions {////// `staged_path` is `Some` only on the fast path. Callers are responsible for/// cleaning up the staged directory; `wt remove` does this with a detached-/// background `rm -rf` so the foreground command returns immediately.+/// [`drain_trash`] so the foreground command returns immediately.pub struct RemovalOutput {pub branch_result: Option<anyhow::Result<BranchDeletionResult>>,/// Path to the staged trash directory on the fast path.@@ -413,7 +418,7 @@ pub fn remove_worktree_with_cleanup(/// Every removal path runs this, in the foreground, before the worktree/// directory stops existing: the synchronous/// [`remove_worktree_with_cleanup`], and the default background path, which-/// stages here and hands the `rm -rf` to a detached process. Keeping the three+/// stages here and hands the deletion to a detached [`drain_trash`]. Keeping the three/// steps together is what makes the dirty-worktree gate a single decision/// rather than a sequence each caller re-assembles — the order below is easy/// to get subtly wrong, and getting it wrong destroys uncommitted work.@@ -752,12 +757,151 @@ pub(crate) fn generate_removing_path(trash_dir: &Path, git_dir: &Path) -> PathButrash_dir.join(format!("{}-{}", name, timestamp))}+/// Entries in `trash_dir` named `<name>-<timestamp>`, as removal stages them.+///+/// Anything else in the directory was not staged by worktrunk and is left+/// alone. A missing directory has no entries.+pub fn staged_trash_entries(trash_dir: &Path) -> Vec<PathBuf> {+ let Ok(read_dir) = fs::read_dir(trash_dir) else {+ return Vec::new();+ };+ read_dir+ .filter_map(|entry| entry.ok())+ .filter(|entry| {+ entry.file_name().to_str().is_some_and(|name| {+ name.rsplit_once('-')+ .is_some_and(|(_, timestamp)| timestamp.parse::<u64>().is_ok())+ })+ })+ .map(|entry| entry.path())+ .collect()+}++/// Whether a drain holds `trash_dir`'s lock.+///+/// A removal that finds one running skips starting another: the running drain+/// lists the trash again before it exits. The check holds the lock for an+/// instant, so a drain starting at that moment may exit early; the check then+/// reports no drain running and the caller starts one.+pub fn trash_drain_running(trash_dir: &Path) -> bool {+ open_drain_lock(trash_dir)+ .is_ok_and(|lock| matches!(lock.try_lock(), Err(TryLockError::WouldBlock)))+}++fn open_drain_lock(trash_dir: &Path) -> anyhow::Result<fs::File> {+ let lock_path = trash_dir.with_extension("lock");+ fs::OpenOptions::new()+ .create(true)+ .truncate(false)+ .write(true)+ .open(&lock_path)+ .with_context(|| format!("Failed to open {}", format_path_for_display(&lock_path)))+}++/// Delete every staged entry in `trash_dir`, unless another drain is running.+///+/// Every staged removal starts a drain, so a prune starts one per worktree.+/// Concurrent deletions on one filesystem add CPU without adding throughput:+/// a 193-worktree prune ran 193 `rm -rf` processes using about three cores to+/// delete 1,500 files/s, where one process alone deleted 3,000–5,000 files/s+/// at under one core. So one drain per trash directory does the deleting.+///+/// The running drain holds an exclusive lock on `<trash_dir>.lock`. A drain+/// that finds the lock held exits and leaves its entry to the holder, which+/// lists the directory again after each pass. After releasing the lock, the+/// holder lists once more and, if it finds a new entry, takes the lock again:+/// an entry staged while the holder was releasing it would otherwise wait for+/// the next removal.+///+/// An entry that a drain can't fully delete, such as one holding another+/// user's files, stays in the trash for a later drain rather than being+/// retried in a loop.+pub fn drain_trash(trash_dir: &Path) -> anyhow::Result<()> {+ let mut undeletable = HashSet::new();+ loop {+ let lock = open_drain_lock(trash_dir)?;+ match lock.try_lock() {+ Err(TryLockError::WouldBlock) => return Ok(()),+ result => result+ .map_err(std::io::Error::from)+ .context("Failed to lock the trash for deletion")?,+ }+ loop {+ let pending: Vec<_> = staged_trash_entries(trash_dir)+ .into_iter()+ .filter(|entry| !undeletable.contains(entry))+ .collect();+ if pending.is_empty() {+ break;+ }+ for entry in pending {+ if !remove_dir_with_progress(&entry, &Progress::disabled()) {+ undeletable.insert(entry);+ }+ }+ }+ drop(lock);+ if staged_trash_entries(trash_dir)+ .iter()+ .all(|entry| undeletable.contains(entry))+ {+ return Ok(());+ }+ }+}+#[cfg(test)]mod tests {use super::*;use crate::git::ErrorExt;use crate::testing::TestRepo;+ /// A drain deletes worktrunk's staged entries and leaves anything else; a+ /// drain that finds another holding the lock leaves the entries to it.+ #[test]+ fn drain_trash_deletes_staged_entries_once_the_lock_is_free() {+ let temp = tempfile::tempdir().unwrap();+ let trash = temp.path().join("trash");+ let staged = trash.join("repo.feature-branch-1700000000");+ let foreign = trash.join("not-staged");+ fs::create_dir_all(staged.join("node_modules/pkg")).unwrap();+ fs::write(staged.join("node_modules/pkg/index.js"), "x").unwrap();+ fs::create_dir_all(&foreign).unwrap();+ fs::write(trash.join("stray-file-1700000000"), "a staged file").unwrap();+ assert_eq!(staged_trash_entries(&trash).len(), 2);++ assert!(!trash_drain_running(&trash));+ let holder = fs::File::create(temp.path().join("trash.lock")).unwrap();+ holder.lock().unwrap();+ assert!(trash_drain_running(&trash));+ drain_trash(&trash).unwrap();+ assert!(staged.exists(), "the lock holder owns the deletion");++ drop(holder);+ drain_trash(&trash).unwrap();+ assert!(staged_trash_entries(&trash).is_empty());+ assert!(foreign.exists());+ assert!(staged_trash_entries(&temp.path().join("missing")).is_empty());+ }++ /// An entry the drain can't delete ends the drain rather than looping.+ #[cfg(unix)]+ #[test]+ fn drain_trash_stops_at_an_undeletable_entry() {+ use std::os::unix::fs::PermissionsExt;++ let temp = tempfile::tempdir().unwrap();+ let trash = temp.path().join("trash");+ fs::create_dir(&trash).unwrap();+ fs::write(trash.join("stuck-1700000000"), "x").unwrap();+ // Unlinking an entry needs write permission on the trash itself.+ fs::set_permissions(&trash, fs::Permissions::from_mode(0o555)).unwrap();++ drain_trash(&trash).unwrap();++ fs::set_permissions(&trash, fs::Permissions::from_mode(0o755)).unwrap();+ }+/// A `git worktree lock` must stop the rename even when the caller skipped/// `prepare_worktree_removal` (merge used to construct a plan by hand).#[test]diff --git a/src/main.rs b/src/main.rsindex 3c087ccb9..30acbe106 100644--- a/src/main.rs+++ b/src/main.rs@@ -971,6 +971,7 @@ fn dispatch_command(expected_tip.as_deref(),force_delete,),+ Commands::InternalDrainTrash { trash_dir } => worktrunk::git::drain_trash(&trash_dir),// `working_dir` is the top-level `-C <path>` flag, applied as the// child's current directory so global `-C` works for custom// subcommands the same way it does for built-ins.diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 3874ebeab..1754a2487 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -16,7 +16,7 @@ use crate::commands::command_executor::FailureStrategy;use crate::commands::hook_plan::{ApprovedHookPlan, execute_planned_hook, register_planned};use crate::commands::hooks::HookAnnouncer;use crate::commands::process::{- HookLog, InternalOp, build_remove_command, build_remove_paths_command,+ HookLog, InternalOp, build_drain_trash_command, build_remove_command,build_remove_placeholder_command, spawn_detached,};use crate::commands::template_vars::TemplateVars;@@ -121,8 +121,8 @@ pub enum RemovalExecution {/// Remove inline and report actual outcomes: progress message, spinner/// over the trash cleanup, success message from what really happened.Foreground,- /// Announce, then stage the worktree into trash and hand the `rm -rf` to a- /// detached process; the branch deletion still runs synchronously on the+ /// Announce, then stage the worktree into trash and hand its deletion to a+ /// detached trash drain; the branch deletion still runs synchronously on the/// fast path. The mode says what happens when the rename-into-trash fails.Background(BackgroundFallbackMode),/// The TUI (picker) path: the same inline removal as `Foreground` with no@@ -147,7 +147,7 @@ struct BackgroundRemovalOutcome {}/// Spawn background worktree removal: clean-check, stop fsmonitor,-/// rename-then-prune, spawn detached rm.+/// rename-then-prune, spawn a detached trash drain.////// Shared sequence for both detached HEAD and branch background removal paths./// The caller is responsible for output messages before this call, and hooks@@ -191,7 +191,7 @@ fn spawn_background_removal(////// This function has side effects: it renames the worktree directory and prunes git metadata./// On the fast path, the branch is also deleted synchronously (since after prune, the branch-/// is no longer checked out in any worktree), and the background command is just `rm -rf`.+/// is no longer checked out in any worktree), and the background command is just a trash drain./// If rename fails (cross-filesystem, permissions, Windows file locking), either returns the/// legacy `git worktree remove` command with branch deletion deferred to the background, or/// runs that fallback synchronously for non-current worktrees when the caller needs the@@ -218,20 +218,29 @@ fn execute_instant_removal_or_fallback(// Dirty-worktree gate, fsmonitor stop, then the rename into .git/wt/trash/// (instant on same filesystem) — the same prelude the synchronous// `remove_worktree_with_cleanup` runs. On the fast path the background- // process is then just an `rm -rf`.- if let Some(staged_path) =- stage_worktree_removal(repo, worktree_path, branch_name, force_worktree)?- {+ // process is then just a trash drain.+ if stage_worktree_removal(repo, worktree_path, branch_name, force_worktree)?.is_some() {if changed_directory {// Keep the shell's PWD valid until its cd directive is consumed.let _ = std::fs::create_dir(worktree_path);}// Trash cleanup is independent of branch deletion. Start it before a// deletion lock wait, and still attempt deletion if scheduling fails.- let cleanup = spawn_cleanup(- &build_remove_paths_command([&staged_path]),- InternalOp::Remove,- );+ // A running drain deletes this entry too, so a prune starts one+ // drain rather than one per worktree.+ let trash_dir = repo.wt_trash_dir();+ let cleanup = if worktrunk::git::trash_drain_running(&trash_dir) {+ Ok(())+ } else {+ std::env::current_exe()+ .context("Failed to locate Worktrunk executable")+ .and_then(|executable| {+ spawn_cleanup(+ &build_drain_trash_command(executable, &trash_dir),+ InternalOp::Remove,+ )+ })+ };// Delete branch synchronously now that prune has removed the worktree metadata.// Fresh refs, not the pre-hook planning decision: hooks or concurrent// processes may have advanced the branch (`execute_branch_deletion`).@@ -2479,17 +2488,13 @@ mod tests {},BackgroundFallbackMode::Detached,|args, _| {- let shell = worktrunk::shell_exec::ShellConfig::get()?;- let output = Cmd::new(shell.executable.to_str().unwrap())- .args(&shell.args)- .args(args)- .run()?;- assert!(output.status.success());- Ok(())+ assert_eq!(args.last().unwrap(), repo.wt_trash_dir().as_os_str());+ worktrunk::git::drain_trash(&repo.wt_trash_dir())},).unwrap();assert!(!checkout.exists());+ assert!(worktrunk::git::staged_trash_entries(&repo.wt_trash_dir()).is_empty());assert!(!registration.exists());assert!(repo.run_command(&["rev-parse", "--verify", "feature"])diff --git a/src/remove_dir.rs b/src/remove_dir.rsindex e1de4bb6b..30373775d 100644--- a/src/remove_dir.rs+++ b/src/remove_dir.rs@@ -1,92 +1,248 @@//! Recursive directory removal with progress reporting.//!-//! Walks the tree iteratively (no recursion), unlinks files in parallel, then-//! removes the now-empty directories deepest-first. Each unlinked leaf bumps-//! a [`Progress`] counter so a TTY spinner can render live updates; the-//! caller reads `progress.totals()` to drive the matching post-op summary.+//! Each unlinked leaf bumps a [`Progress`] counter so a TTY spinner can render+//! live updates; the caller reads `progress.totals()` to drive the matching+//! post-op summary.//!//! Best-effort by design: this is the trash-cleanup phase that runs *after*//! the worktree has already been pruned from git's metadata, so a partial//! cleanup leaves the user with a stale directory under `.git/wt/trash/` —//! recoverable, not catastrophic. read/unlink/rmdir errors are swallowed so//! the caller can always report the count of leaves we did manage to remove.-//! Parallel unlink runs on a dedicated 4-thread pool — file removal is-//! filesystem latency, not CPU, so a small pool gives most of the speedup-//! without the per-process thread cost of rayon's default `2× CPU cores`-//! global pool (which adds up across many subprocesses in test runs).--use std::fs;-use std::path::{Path, PathBuf};-use std::sync::LazyLock;+//!+//! The removal never follows a symlink or junction, at the root or below it,+//! so nothing outside the tree is deleted, even while another process changes+//! the tree. On Unix each directory is opened relative to its parent's file+//! descriptor with `O_NOFOLLOW`, and entries are unlinked relative to the+//! directory that holds them; a directory swapped for a symlink mid-walk is+//! unlinked as a link. Elsewhere `std::fs::remove_dir_all` provides the same+//! guarantee. The Unix walk also gives the owner read, write, and search+//! permission on each directory it enters: listing a directory and unlinking+//! its entries need them, and installers leave read-only directories+//! (`dr-xr-xr-x`) that would otherwise strand the whole tree. A directory+//! owned by another user stays as it is.+//!+//! Unix unlinks run on a dedicated 4-thread pool, each directory's files in+//! parallel — file removal is filesystem latency, not CPU, so a small pool+//! gives most of the speedup without the per-process thread cost of rayon's+//! default `2× CPU cores` global pool (which adds up across many subprocesses+//! in test runs). On a 199k-entry pnpm `node_modules` under `taskpolicy -b`,+//! this deleted about twice as fast as `rm -rf`.-use rayon::prelude::*;+use std::path::Path;use crate::progress::{DataCopy, Progress};-/// Capped at 4 threads — same reasoning as `copy::COPY_POOL` (filesystem I/O,-/// don't oversubscribe CPU when many wt subprocesses run in parallel).-static REMOVE_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {- rayon::ThreadPoolBuilder::new()- .num_threads(4)- .build()- .expect("failed to build remove thread pool")-});-/// Remove a directory tree, reporting per-file progress.////// Each successful unlink is recorded on `progress`, so a `progress` dedicated/// to this call reports `(files_removed, bytes_removed)` in `totals()`-/// afterwards. Bytes use `symlink_metadata`, so symlinks count as their own-/// size, not the target's. Any I/O errors are silently skipped — a leaf that-/// can't be unlinked is simply not counted.-pub fn remove_dir_with_progress(path: &Path, progress: &Progress) {- let mut leaves: Vec<PathBuf> = Vec::new();- let mut dirs: Vec<PathBuf> = Vec::new();- let mut stack = vec![path.to_path_buf()];-- while let Some(dir) = stack.pop() {- // read_dir failure (NotFound, EACCES, anything else) → skip this- // subtree. We still try to rmdir it later in case it's actually empty.- let Ok(entries) = fs::read_dir(&dir) else {- dirs.push(dir);- continue;+/// afterwards. Symlinks count as their own size, not the target's. A symlink+/// or file at `path` is unlinked, as `rm -rf` does. A path without a final+/// name (`/`, `.`, `x/..`) is left alone, as `rm -rf` refuses it. Any I/O+/// errors are silently skipped — a leaf that can't be unlinked is simply not+/// counted.+///+/// Returns whether `path` is gone afterwards. A failure to inspect it counts+/// as still present.+pub fn remove_dir_with_progress(path: &Path, progress: &Progress) -> bool {+ if path.file_name().is_some() {+ tree::remove(path, progress);+ }+ matches!(path.symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound)+}++#[cfg(unix)]+mod tree {+ use std::os::fd::{AsFd, BorrowedFd};+ use std::path::Path;+ use std::sync::LazyLock;++ use rayon::prelude::*;+ use rustix::fs::{+ AtFlags, CWD, Dir, FileType, Mode, OFlags, RawMode, chmodat, fchmod, fstat, openat, statat,+ unlinkat,+ };+ use rustix::io::Errno;+ use rustix::path::Arg;++ use super::{DataCopy, Progress};++ /// Capped at 4 threads — same reasoning as `copy::COPY_POOL` (filesystem I/O,+ /// don't oversubscribe CPU when many wt subprocesses run in parallel).+ static REMOVE_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {+ rayon::ThreadPoolBuilder::new()+ .num_threads(4)+ .build()+ .expect("failed to build remove thread pool")+ });++ const DIR_FLAGS: OFlags = OFlags::RDONLY+ .union(OFlags::DIRECTORY)+ .union(OFlags::NOFOLLOW)+ .union(OFlags::CLOEXEC);++ pub(super) fn remove(path: &Path, progress: &Progress) {+ REMOVE_POOL.install(|| remove_entry(CWD, path, progress));+ }++ /// Remove `name` inside `parent`, recursing into it only when it is a real+ /// directory.+ fn remove_entry(parent: BorrowedFd<'_>, name: impl Arg + Copy, progress: &Progress) {+ let dir = match openat(parent, name, DIR_FLAGS, Mode::empty()) {+ Ok(dir) => dir,+ // `O_NOFOLLOW` on a symlink fails with ELOOP (ENOTDIR on some+ // systems); either way the entry is a leaf to unlink.+ Err(Errno::NOTDIR | Errno::LOOP) => return remove_leaf(parent, name, progress),+ // A directory its owner can't read or search.+ Err(Errno::ACCESS) => {+ let Some(dir) = grant_owner_access_at(parent, name)+ .and_then(|()| openat(parent, name, DIR_FLAGS, Mode::empty()).ok())+ else {+ return;+ };+ dir+ }+ Err(_) => return,};- // Push parents first; we'll reverse before rmdir so children unlink first.- dirs.push(dir);- for entry in entries.flatten() {- // entry.file_type() is an lstat on Unix — symlinks report- // `is_dir() == false` and fall through to the leaf branch, which- // is what we want (unlink the link, not the target).- let Ok(file_type) = entry.file_type() else {+ empty_dir(dir.as_fd(), progress);+ let _ = unlinkat(parent, name, AtFlags::REMOVEDIR);+ }++ fn empty_dir(dir: BorrowedFd<'_>, progress: &Progress) {+ // Opening it needed read and search; unlinking entries needs write.+ if let Some(mode) = fstat(dir)+ .ok()+ .and_then(|stat| with_owner_access(stat.st_mode))+ {+ let _ = fchmod(dir, mode);+ }+ let mut dirs = Vec::new();+ let mut leaves = Vec::new();+ for entry in Dir::read_from(dir).into_iter().flatten().flatten() {+ let name = entry.file_name();+ if name == c"." || name == c".." {continue;- };- let entry_path = entry.path();- if file_type.is_dir() {- stack.push(entry_path);- } else {- leaves.push(entry_path);}+ match entry.file_type() {+ // `remove_entry` opens without following symlinks and unlinks+ // whatever turns out not to be a directory.+ FileType::Directory | FileType::Unknown => dirs.push(name.to_owned()),+ _ => leaves.push(name.to_owned()),+ }+ }+ leaves+ .par_iter()+ .for_each(|name| remove_leaf(dir, name.as_c_str(), progress));+ // Subdirectories go one at a time, so the open descriptors number the+ // tree's depth.+ for name in &dirs {+ remove_entry(dir, name.as_c_str(), progress);}}- REMOVE_POOL.install(|| {- leaves.par_iter().for_each(|leaf| {- // Capture size before unlinking. Best-effort: symlink_metadata may- // fail on a racy delete, in which case we still try to unlink and- // count the leaf with zero bytes.- let bytes = leaf.symlink_metadata().map(|m| m.len()).unwrap_or(0);- if fs::remove_file(leaf).is_ok() {- // A removal frees extents rather than moving them, so it has no- // side of the reflink split to land on.+ fn remove_leaf(parent: BorrowedFd<'_>, name: impl Arg + Copy, progress: &Progress) {+ // Capture size before unlinking. Best-effort: a racy delete counts+ // the leaf with zero bytes.+ let bytes = statat(parent, name, AtFlags::SYMLINK_NOFOLLOW)+ .map(|stat| u64::try_from(stat.st_size).unwrap_or(0))+ .unwrap_or(0);+ if unlinkat(parent, name, AtFlags::empty()).is_ok() {+ // A removal frees extents rather than moving them, so it has no+ // side of the reflink split to land on.+ progress.record(bytes, DataCopy::Neither);+ }+ }++ /// Add the owner's `rwx` bits to the directory `name`, never through a+ /// symlink.+ #[cfg(not(target_os = "linux"))]+ fn grant_owner_access_at(parent: BorrowedFd<'_>, name: impl Arg + Copy) -> Option<()> {+ let stat = statat(parent, name, AtFlags::SYMLINK_NOFOLLOW).ok()?;+ let mode = with_owner_access(stat.st_mode)?;+ chmodat(parent, name, mode, AtFlags::SYMLINK_NOFOLLOW).ok()+ }++ /// Add the owner's `rwx` bits to the directory `name`, never through a+ /// symlink. Linux's `fchmodat` ignores `AT_SYMLINK_NOFOLLOW`, so this opens+ /// the directory with `O_PATH`, which needs no permission on it, and+ /// changes the mode through that descriptor's `/proc` link.+ #[cfg(target_os = "linux")]+ fn grant_owner_access_at(parent: BorrowedFd<'_>, name: impl Arg + Copy) -> Option<()> {+ use std::os::fd::AsRawFd;++ let dir = openat(+ parent,+ name,+ OFlags::PATH | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,+ Mode::empty(),+ )+ .ok()?;+ let mode = with_owner_access(fstat(&dir).ok()?.st_mode)?;+ let link = format!("/proc/self/fd/{}", dir.as_raw_fd());+ chmodat(CWD, link.as_str(), mode, AtFlags::empty()).ok()+ }++ /// `st_mode`'s permissions with the owner's `rwx` added, or `None` when+ /// the owner already has them.+ fn with_owner_access(st_mode: RawMode) -> Option<Mode> {+ let mode = Mode::from_raw_mode(st_mode);+ (!mode.contains(Mode::RWXU)).then_some(mode | Mode::RWXU)+ }+}++#[cfg(not(unix))]+mod tree {+ use std::fs;+ use std::path::Path;++ use super::{DataCopy, Progress};++ /// Measure the tree, then delete it with `fs::remove_dir_all`, which+ /// opens each directory relative to its parent's handle and removes links+ /// and junctions without following them, even while the tree changes.+ /// The leaves are recorded once the whole tree is gone.+ pub(super) fn remove(path: &Path, progress: &Progress) {+ let Ok(metadata) = path.symlink_metadata() else {+ return;+ };+ if !metadata.is_dir() {+ // A directory symlink or junction is removed as a directory; that+ // removes the link, not its target.+ if fs::remove_file(path)+ .or_else(|_| fs::remove_dir(path))+ .is_ok()+ {+ progress.record(metadata.len(), DataCopy::Neither);+ }+ return;+ }+ let mut leaf_sizes = Vec::new();+ let mut stack = vec![path.to_path_buf()];+ while let Some(dir) = stack.pop() {+ let Ok(entries) = fs::read_dir(&dir) else {+ continue;+ };+ for entry in entries.flatten() {+ // Symlinks and junctions report `is_dir() == false` and count+ // as leaves.+ let Ok(metadata) = entry.metadata() else {+ continue;+ };+ if metadata.is_dir() {+ stack.push(entry.path());+ } else {+ leaf_sizes.push(metadata.len());+ }+ }+ }+ if fs::remove_dir_all(path).is_ok() {+ for bytes in leaf_sizes {+ // A removal frees extents rather than moving them, so it has+ // no side of the reflink split to land on.progress.record(bytes, DataCopy::Neither);}- });- });-- // Pop order pushed parents before children, so reversing gives- // deepest-first — exactly what `rmdir` needs.- for dir in dirs.iter().rev() {- let _ = fs::remove_dir(dir);+ }}}@@ -134,6 +290,20 @@ mod tests {assert_eq!(progress.totals(), (0, 0));}+ #[test]+ fn test_remove_dir_with_progress_refuses_paths_without_a_name() {+ let temp = tempfile::tempdir().unwrap();+ let kept = temp.path().join("kept");+ std::fs::create_dir_all(kept.join("child")).unwrap();+ std::fs::write(kept.join("file.txt"), b"x").unwrap();++ let progress = Progress::disabled();+ assert!(!remove_dir_with_progress(&kept.join("child/.."), &progress));++ assert_eq!(progress.totals(), (0, 0));+ assert!(kept.join("file.txt").exists());+ }+#[cfg(unix)]#[test]fn test_remove_dir_with_progress_handles_symlinks() {@@ -153,44 +323,89 @@ mod tests {assert!(!root.exists());}- /// Cover the read_dir-fails path. Permission-denied is the realistic- /// non-NotFound failure; the function should silently skip the subtree- /// and still rmdir what it can. Skipped when running as root (the kernel- /// ignores mode-0 directories for euid 0).+ /// A directory symlink inside the tree is unlinked; the directory it+ /// points to keeps its files.#[cfg(unix)]#[test]- fn test_remove_dir_with_progress_skips_unreadable_subtree() {+ fn test_remove_dir_with_progress_keeps_directory_symlink_targets() {+ let temp = tempfile::tempdir().unwrap();+ let outside = temp.path().join("outside");+ std::fs::create_dir(&outside).unwrap();+ std::fs::write(outside.join("keep.txt"), b"abc").unwrap();+ let root = temp.path().join("tree");+ std::fs::create_dir_all(root.join("nested")).unwrap();+ std::os::unix::fs::symlink(&outside, root.join("nested/link")).unwrap();++ let progress = Progress::disabled();+ remove_dir_with_progress(&root, &progress);++ assert!(!root.exists());+ assert!(outside.join("keep.txt").exists());+ assert_eq!(progress.totals().0, 1);+ }++ /// A symlink passed as the root is unlinked; its target keeps its files.+ #[cfg(unix)]+ #[test]+ fn test_remove_dir_with_progress_unlinks_symlink_root() {+ let temp = tempfile::tempdir().unwrap();+ let target = temp.path().join("target");+ std::fs::create_dir(&target).unwrap();+ std::fs::write(target.join("keep.txt"), b"abc").unwrap();+ let link = temp.path().join("link");+ std::os::unix::fs::symlink(&target, &link).unwrap();++ let progress = Progress::disabled();+ remove_dir_with_progress(&link, &progress);++ assert!(link.symlink_metadata().is_err());+ assert!(target.join("keep.txt").exists());+ assert_eq!(progress.totals().0, 1);+ }++ /// Read-only directories, as installers leave them, are deleted along+ /// with their contents.+ #[cfg(unix)]+ #[test]+ fn test_remove_dir_with_progress_removes_read_only_directories() {use std::os::unix::fs::PermissionsExt;let temp = tempfile::tempdir().unwrap();let root = temp.path().join("tree");- let blocked = root.join("blocked");- std::fs::create_dir_all(&blocked).unwrap();- std::fs::write(blocked.join("hidden.txt"), b"x").unwrap();- // Make `blocked` unreadable. Also blocks rmdir of `blocked` itself,- // but that's fine — we want to exercise the read_dir error branch- // and verify the function returns gracefully.- std::fs::set_permissions(&blocked, std::fs::Permissions::from_mode(0o000)).unwrap();-- // Skip if running as root: euid 0 ignores DAC mode bits, so the- // walk would succeed and unlink hidden.txt. Probe via a read attempt- // — same pattern as the integration tests in tests/.- if std::fs::read_dir(&blocked).is_ok() {- std::fs::set_permissions(&blocked, std::fs::Permissions::from_mode(0o755)).ok();- eprintln!("Skipping - running with elevated privileges");- return;- }+ let read_only = root.join("read-only");+ std::fs::create_dir_all(read_only.join("nested")).unwrap();+ std::fs::write(read_only.join("nested/a.txt"), b"x").unwrap();+ std::fs::set_permissions(+ read_only.join("nested"),+ std::fs::Permissions::from_mode(0o555),+ )+ .unwrap();+ std::fs::set_permissions(&read_only, std::fs::Permissions::from_mode(0o555)).unwrap();let progress = Progress::disabled();remove_dir_with_progress(&root, &progress);- // Restore so the tempdir's Drop can clean up.- std::fs::set_permissions(&blocked, std::fs::Permissions::from_mode(0o755)).ok();+ assert_eq!(progress.totals(), (1, 1));+ assert!(!root.exists());+ }++ /// A directory its owner can't read is made readable and deleted.+ #[cfg(unix)]+ #[test]+ fn test_remove_dir_with_progress_removes_unreadable_directories() {+ use std::os::unix::fs::PermissionsExt;- assert_eq!(- progress.totals(),- (0, 0),- "no leaves should be unlinked when blocked"- );+ let temp = tempfile::tempdir().unwrap();+ let root = temp.path().join("tree");+ let unreadable = root.join("unreadable");+ std::fs::create_dir_all(&unreadable).unwrap();+ std::fs::write(unreadable.join("b.txt"), b"y").unwrap();+ std::fs::set_permissions(&unreadable, std::fs::Permissions::from_mode(0o000)).unwrap();++ let progress = Progress::disabled();+ remove_dir_with_progress(&root, &progress);++ assert_eq!(progress.totals(), (1, 1));+ assert!(!root.exists());}}diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex d10747682..8bbd5a240 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -4430,33 +4430,36 @@ fn test_remove_stale_staging_dir_from_crashed_removal(mut repo: TestRepo) {);}-/// `wt remove` sweeps `.git/wt/trash/` entries older than 24 hours.+/// `wt remove` deletes entries an interrupted removal left in `.git/wt/trash/`.///-/// Each run of `wt remove` fires a detached `rm -rf` on trash entries whose-/// encoded timestamp is more than a day in the past. This provides eventual-/// cleanup for directories orphaned when a previous background removal was-/// interrupted. Fresh entries (from recent or in-flight removals) are left-/// alone so concurrent removals don't race.+/// Each staged removal and each internal sweep starts a detached+/// `wt _drain-trash`, which deletes every staged entry, including read-only+/// directories that `rm -rf` can't delete. Entries not named+/// `<name>-<timestamp>` were not staged by worktrunk and stay.#[rstest]-fn test_remove_sweeps_stale_trash_entries(mut repo: TestRepo) {+fn test_remove_drains_leftover_trash_entries(mut repo: TestRepo) {let git_common_dir = crate::common::resolve_git_common_dir(repo.root_path());let trash_dir = git_common_dir.join("wt/trash");std::fs::create_dir_all(&trash_dir).unwrap();- // Pre-populate the trash directory with a stale entry (2 days old) and a- // fresh entry (just created). The stale entry should be swept; the fresh- // entry should survive.let day = 24 * 60 * 60;- let stale_timestamp = crate::common::TEST_EPOCH - 2 * day;- let stale_entry = trash_dir.join(format!("orphan-stale-{stale_timestamp}"));+ let stale_entry = trash_dir.join(format!(+ "orphan-stale-{}",+ crate::common::TEST_EPOCH - 2 * day+ ));let fresh_entry = trash_dir.join(format!("orphan-fresh-{}", crate::common::TEST_EPOCH));- std::fs::create_dir(&stale_entry).unwrap();- std::fs::write(stale_entry.join("marker"), "leftover").unwrap();- std::fs::create_dir(&fresh_entry).unwrap();- std::fs::write(fresh_entry.join("marker"), "recent").unwrap();+ let foreign_entry = trash_dir.join("not-staged");+ for entry in [&stale_entry, &fresh_entry, &foreign_entry] {+ std::fs::create_dir(entry).unwrap();+ std::fs::write(entry.join("marker"), "leftover").unwrap();+ }+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt;+ std::fs::set_permissions(&stale_entry, std::fs::Permissions::from_mode(0o555)).unwrap();+ }- // Create and remove a real worktree to trigger the sweep, which runs after- // the primary `wt remove` output has been printed.+ // Create and remove a real worktree to trigger the drain.let _ = repo.add_worktree("feature-sweep");let output = repo.wt_command()@@ -4469,14 +4472,57 @@ fn test_remove_sweeps_stale_trash_entries(mut repo: TestRepo) {String::from_utf8_lossy(&output.stderr));- // The stale entry is removed by a detached `rm -rf` — poll for absence.- crate::common::wait_for("stale trash entry swept", || !stale_entry.exists());+ crate::common::wait_for("leftover trash entries drained", || {+ !stale_entry.exists() && !fresh_entry.exists()+ });+ assert!(+ foreign_entry.exists(),+ "unstaged entries must be left alone"+ );+}- // The fresh entry must survive — only entries older than 24 hours are swept.+/// While another drain holds `trash.lock`, a removal leaves its staged entry+/// to that drain; the next removal after the lock frees deletes it.+#[rstest]+fn test_remove_leaves_entries_to_the_running_drain(mut repo: TestRepo) {+ let git_common_dir = crate::common::resolve_git_common_dir(repo.root_path());+ let trash_dir = git_common_dir.join("wt/trash");+ std::fs::create_dir_all(&trash_dir).unwrap();+ let holder = std::fs::File::create(git_common_dir.join("wt/trash.lock")).unwrap();+ holder.lock().unwrap();++ let _ = repo.add_worktree("feature-held");+ let output = repo+ .wt_command()+ .args(["remove", "feature-held"])+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "{}",+ String::from_utf8_lossy(&output.stderr)+ );+ let staged: Vec<_> = std::fs::read_dir(&trash_dir)+ .unwrap()+ .map(|entry| entry.unwrap().path())+ .collect();+ assert_eq!(staged.len(), 1, "the lock holder owns the deletion");++ drop(holder);+ let _ = repo.add_worktree("feature-next");+ let output = repo+ .wt_command()+ .args(["remove", "feature-next"])+ .output()+ .unwrap();assert!(- fresh_entry.exists(),- "fresh trash entry (age 0) must not be swept"+ output.status.success(),+ "{}",+ String::from_utf8_lossy(&output.stderr));+ crate::common::wait_for("trash drained after the lock frees", || {+ std::fs::read_dir(&trash_dir).unwrap().next().is_none()+ });}/// `wt remove -vv` resolves every fsmonitor daemon in ONE `lsof` spawn and@@ -4680,20 +4726,16 @@ fn test_remove_foreground_succeeds_with_stuck_directory(mut repo: TestRepo) {let stderr = String::from_utf8_lossy(&output.stderr);- // Restore permissions in trash dir so TempDir cleanup works- let git_dir = repo.root_path().join(".git");- let trash_dir = git_dir.join("wt").join("trash");- if trash_dir.exists() {- for entry in fs::read_dir(&trash_dir).unwrap().flatten() {- restore_dir_permissions(&entry.path());- }- }-assert!(output.status.success(),"Remove should succeed via fast path, got: {stderr}");assert!(!worktree_path.exists(), "Worktree directory should be gone");+ let trash_dir = repo.root_path().join(".git/wt/trash");+ assert!(+ fs::read_dir(&trash_dir).unwrap().next().is_none(),+ "the read-only directory should be deleted with the rest of the worktree"+ );}/// Same as above but for the detached HEAD code path.@@ -4735,20 +4777,16 @@ fn test_remove_foreground_succeeds_with_stuck_directory_detached(mut repo: TestRlet stderr = String::from_utf8_lossy(&output.stderr);- // Restore permissions in trash dir so TempDir cleanup works- let git_dir = repo.root_path().join(".git");- let trash_dir = git_dir.join("wt").join("trash");- if trash_dir.exists() {- for entry in fs::read_dir(&trash_dir).unwrap().flatten() {- restore_dir_permissions(&entry.path());- }- }-assert!(output.status.success(),"Remove should succeed via fast path, got: {stderr}");assert!(!worktree_path.exists(), "Worktree directory should be gone");+ let trash_dir = repo.root_path().join(".git/wt/trash");+ assert!(+ fs::read_dir(&trash_dir).unwrap().next().is_none(),+ "the read-only directory should be deleted with the rest of the worktree"+ );}/// Worktrees with initialized git submodules should be removable.@@ -5015,22 +5053,6 @@ fn test_remove_refuses_dirty_submodule_hidden_by_user_config(mut repo: TestRepo));}-/// Restore write permissions recursively so TempDir cleanup succeeds.-#[cfg(unix)]-fn restore_dir_permissions(dir: &std::path::Path) {- use std::fs::{self, Permissions};- use std::os::unix::fs::PermissionsExt;-- let _ = fs::set_permissions(dir, Permissions::from_mode(0o755));- if let Ok(entries) = fs::read_dir(dir) {- for entry in entries.flatten() {- if entry.file_type().is_ok_and(|t| t.is_dir()) {- restore_dir_permissions(&entry.path());- }- }- }-}-// ============================================================================// Docs-page example snapshot//diff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rsindex 0ec0e185a..24add5e5c 100644--- a/tests/integration_tests/step_prune.rs+++ b/tests/integration_tests/step_prune.rs@@ -2202,15 +2202,9 @@ fn test_removal_runs_post_remove_after_cleanup_spawn_failure("{stderr}");}+ // The trash entry may already be gone: `wt remove`'s sweep starts a drain+ // under the shared log, which the blocked branch log doesn't stop.assert!(!worktree.exists());- assert!(- common_dir- .join("wt/trash")- .read_dir()- .unwrap()- .next()- .is_some()- );crate::common::wait_for("post-remove hook after cleanup scheduling fails", || {marker.exists()});