Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

perf(list): settle picker rows top-down and fork rebase lookups concurrently (#4286)

Maximilian Roosd13e362cd23f
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rs
index fede7cf11..66fe6b684 100644
--- a/src/commands/list/collect/mod.rs
+++ b/src/commands/list/collect/mod.rs
@@ -24,8 +24,8 @@
//! | 1 | `git rev-parse --git-common-dir --is-inside-work-tree --show-toplevel --git-dir --symbolic-full-name HEAD` | [`Repository::prewarm`] (`prewarm_rev_parse`) | Five facts in one fork: shared `.git`, in-worktree gate, worktree root, per-worktree `.git/worktrees/<name>`, current branch. Populates process-global caches. Parallel with #2 at process startup. |
//! | 2 | `git config --list -z` (cwd = discovery path) | [`Repository::prewarm`] (`prewarm_git_config`) | Whole merged config (system + global + local) in one shot, NUL-delimited so values containing `\n` or `=` parse unambiguously. Stashed in `GIT_CONFIG_PRELOAD` keyed by discovery path; every later `config_last("…")` reads from memory. Parallel with #1. |
//! | 3 | `git config --list -z` (cwd = `git_common_dir`) | [`Repository::prewarm`] post-pass (`prewarm_git_config_from_common_dir`) | **Conditional.** [`Repository::at`] consumes #2's preload into `cache.all_config`, so `all_config()` is a memory hit on a normal repo. In `extensions.worktreeConfig=true` repos, #2's read is declined — `--list` from a linked worktree misses the main-worktree `config.worktree` overrides (most importantly `core.bare = true` for the `myproject/.git + sibling worktrees` layout) — and prewarm re-forks from the common dir after its threads join, preloading the full merged set. [`Repository::all_config`] forks the same command on demand only when prewarm never ran for the path (a non-base `Repository::at`, tests). See `prewarm_git_config` for the full reasoning. |
-//! | 4 | `git worktree list --porcelain` | [`Repository::list_worktrees`] | Path, HEAD SHA, branch, and flags per worktree — the row source for the skeleton. The picker prelude triggers this once for `num_items_estimate`; collect's rayon scope then hits the cache. |
-//! | 5 | `git for-each-ref --format=… refs/heads/` | [`Repository::local_branches`] inside collect's `rayon::scope` | Local branch tips (name, SHA, committer date, upstream) for branch-only rows (`branches=true`) and for the stale-default-branch check. `remotes=true` adds a sibling `refs/remotes/` fork. The scope joins; this fork gates the skeleton. |
+//! | 4 | `git worktree list --porcelain` | [`Repository::list_worktrees`] | Path, HEAD SHA, branch, and flags per worktree — the row source for the skeleton. The picker prelude triggers this once for `num_items_estimate`; collect's rayon scope then hits the cache. **Plus one `git rev-parse --git-dir` per detached worktree**, run inside the same cache initializer (`finalize_worktrees`): git lists a mid-rebase worktree only as detached, so each detached worktree's git dir is resolved and its `rebase-{merge,apply}/head-name` read to recover the branch. These are the one O(N) fork group before the skeleton — O(detached worktrees), forked together through `Cmd::run_concurrently`. |
+//! | 5 | `git for-each-ref --format=… refs/heads/` | [`Repository::local_branches`] inside collect's `rayon::scope` | Local branch tips (name, SHA, committer date, upstream) for branch-only rows (`branches=true`) and for the stale-default-branch check. `remotes=true` adds a sibling `refs/remotes/` fork. Runs beside #4, which is the longer of the two on a repo with many worktrees. The post-skeleton ref snapshot scans `refs/heads/` again rather than reusing this inventory (see "Pipeline"). |
//! | 6 | `git log --no-walk --no-show-signature --format=… SHA₁ … SHA_N` | collect, after the scope | Batched commit metadata for every worktree HEAD + branch tip. See breakdown below. |
//!
//! Things that *look* like forks but aren't, on the steady-state path:
@@ -125,79 +125,185 @@
//! The result is cached to `worktrunk.default-branch` so every subsequent
//! run is a memory hit served from #2.
//!
-//! ### Post-Skeleton Operations
+//! ## Pipeline
//!
-//! After the skeleton renders, remaining setup runs before spawning the worker thread.
-//! These operations are parallelized using `rayon::scope` with single-level parallelism:
+//! Every step from `collect()`'s entry to the first task result, with the
+//! thread each runs on and every point where one thread blocks on another.
+//! `wt list` runs `collect()` on the main thread; the picker runs it on its
+//! `picker-collect` thread after a prelude on the main thread (below). Both
+//! phase scopes are `rayon::scope` calls made from outside any pool, so they
+//! run on the **global** rayon pool — the pool skim's matcher uses — while
+//! the task pool and the picker's previews run on [`COLLECT_POOL`].
//!
//! ```text
-//! Skeleton render
-//! ├─ is_builtin_fsmonitor_enabled() (5ms, sequential - gate)
-//! ├─ rayon::scope(
-//! │ ├─ switch_previous() (5ms)
-//! │ ├─ capture_refs[_with_ahead_behind]() (ref snapshot)
-//! │ │ └─ prime_upstream_ahead_behind_cache() (nested; reads the snapshot)
-//! │ ├─ start_fsmonitor_daemon × N worktrees (socket probe; fork only when no daemon answers)
-//! │ ) // joins on the slowest spawn
-//! ├─ integration_targets(snapshot) (sequential; needs the snapshot)
-//! ├─ populate ListItem.commit from cache (cache-hit lookups, sub-ms)
-//! Worker thread spawns
-//! └─ paint Age/Message columns (workers already running)
+//! collect() thread rayon workers
+//! ──────────────── ─────────────
+//! redirect_objects_for_observation
+//! J1 rayon::scope ───────────────────────┬─ list_worktrees [cell] worktree list --porcelain
+//! (global pool; body = spawns only) │ └─ finalize_worktrees: rev-parse --git-dir × detached
+//! │ (run_concurrently: children only, no pool wait)
+//! │ + read rebase-{merge,apply}/head-name
+//! ├─ local_branches [cell] for-each-ref refs/heads/
+//! ├─ remote_branches [cell] for-each-ref refs/remotes/ (--remotes)
+//! ├─ default_branch, is_bare, url_template, config()
+//! │ [cells] config map / .config/wt.toml
+//! join ◄──────────────────────────────┘ (waits for the slowest: worktree list + backfill)
+//! stale-default check, branch-only rows CPU
+//! commit_details_many git log --no-walk (1 fork, serial)
+//! sort; build rows CPU + is_worktree_at_expected_path × worktrees
+//! custom columns, task plan CPU
+//! [picker] ci_status::populate_from_cache one ci-status/<branch>.json read per row (serial)
+//! layout; render skeleton ─────────────────── FIRST PAINT (wt list)
+//! [picker] on_skeleton: rows → skim ───────── FIRST PAINT (picker)
+//! then spawn preview precompute onto COLLECT_POOL (first row: every mode;
+//! branch-only rows: default mode), then maybe_spawn_comments, whose
+//! ci_platform() may fork `git remote get-url` (serial, before J2)
+//! prime_worktree_path_caches canonicalize + read .git × worktrees (serial fs)
+//! J2 rayon::scope ───────────────────────┬─ switch_previous
+//! (global pool) ├─ capture_refs[_with_ahead_behind]
+//! │ for-each-ref refs/heads/ (again), then refs/remotes/
+//! │ + ahead-behind/ cache reads; cold: %(ahead-behind) walk
+//! │ └─ spawn prime_upstream_ahead_behind_cache
+//! │ cold: one %(ahead-behind) walk per upstream
+//! └─ start_fsmonitor_daemon_at × worktrees
+//! socket connect; fork `start` on a miss
+//! join ◄──────────────────────────────┘ (waits for the slowest: usually the snapshot)
+//! integration_targets, prime_comparison_base, is_previous, commit fields CPU
+//! generate + sort work items CPU
+//! std::thread ─► J3 COLLECT_POOL.install(par_bridge) ── work items in queue order,
+//! each: git subprocess(es) → tx.send
+//! paint Age/Message
+//! W4 drain_results: rx.recv_timeout until every sender drops or the deadline passes
//! ```
//!
-//! **Why the Age/Message paint:** those two columns carry no task — their
-//! data is the `ListItem.commit` populated above — so without an explicit
-//! repaint they'd sit on the skeleton placeholder until the row's first *task*
-//! result happened to redraw it, lagging behind the slower task-driven columns
-//! (and a cache-warm Summary preview). The paint runs *after* the worker pool
-//! is spawned — so the slow git subprocesses (the long pole) aren't delayed by
-//! it — but *before* the drain renders any result, so Age/Message still reach
-//! the screen ahead of every task-driven column. Reading `all_items` here is
-//! race-free: the worker thread only sends results through the channel, and the
-//! drain (the sole `all_items` mutator) hasn't started. `render_skeleton_row`
-//! fills Age/Message from `item.commit` while every task column keeps its
-//! placeholder.
-//!
-//! **Why fsmonitor check is sequential:** It gates whether daemon starts are needed.
-//! The check is fast (~5ms) and must complete before we know which spawns to add.
-//!
-//! **Why fsmonitor starts are in the parallel scope:** The `git fsmonitor--daemon start`
-//! command returns quickly after signaling the daemon. By the time the worker thread
-//! starts executing `git status` commands, daemons have had time to initialize. In the
-//! steady state every daemon is already up, and the in-process socket probe in
-//! `start_fsmonitor_daemon_at` returns without forking.
+//! Nothing between J1's join and the skeleton overlaps with anything else:
+//! the commit-details fork, row building, and (picker) the CI-cache reads run
+//! one after another on the collect thread. Nothing between the skeleton and
+//! J3 overlaps with the task pool either: the pool opens only after J2 joins,
+//! and J2 joins on the ref snapshot, whose two `for-each-ref` scans run one
+//! after the other and repeat J1's `refs/heads/` scan.
+//!
+//! ### Picker prelude
+//!
+//! Before `collect()` starts, the picker's main thread runs, in order:
+//! `prewarm_info` (one `rev-parse`), `head_sha` (one `rev-parse HEAD`), a
+//! speculative `UnifiedDiff` preview for the current worktree on
+//! [`COLLECT_POOL`] (not joined), and the `num_items_estimate` — which calls
+//! `list_worktrees` (worktree list + rebase backfill) and, with branches or
+//! remotes shown, `local_branches` / `remote_branches`. The estimate only
+//! counts rows, so it does not need the backfill, but it pays for it on the
+//! main thread before skim starts. The speculative preview resolves its
+//! comparison base through `capture_refs`, which fills the same
+//! `local_branches` / `remote_branches` cells, so the estimate may block on a
+//! cell that preview job is initializing. `collect()`'s J1 then finds every
+//! inventory cached.
+//!
+//! ### Blocking waits
+//!
+//! | Wait | Site | Blocked thread | Released when |
+//! |------|------|----------------|---------------|
+//! | J1, J2 `rayon::scope` join | `collect()` | collect thread (outside the pool: parked, doesn't steal); scope bodies and spawns run on global-pool workers, which steal while waiting | every spawn returns |
+//! | J3 `install` + `par_bridge` | work-item thread | the spawned `std::thread`; pool workers steal | every item returns |
+//! | W4 channel receive | `drain_results` | collect thread | all senders drop, or the drain deadline |
+//! | `RepoCache` `OnceCell` (`worktrees`, `local_branches`, `remote_branches`, `default_branch`, `project_config`, `resolved_config`, `comparison_base`, `sparse_checkout_paths`, …) | any accessor | any thread that reads a cell while another thread is initializing it (a caller that finds it empty runs the initializer itself) | the running initializer returns |
+//! | `DashMap` entry (`GIT_DIRS`, `WORKTREE_ROOTS`, `commit_tree`, `effective_remote_urls`, …) | per-key accessors | a thread touching the same shard while `or_insert_with` forks under the shard lock | that fork returns |
+//! | `CMD_SEMAPHORE` (32) | `Cmd::run`, `run_concurrently`, `pipe_into` on background threads | the caller | a running command exits (a permit spans one child, or one `run_concurrently` batch) |
+//! | `HEAVY_OPS_SEMAPHORE` (4), `LLM_SEMAPHORE` (8) | heavy diff/rev-list tasks; summaries | the task | a heavy op / LLM call finishes; each is taken before `CMD_SEMAPHORE`, never after |
+//! | Child processes | every fork | the forking thread | the child exits |
+//!
+//! ### Deadlock freedom
+//!
+//! The waits above cannot form a cycle as long as one rule holds: **a cell
+//! initializer waits only on its own child processes, on semaphore permits,
+//! and on other cells — never on a rayon join, scope, or parallel iterator,
+//! and never on a channel.** Rayon joins happen only at the phase boundaries
+//! drawn above (J1–J3) and in command-level orchestration, never inside a
+//! repository accessor.
+//!
+//! Why the rule suffices: a thread that waits on a cell is waiting on a
+//! thread that is *running* that cell's initializer (a caller that finds the
+//! cell empty runs the initializer itself, so no one waits on an initializer
+//! that is merely queued). That initializer waits only on children, which
+//! wait on nothing in this process; on permits, which are held only across a
+//! child's lifetime (or taken in a fixed order: heavy/LLM before command);
+//! and on other cells, whose initializers obey the same rule. So every chain
+//! of waits ends at a child process. What the rule excludes is a rayon wait
+//! inside an initializer: a waiting rayon thread runs other queued jobs, and
+//! if one of those reads the cell being initialized, it waits on its own
+//! thread's stack frame and never returns. `wt step prune` reads
+//! `list_worktrees` from inside its own `par_iter`, which is how a
+//! `par_iter` inside the worktree-list initializer deadlocked. The rebase
+//! backfill therefore forks through `Cmd::run_concurrently`, which waits on
+//! children only.
+//!
+//! ### Direct reads of git's files
+//!
+//! Four steps read state from the git directory instead of asking git:
+//! the rebase backfill's `head-name` read (inside `list_worktrees`), the `.git` reads in
+//! `prime_worktree_path_caches` (before J2), the fsmonitor socket connect
+//! (in J2), and the `GitOperation` task's `operation_in_progress_at` (in J3).
+//!
+//! ### Why the Age/Message paint
+//!
+//! Those two columns carry no task — their data is the `ListItem.commit`
+//! populated after J2 — so without an explicit repaint they'd sit on the
+//! skeleton placeholder until the row's first *task* result happened to
+//! redraw it, lagging behind the slower task-driven columns (and a cache-warm
+//! Summary preview). The paint runs *after* the worker pool is spawned — so
+//! the slow git subprocesses (the long pole) aren't delayed by it — but
+//! *before* the drain renders any result, so Age/Message still reach the
+//! screen ahead of every task-driven column. Reading `all_items` here is
+//! race-free: the worker thread only sends results through the channel, and
+//! the drain (the sole `all_items` mutator) hasn't started.
+//! `render_skeleton_row` fills Age/Message from `item.commit` while every task
+//! column keeps its placeholder.
+//!
+//! ### Why fsmonitor daemons are started before the task pool
+//!
+//! When git's builtin fsmonitor is enabled and no daemon runs for a worktree,
+//! the first git command there spawns one itself, and a command that does so
+//! under parallel load can hang (the workaround scalar adopted in
+//! <https://gitlab.com/gitlab-org/git/-/merge_requests/148>; jj hit the same
+//! hang in <https://github.com/jj-vcs/jj/issues/6440>). The requirement is per
+//! worktree — a daemon started before that worktree's first command — but J2
+//! meets it with a barrier: every start returns before any task runs. In the
+//! steady state every daemon is already up, and `start_fsmonitor_daemon_at`
+//! detects that by connecting to `<git-dir>/fsmonitor--daemon.ipc` without
+//! forking. The connect fails when that path exceeds the platform's socket
+//! path limit (104 bytes on macOS, which git itself works around), so
+//! worktrees with long git-dir paths still fork `git fsmonitor--daemon start`
+//! on every run, which reports the daemon already running.
//!
//! When adding new features, ask: "Can this be computed after skeleton?" If yes, defer it.
//! The skeleton shows `·` placeholder for gutter symbols, filled in when data loads.
//!
-//! ### Measured Phase Timings
+//! ### Measured phase timings
//!
-//! Representative medians on the worktrunk dev repo (7 worktrees, 6
-//! branches, warm caches, release build, `--progressive` forced so the
-//! progressive-table path fires even with stdout piped).
+//! One traced run each on a 143-worktree repo (16 detached, ~90 local
+//! branches, 5 worktrees whose socket path exceeds the limit), warm caches,
+//! release build, `-vv` tracing. Milliseconds from process start.
//!
-//! | Phase | median | cmds |
-//! |-------|-------:|-----:|
-//! | `List collect started → Skeleton rendered` (pre-skeleton) | ~60ms | 23 |
-//! | `Skeleton rendered → Spawning worker thread` (rayon::scope + work-item setup) | ~41ms | 7 |
-//! | `Spawning worker thread → Parallel execution started` | <100µs | 0 |
-//! | `Parallel execution started → First result received` | <100µs | 0 |
-//! | `First result received → All results drained` (parallel work) | ~436ms | 154 |
-//! | `All results drained → List collect complete` (final render) | ~344µs | 0 |
-//! | Wall clock | ~549ms | — |
+//! | Milestone | `wt list --progressive` | picker (`WORKTRUNK_PICKER_DRY_RUN`) |
+//! |-----------|------:|------:|
+//! | `List collect started` | 41 | 161 (after an ~85ms prelude) |
+//! | worktree list returns (runs beside `refs/heads/` scan) | 82 | 127 (prelude) |
+//! | rebase backfill returns | 95 | 156 (prelude) |
+//! | commit-details `git log` returns | 123 | 222 |
+//! | skeleton / rows sent to skim | ~130 | ~294 |
+//! | `Spawning worker thread` (J2 joined) | 171 | 494 |
+//! | `All results drained` | 984 | — |
//!
-//! The 23-command pre-skeleton count is well above the five-to-six
-//! critical-path forks documented above — worth an audit. Most of the extras
-//! come from per-worktree probes that creep into the phase.
+//! In the picker, the preview precompute spawned at the skeleton (one
+//! `diff-tree` per branch-only row) competes with J2 and the work that
+//! precedes it for CPU, which is why its skeleton-to-pool gap is 200ms
+//! against `wt list`'s 46ms.
//!
//! Reproduce end-to-end via
//! `cargo bench --bench time_to_first_output -- list`; for a per-phase
-//! breakdown, capture a trace and inspect it as described in
-//! `benches/AGENTS.md` under "Analyzing a trace":
+//! breakdown, see `benches/AGENTS.md` under "Analyzing a trace":
//!
//! ```bash
-//! RUST_LOG=debug ./target/release/wt -C <repo> list --progressive \
-//! 2> >(cargo run -p wt-perf --release -q -- trace > trace.json)
+//! cargo run -p wt-perf --release -- timeline -- -C <repo> list --progressive
//! ```
//!
//! ## Unified Collection Architecture
@@ -216,8 +322,12 @@
//! cores unless `RAYON_NUM_THREADS` is set).
//!
//! **Task ordering**: Work items are sorted so local git operations run first, network tasks
-//! (CI status, URL health checks) run last. This ensures the table fills in quickly with local
-//! data while slower network requests complete in the background.
+//! (CI status, URL health checks) run last, and within each class rows run in display order.
+//! Workers pull items through `par_bridge`, which hands them out in that order. A splitting
+//! `into_par_iter` would not: it gives each worker a contiguous slice, so the slices holding
+//! the last rows and the network tasks start at once, and the top rows a user is looking at
+//! could settle last. This ensures the table fills in quickly with local data, from the top,
+//! while slower network requests complete in the background.
//!
//! ## Caching
//!
@@ -1775,7 +1885,8 @@ pub fn collect(
));
}
- // Sort work items: network tasks last to avoid blocking local operations
+ // Sort work items: network tasks last to avoid blocking local operations.
+ // Stable, so rows keep display order within each class.
all_work_items.sort_by_key(|item| item.kind.is_network());
// Phase 2: Execute all work items in a single Rayon pool on a worker
@@ -1788,9 +1899,10 @@ pub fn collect(
worktrunk::trace::instant("Parallel execution started");
// Run on the dedicated `COLLECT_POOL` so the blocking git subprocess
// tasks leave the global pool free for skim's per-keystroke matcher
- // when the picker is open. See `COLLECT_POOL`.
+ // when the picker is open. See `COLLECT_POOL`. `par_bridge` hands
+ // items out in queue order (see "Task ordering" above).
COLLECT_POOL.install(|| {
- all_work_items.into_par_iter().for_each(|item| {
+ all_work_items.into_iter().par_bridge().for_each(|item| {
let result = item.execute();
let _ = tx_worker.send(result);
});
diff --git a/src/git/mod.rs b/src/git/mod.rs
index 71d2e524b..36734b190 100644
--- a/src/git/mod.rs
+++ b/src/git/mod.rs
@@ -856,17 +856,13 @@ impl WorktreeInfo {
// Helper functions for worktree parsing
//
// These live in mod.rs rather than parse.rs because they bridge multiple concerns:
-// - read_rebase_branch() uses Repository (from repository.rs) to access git internals
-// - finalize_worktree() operates on WorktreeInfo (defined here in mod.rs)
-// - Both are tightly coupled to the WorktreeInfo type definition
+// - finalize_worktrees() uses Repository (from repository.rs) to access git internals
+// - it operates on WorktreeInfo (defined here in mod.rs)
//
// Placing them here avoids circular dependencies and keeps them close to WorktreeInfo.
-/// Helper function to read rebase branch information
-fn read_rebase_branch(worktree_path: &PathBuf) -> Option<String> {
- let repo = Repository::current().ok()?;
- let git_dir = repo.worktree_at(worktree_path).git_dir().ok()?;
-
+/// The branch a rebase in this git dir is rewriting, from `head-name`.
+fn rebase_branch(git_dir: &Path) -> Option<String> {
// Check both rebase-merge and rebase-apply
for rebase_dir in ["rebase-merge", "rebase-apply"] {
let head_name_path = git_dir.join(rebase_dir).join("head-name");
@@ -884,16 +880,32 @@ fn read_rebase_branch(worktree_path: &PathBuf) -> Option<String> {
None
}
-/// Finalize a worktree after parsing, filling in branch name from rebase state if needed.
-pub(crate) fn finalize_worktree(mut wt: WorktreeInfo) -> WorktreeInfo {
- // If detached but no branch, check if we're rebasing
- if wt.detached
- && wt.branch.is_none()
- && let Some(branch) = read_rebase_branch(&wt.path)
- {
- wt.branch = Some(branch);
+/// Finalize worktrees after parsing: a detached worktree mid-rebase takes the
+/// name of the branch it is rebasing.
+///
+/// `git worktree list` reports a rebasing worktree only as `detached`, so each
+/// detached worktree needs its own `git rev-parse --git-dir` to find its rebase
+/// state. [`WorkingTree::git_dirs`] runs those forks concurrently as child
+/// processes rather than on a thread pool: this runs inside the
+/// `list_worktrees` cache initializer, and a pool thread waiting there would
+/// run other pool jobs, one of which could wait on the same cache.
+pub(crate) fn finalize_worktrees(repo: &Repository, worktrees: &mut [WorktreeInfo]) {
+ let mut detached: Vec<&mut WorktreeInfo> = worktrees
+ .iter_mut()
+ .filter(|wt| wt.detached && wt.branch.is_none())
+ .collect();
+ if detached.is_empty() {
+ return;
+ }
+ let trees: Vec<WorkingTree<'_>> = detached
+ .iter()
+ .map(|wt| repo.worktree_at(&wt.path))
+ .collect();
+ for (wt, git_dir) in detached.iter_mut().zip(WorkingTree::git_dirs(&trees)) {
+ if let Ok(git_dir) = git_dir {
+ wt.branch = rebase_branch(&git_dir);
+ }
}
- wt
}
#[cfg(test)]
diff --git a/src/git/parse.rs b/src/git/parse.rs
index ccff00d83..5f0fa2d7e 100644
--- a/src/git/parse.rs
+++ b/src/git/parse.rs
@@ -2,7 +2,7 @@
use std::path::PathBuf;
-use super::{GitError, WorktreeInfo, finalize_worktree};
+use super::{GitError, WorktreeInfo};
impl WorktreeInfo {
pub(crate) fn parse_porcelain_list(output: &str) -> anyhow::Result<Vec<Self>> {
@@ -12,7 +12,7 @@ impl WorktreeInfo {
for line in output.lines() {
if line.is_empty() {
if let Some(wt) = current.take() {
- worktrees.push(finalize_worktree(wt));
+ worktrees.push(wt);
}
continue;
}
@@ -85,9 +85,8 @@ impl WorktreeInfo {
// Push the last worktree if the output doesn't end with a blank line
if let Some(wt) = current {
- worktrees.push(finalize_worktree(wt));
+ worktrees.push(wt);
}
-
Ok(worktrees)
}
}
diff --git a/src/git/repository/tests.rs b/src/git/repository/tests.rs
index 02888b7e5..55e5163d7 100644
--- a/src/git/repository/tests.rs
+++ b/src/git/repository/tests.rs
@@ -1,6 +1,8 @@
use std::path::PathBuf;
-use super::super::{DefaultBranchName, WorktreeInfo, finalize_worktree};
+use super::super::{DefaultBranchName, WorktreeInfo, finalize_worktrees};
+use crate::git::Repository;
+use crate::testing::TestRepo;
#[cfg(unix)]
#[test]
@@ -76,7 +78,11 @@ fn test_finalize_worktree_with_branch() {
prunable: None,
};
- let finalized = finalize_worktree(wt.clone());
+ let test = TestRepo::with_initial_commit();
+ let repo = Repository::at(test.root_path()).unwrap();
+ let mut worktrees = [wt];
+ finalize_worktrees(&repo, &mut worktrees);
+ let [finalized] = worktrees;
assert_eq!(finalized.branch, Some("feature".to_string()));
}
@@ -93,16 +99,18 @@ fn test_finalize_worktree_detached_with_branch() {
prunable: None,
};
- let finalized = finalize_worktree(wt.clone());
+ let test = TestRepo::with_initial_commit();
+ let repo = Repository::at(test.root_path()).unwrap();
+ let mut worktrees = [wt];
+ finalize_worktrees(&repo, &mut worktrees);
+ let [finalized] = worktrees;
assert_eq!(finalized.branch, Some("feature".to_string()));
}
#[test]
fn test_finalize_worktree_detached_no_branch() {
- // Detached worktree with no branch should attempt rebase detection
- // Note: This test validates the logic flow but doesn't test actual file reading
- // since that would require setting up git rebase state files.
- // Actual rebase detection has been manually verified.
+ // A detached worktree at a nonexistent path fails the git-dir lookup,
+ // so the branch stays empty.
let wt = WorktreeInfo {
path: PathBuf::from("/nonexistent/path"),
head: "abcd1234".to_string(),
@@ -113,12 +121,45 @@ fn test_finalize_worktree_detached_no_branch() {
prunable: None,
};
- let finalized = finalize_worktree(wt);
- // With a nonexistent path, rebase detection should fail gracefully
- // and branch should remain None
+ let test = TestRepo::with_initial_commit();
+ let repo = Repository::at(test.root_path()).unwrap();
+ let mut worktrees = [wt];
+ finalize_worktrees(&repo, &mut worktrees);
+ let [finalized] = worktrees;
assert_eq!(finalized.branch, None);
}
+#[test]
+fn test_finalize_worktree_linked_mid_rebase() {
+ // A linked worktree stopped mid-rebase is detached, so `git worktree list`
+ // reports no branch; the branch comes from `rebase-merge/head-name` under
+ // its git dir.
+ let test = TestRepo::with_initial_commit();
+ let linked = test.root_path().parent().unwrap().join("linked-rebase");
+ test.run_git(&["worktree", "add", "-b", "feature", linked.to_str().unwrap()]);
+ std::fs::write(linked.join("feature.txt"), "feature\n").unwrap();
+ test.run_git_in(&linked, &["add", "feature.txt"]);
+ test.run_git_in(&linked, &["commit", "-m", "Feature"]);
+ // `--exec` stops the rebase after replaying the commit, leaving it open.
+ let _ = test
+ .git_command()
+ .current_dir(&linked)
+ .args(["rebase", "--exec", "false", "HEAD~1"])
+ .run();
+
+ let repo = Repository::at(test.root_path()).unwrap();
+ let linked = dunce::canonicalize(&linked).unwrap();
+ let wt = repo
+ .list_worktrees()
+ .unwrap()
+ .iter()
+ .find(|wt| dunce::canonicalize(&wt.path).unwrap() == linked)
+ .unwrap()
+ .clone();
+ assert!(wt.detached, "precondition: the rebase detaches HEAD");
+ assert_eq!(wt.branch.as_deref(), Some("feature"));
+}
+
#[test]
fn test_parse_locked_worktree() {
let output = "worktree /path/to/locked
diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rs
index f6a7f0629..d8bbd3012 100644
--- a/src/git/repository/working_tree.rs
+++ b/src/git/repository/working_tree.rs
@@ -361,18 +361,22 @@ impl<'a> WorkingTree<'a> {
/// applies env mutations in call order. Repo-level
/// [`Repository::run_command`] keeps the inherited context on purpose.
pub fn run_command_output(&self, args: &[&str]) -> anyhow::Result<std::process::Output> {
- self.repo
- .with_object_store_env(
- Cmd::new("git")
- .args(args.iter().copied())
- .current_dir(&self.path)
- .context(path_to_logging_context(&self.path))
- .scrub_git_discovery_env(),
- )
+ self.git_command(args)
.run()
.with_context(|| format!("Failed to execute: git {}", args.join(" ")))
}
+ /// The `git` command [`Self::run_command_output`] runs, unexecuted.
+ fn git_command(&self, args: &[&str]) -> Cmd {
+ self.repo.with_object_store_env(
+ Cmd::new("git")
+ .args(args.iter().copied())
+ .current_dir(&self.path)
+ .context(path_to_logging_context(&self.path))
+ .scrub_git_discovery_env(),
+ )
+ }
+
// =========================================================================
// Worktree-specific methods
// =========================================================================
@@ -654,22 +658,62 @@ impl<'a> WorkingTree<'a> {
Entry::Occupied(e) => Ok(e.get().clone()),
Entry::Vacant(e) => {
let stdout = self.run_command(&["rev-parse", "--git-dir"])?;
- let path = PathBuf::from(stdout.trim());
-
- // Always canonicalize to resolve symlinks (e.g., /var -> /private/var on macOS)
- let absolute_path = if path.is_relative() {
- self.path.join(&path)
- } else {
- path
- };
- let resolved =
- canonicalize(&absolute_path).context("Failed to resolve git directory")?;
-
- Ok(e.insert(resolved).clone())
+ Ok(e.insert(self.resolve_git_dir(&stdout)?).clone())
}
}
}
+ /// [`Self::git_dir`] for several worktrees, forking the uncached
+ /// `git rev-parse --git-dir` lookups concurrently through
+ /// [`Cmd::run_concurrently`]. Results are in input order.
+ pub fn git_dirs(worktrees: &[WorkingTree<'_>]) -> Vec<anyhow::Result<PathBuf>> {
+ const ARGS: [&str; 2] = ["rev-parse", "--git-dir"];
+ let cached: Vec<Option<PathBuf>> = worktrees
+ .iter()
+ .map(|wt| super::GIT_DIRS.get(&wt.path).map(|e| e.value().clone()))
+ .collect();
+ let cmds: Vec<Cmd> = worktrees
+ .iter()
+ .zip(&cached)
+ .filter(|(_, cached)| cached.is_none())
+ .map(|(wt, _)| wt.git_command(&ARGS))
+ .collect();
+ let mut fetched = Cmd::run_concurrently(&cmds).into_iter();
+ worktrees
+ .iter()
+ .zip(cached)
+ .map(|(wt, cached)| {
+ if let Some(git_dir) = cached {
+ return Ok(git_dir);
+ }
+ let output = fetched
+ .next()
+ .context("run_concurrently returned fewer results than commands")??;
+ if !output.status.success() {
+ return Err(CommandError::from_failed_output("git", &ARGS, &output).into());
+ }
+ let git_dir = wt.resolve_git_dir(&String::from_utf8_lossy(&output.stdout))?;
+ Ok(super::GIT_DIRS
+ .entry(wt.path.clone())
+ .or_insert(git_dir)
+ .clone())
+ })
+ .collect()
+ }
+
+ /// Canonicalize `git rev-parse --git-dir` output run in this worktree.
+ fn resolve_git_dir(&self, stdout: &str) -> anyhow::Result<PathBuf> {
+ let path = PathBuf::from(stdout.trim());
+
+ // Always canonicalize to resolve symlinks (e.g., /var -> /private/var on macOS)
+ let absolute_path = if path.is_relative() {
+ self.path.join(&path)
+ } else {
+ path
+ };
+ canonicalize(&absolute_path).context("Failed to resolve git directory")
+ }
+
/// Reason recorded by `git worktree lock`, if this worktree is locked.
///
/// Reads the `locked` file in the worktree's git dir — the same file git
diff --git a/src/git/repository/worktrees.rs b/src/git/repository/worktrees.rs
index 3ccb02fbb..72fa5bf76 100644
--- a/src/git/repository/worktrees.rs
+++ b/src/git/repository/worktrees.rs
@@ -15,7 +15,7 @@ use super::{
GitError, InProgressOperation, Repository, ResolvedWorktree, Selector, WorktreeInfo,
is_valid_branch_name, normalize_selector, resolve_input_path,
};
-use crate::git::{CommandError, PlumbingDiff, WorktreeId, is_bare_repo_dir};
+use crate::git::{CommandError, PlumbingDiff, WorktreeId, finalize_worktrees, is_bare_repo_dir};
use crate::path::{format_path_for_display, paths_match};
use crate::shell_exec::Cmd;
use crate::styling::{
@@ -75,6 +75,7 @@ impl Repository {
first.path = self.repo_path()?.to_path_buf();
}
+ finalize_worktrees(self, &mut worktrees);
Ok(worktrees)
})
.map(Vec::as_slice)
diff --git a/src/shell_exec.rs b/src/shell_exec.rs
index 6999ed985..807b15ea9 100644
--- a/src/shell_exec.rs
+++ b/src/shell_exec.rs
@@ -1177,6 +1177,27 @@ fn record_captured(
log_output(trace, stdin, result.as_ref().ok());
}
+/// A child from [`Cmd::spawn_captured`], traced from spawn until
+/// [`Self::wait`] reaps it.
+struct CapturedChild {
+ child: std::process::Child,
+ trace: CommandTrace,
+ _tracked: Option<BackgroundPid>,
+}
+
+impl CapturedChild {
+ fn wait(self) -> std::io::Result<std::process::Output> {
+ let CapturedChild {
+ child,
+ mut trace,
+ _tracked,
+ } = self;
+ let result = child.wait_with_output();
+ record_captured(&mut trace, None, &result);
+ result
+ }
+}
+
/// Structured error from [`Cmd::delayed_stream`].
///
/// Separates command output from command identity so callers can format each
@@ -1330,6 +1351,17 @@ impl Cmd {
cmd
}
+ /// [`Self::check_spawn_preconditions`], after checking that this thread's
+ /// commands haven't been cancelled. Background callers check after taking
+ /// their semaphore permit: a command can be cancelled while parked on the
+ /// semaphore, and that is the common case in a large fan-out.
+ fn check_before_spawn(&self) -> std::io::Result<()> {
+ if background_cancelled() {
+ return Err(cancelled_error());
+ }
+ self.check_spawn_preconditions()
+ }
+
fn check_spawn_preconditions(&self) -> std::io::Result<()> {
if let Some(dir) = &self.current_dir {
let metadata = std::fs::metadata(dir)?;
@@ -1643,17 +1675,7 @@ impl Cmd {
let mut trace = CommandTrace::new(self.context.as_deref(), &cmd_str)
.reads_stdin(self.stdin_data.is_some());
- // Checked after the permit, not before: a command can be cancelled
- // while parked on the semaphore, and that is the common case in a
- // large fan-out.
- if background_cancelled() {
- let e = cancelled_error();
- trace.fail(&e);
- external_log.record(None);
- return Err(e);
- }
-
- if let Err(e) = self.check_spawn_preconditions() {
+ if let Err(e) = self.check_before_spawn() {
trace.fail(&e);
external_log.record(None);
return Err(e);
@@ -1720,6 +1742,81 @@ impl Cmd {
result
}
+ /// Run `cmds` as concurrent child processes and return each one's output,
+ /// in input order.
+ ///
+ /// Every child is spawned before any is waited on, so a batch of short
+ /// commands costs about one command's latency without a thread per
+ /// command. Outputs are read one child at a time, in order, so a child
+ /// that writes more than a pipe buffer blocks until its turn: use this
+ /// for commands with small output.
+ ///
+ /// The caller waits only on its own children, which makes this safe where
+ /// a thread pool is not: inside a cache initializer that pool jobs also
+ /// read. A rayon thread that waits runs other pool jobs, and a job that
+ /// reads the cache being initialized then deadlocks.
+ ///
+ /// On a background thread the batch takes one semaphore permit, as
+ /// [`Self::pipe_into`] does: one permit per child could deadlock two
+ /// batches that each hold part of the pool. At most
+ /// `max_concurrent_commands()` children run at once. Stdin, timeouts,
+ /// `external()` logging and shell commands are not supported.
+ pub fn run_concurrently(cmds: &[Cmd]) -> Vec<std::io::Result<std::process::Output>> {
+ assert!(
+ cmds.iter().all(|cmd| !cmd.shell_wrap
+ && cmd.stdin_data.is_none()
+ && cmd.timeout.is_none()
+ && cmd.external_label.is_none()),
+ "run_concurrently supports captured commands without stdin, timeout, external() or shell"
+ );
+
+ let _guard = (!is_foreground_thread()).then(|| semaphore().acquire());
+
+ let mut results = Vec::with_capacity(cmds.len());
+ for chunk in cmds.chunks(max_concurrent_commands()) {
+ let children: Vec<_> = chunk.iter().map(Cmd::spawn_captured).collect();
+ results.extend(
+ children
+ .into_iter()
+ .map(|child| child.and_then(CapturedChild::wait)),
+ );
+ }
+ results
+ }
+
+ /// Spawn `self` with piped stdout/stderr and null stdin, without waiting.
+ /// A failure to spawn resolves the trace before returning.
+ fn spawn_captured(&self) -> std::io::Result<CapturedChild> {
+ let cmd_str = self.command_string();
+ self.log_run_start(&cmd_str);
+ let mut trace = CommandTrace::new(self.context.as_deref(), &cmd_str);
+
+ if let Err(e) = self.check_before_spawn() {
+ trace.fail(&e);
+ return Err(e);
+ }
+
+ let mut cmd = self.direct_command();
+ self.apply_common_settings(&mut cmd);
+ cmd.stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+ match cmd.spawn() {
+ Ok(child) => {
+ let tracked = self.track_if_cancellable(child.id());
+ Ok(CapturedChild {
+ child,
+ trace,
+ _tracked: tracked,
+ })
+ }
+ Err(e) => {
+ trace.fail(&e);
+ Err(e)
+ }
+ }
+ }
+
/// Run `self` with its stdout piped directly into `next`'s stdin, and
/// return both children's captured output.
///
@@ -1777,23 +1874,11 @@ impl Cmd {
let _guard = (!is_foreground_thread()).then(|| semaphore().acquire());
- // Cancelled, possibly while parked on the semaphore above (see
- // `background_cancelled`). Nothing has spawned, so neither half runs.
- if background_cancelled() {
- let e = cancelled_error();
- CommandTrace::record_failed(
- self.context.as_deref(),
- &first_cmd_str,
- self.stdin_data.is_some(),
- &e,
- );
- return Err(e);
- }
-
// Validate both commands before spawning either. Nothing has spawned
- // yet, so a precondition failure emits a one-shot failed record rather
- // than holding a guard across an execution that never happens.
- if let Err(e) = self.check_spawn_preconditions() {
+ // yet, so a cancellation or precondition failure emits a one-shot
+ // failed record rather than holding a guard across an execution that
+ // never happens.
+ if let Err(e) = self.check_before_spawn() {
// stdin_data is still owned here (taken below), so it reflects
// whether the source would have read a buffer; the sink always reads
// the upstream pipe.
@@ -3117,6 +3202,34 @@ mod tests {
assert!(err.to_string().contains("Failed to spawn"), "{err}");
}
+ #[test]
+ #[cfg(unix)]
+ fn test_cmd_run_concurrently_overlaps_children_and_keeps_order() {
+ // The first child waits for a file that only the last child creates, so
+ // it succeeds only if both run at once. A spawn failure in between
+ // stays in its own slot rather than shifting the results after it.
+ let dir = tempfile::tempdir().unwrap();
+ let flag = dir.path().join("flag");
+ let wait_for_flag = format!(
+ "for _ in $(seq 500); do [ -e '{}' ] && exit 0; sleep 0.01; done; exit 1",
+ flag.display()
+ );
+ let results = Cmd::run_concurrently(&[
+ Cmd::new("sh").args(["-c", wait_for_flag.as_str()]),
+ Cmd::new(MISSING_CMD),
+ Cmd::new("sh").args(["-c", "exit 3"]),
+ Cmd::new("touch").arg(flag.to_str().unwrap()),
+ ]);
+ let [waiter, missing, exit3, toucher]: [_; 4] = results.try_into().unwrap();
+ assert!(
+ waiter.unwrap().status.success(),
+ "children ran one at a time"
+ );
+ assert_eq!(missing.unwrap_err().kind(), ErrorKind::NotFound);
+ assert_eq!(exit3.unwrap().status.code(), Some(3));
+ assert!(toucher.unwrap().status.success());
+ }
+
#[test]
#[cfg(unix)]
fn test_cmd_pipe_into_succeeds() {