Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
perf(list): share git status output between working-tree tasks (#2259)
Maximilian Roos846a0d7ade88
diff --git a/src/commands/list/collect/tasks.rs b/src/commands/list/collect/tasks.rsindex 90c8dcae5..9615c2254 100644--- a/src/commands/list/collect/tasks.rs+++ b/src/commands/list/collect/tasks.rs@@ -406,10 +406,11 @@ impl Task for WorkingTreeDiffTask {.working_tree(&ctx.repo).ok_or_else(|| ctx.error(Self::KIND, &anyhow::anyhow!("requires a worktree")))?;- // Use --no-optional-locks to avoid index lock contention with- // WorkingTreeConflictsTask's `git write-tree`.+ // Shared cache: WorkingTreeConflictsTask also needs porcelain. First+ // accessor spawns the subprocess; second hits the cache. Uses+ // --no-optional-locks to avoid index lock contention with `git write-tree`.let status_output = wt- .run_command(&["--no-optional-locks", "status", "--porcelain"])+ .status_porcelain_cached().map_err(|e| ctx.error(Self::KIND, &e))?;let (working_tree_status, is_dirty, has_conflicts) =@@ -495,9 +496,9 @@ impl Task for WorkingTreeConflictsTask {.working_tree(&ctx.repo).ok_or_else(|| ctx.error(Self::KIND, &anyhow::anyhow!("requires a worktree")))?;- // Use --no-optional-locks to avoid index lock contention with WorkingTreeDiffTask.+ // Shared cache with WorkingTreeDiffTask — single subprocess per worktree.let status_output = wt- .run_command(&["--no-optional-locks", "status", "--porcelain"])+ .status_porcelain_cached().map_err(|e| ctx.error(Self::KIND, &e))?;let is_dirty = !status_output.trim().is_empty();diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex c10865594..1fc05f7c4 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -279,6 +279,11 @@ pub(super) struct RepoCache {pub(super) worktree_roots: DashMap<PathBuf, PathBuf>,/// Current branch per worktree: worktree_path -> branch name (None = detached HEAD)pub(super) current_branches: DashMap<PathBuf, Option<String>>,+ /// Cached `git status --porcelain` output per worktree: worktree_path -> raw porcelain.+ /// Populated by `WorkingTree::status_porcelain_cached()` so parallel tasks+ /// (working-tree diff + conflict detection) share one subprocess per worktree+ /// instead of spawning `git status` twice.+ pub(super) status_porcelain: DashMap<PathBuf, String>,}/// Result of resolving a worktree name.diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rsindex 4e5ab5e98..f86477de2 100644--- a/src/git/repository/working_tree.rs+++ b/src/git/repository/working_tree.rs@@ -144,6 +144,23 @@ impl<'a> WorkingTree<'a> {}}+ /// Return cached `git status --porcelain` output for this worktree.+ ///+ /// Keyed by worktree path in the shared `RepoCache`, so parallel tasks that+ /// each want porcelain (e.g., working-tree diff + conflict detection during+ /// `wt list`) share a single subprocess. Uses `--no-optional-locks` to avoid+ /// index-lock contention with the `git write-tree` run by+ /// `WorkingTreeConflictsTask` in parallel.+ pub fn status_porcelain_cached(&self) -> anyhow::Result<String> {+ match self.repo.cache.status_porcelain.entry(self.path.clone()) {+ Entry::Occupied(e) => Ok(e.get().clone()),+ Entry::Vacant(e) => {+ let stdout = self.run_command(&["--no-optional-locks", "status", "--porcelain"])?;+ Ok(e.insert(stdout).clone())+ }+ }+ }+/// Check if the working tree has uncommitted changes.////// Note: This does NOT detect files hidden via `git update-index --assume-unchanged`