Skip to content

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

perf(switch): reuse the list collector's ref scan for the preview comparison base (#3308)

Maximilian Roos73b2e1e4523a
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rs
index ae0e78fee..e38e37a4f 100644
--- a/src/commands/list/collect/mod.rs
+++ b/src/commands/list/collect/mod.rs
@@ -1546,6 +1546,14 @@ pub fn collect(
.as_deref()
.and_then(|s| repo.integration_targets(s));
+ // Seed the repo-wide comparison-base cache from this snapshot so the
+ // diff/summary preview panes reuse this ref scan instead of capturing their
+ // own. They run on `repo` (`wt list --full`'s summary column) or a clone of
+ // it (the picker's panes), both sharing this `Arc<RepoCache>`.
+ if let Some(s) = snapshot.as_deref() {
+ repo.prime_comparison_base(s);
+ }
+
// Patch integration_targets and snapshot into options. When
// default_branch is None (unset or stale), null integration_targets
// out — tasks otherwise see a target derived from the stale value
diff --git a/src/commands/picker/items.rs b/src/commands/picker/items.rs
index 7ba5f7560..07c381e8d 100644
--- a/src/commands/picker/items.rs
+++ b/src/commands/picker/items.rs
@@ -2672,6 +2672,44 @@ mod tests {
);
}
+ #[test]
+ fn prime_comparison_base_feeds_branch_diff_spec() {
+ // Priming the comparison base from the collector's already-captured
+ // snapshot must seed the same upstream-aware base the lazy path would
+ // resolve, so the preview pane reuses that scan instead of capturing
+ // its own. Same upstream-aware setup as the test above: local main
+ // lags origin-main, so the base is origin-main, not the stale local.
+ let (t, repo) = repo_with_main();
+ repo.run_command(&["branch", "origin-main"]).unwrap();
+ repo.run_command(&["checkout", "origin-main"]).unwrap();
+ std::fs::write(t.path().join("upstream.txt"), "from upstream\n").unwrap();
+ repo.run_command(&["add", "upstream.txt"]).unwrap();
+ repo.run_command(&["commit", "-m", "upstream commit"])
+ .unwrap();
+ repo.run_command(&["checkout", "-b", "feature"]).unwrap();
+ std::fs::write(t.path().join("feature.txt"), "feature work\n").unwrap();
+ repo.run_command(&["add", "feature.txt"]).unwrap();
+ repo.run_command(&["commit", "-m", "feature commit"])
+ .unwrap();
+ repo.run_command(&["branch", "--set-upstream-to=origin-main", "main"])
+ .unwrap();
+
+ let head = repo
+ .run_command(&["rev-parse", "refs/heads/feature"])
+ .unwrap();
+ let snapshot = repo.capture_refs().unwrap();
+ repo.prime_comparison_base(&snapshot);
+
+ let spec = repo
+ .branch_diff_spec(head.trim())
+ .expect("primed base resolves a spec");
+ assert_eq!(
+ spec.base_name, "origin-main",
+ "primed base must be the upstream-aware comparison base, got: {:?}",
+ spec.base_name
+ );
+ }
+
#[test]
fn branch_diff_cache_short_circuits_recompute() {
// Pre-populate the disk cache with a sentinel value, then call
diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rs
index 76ffc3437..7a886f7f6 100644
--- a/src/commands/picker/mod.rs
+++ b/src/commands/picker/mod.rs
@@ -1544,12 +1544,11 @@ pub fn handle_picker(
// Wrapped in `Arc` because the progressive handler (running on the
// collect background thread) also calls `spawn_preview`.
//
- // BranchDiff previews resolve the default branch's SHA via
- // `Repository::default_branch_sha`, which sources it from the
- // local-branch inventory cached on the shared `RepoCache`. N parallel
- // preview tasks share one inventory scan instead of each forking
- // `git rev-parse`. Read-only for the picker session — see the
- // accessor's docstring for the staleness contract.
+ // BranchDiff previews resolve the upstream-aware comparison base via
+ // `Repository::branch_diff_spec`, memoized on the shared `RepoCache` and
+ // primed by `collect::collect` from its ref scan, so N parallel preview
+ // tasks share one resolution instead of each capturing refs. Read-only for
+ // the picker session — see `branch_diff_spec` for the staleness contract.
//
// skim 4.x repaints on demand, so the orchestrator needs a handle to skim's
// event loop to surface a preview compute that lands after the keystroke that
diff --git a/src/git/repository/integration.rs b/src/git/repository/integration.rs
index dcea759f2..63efeffc4 100644
--- a/src/git/repository/integration.rs
+++ b/src/git/repository/integration.rs
@@ -586,9 +586,9 @@ impl Repository {
/// Selects the base via the shared [`select_comparison_base`] rule
/// ([`IntegrationTargets::primary`] else the raw local default), so the
/// preview panes and the `wt list` columns can't pick different bases.
- /// Captures a [`RefSnapshot`] on first call; safe in the read-only preview
- /// contexts that use it (wt doesn't move refs there). Fully local — no
- /// network.
+ /// Captures a [`RefSnapshot`] on first call unless already primed via
+ /// [`Self::prime_comparison_base`]; safe in the read-only preview contexts
+ /// that use it (wt doesn't move refs there). Fully local — no network.
fn comparison_base(&self) -> Option<&ComparisonBase> {
self.cache
.comparison_base
@@ -596,18 +596,38 @@ impl Repository {
.as_ref()
}
+ /// Seed the memoized comparison base from an already-captured snapshot, so
+ /// the diff/summary preview panes reuse the `wt list` collector's ref scan
+ /// instead of capturing their own (both share one `Arc<RepoCache>`).
+ /// Idempotent — a no-op once the base is resolved; the lazy
+ /// `comparison_base` path remains the fallback for callers that preview
+ /// without collecting first.
+ pub fn prime_comparison_base(&self, snapshot: &RefSnapshot) {
+ self.cache
+ .comparison_base
+ .get_or_init(|| self.comparison_base_from(snapshot));
+ }
+
fn resolve_comparison_base(&self) -> Option<ComparisonBase> {
// No default branch → no comparison base; skip the ref scan entirely.
self.default_branch()?;
let snapshot = self.capture_refs().ok()?;
- let targets = self.integration_targets(&snapshot);
+ self.comparison_base_from(&snapshot)
+ }
+
+ /// Resolve the comparison base from a captured snapshot. The shared core of
+ /// the lazy [`Self::resolve_comparison_base`] (captures its own snapshot)
+ /// and [`Self::prime_comparison_base`] (reuses the collector's), so both
+ /// select the base by the same [`select_comparison_base`] rule.
+ fn comparison_base_from(&self, snapshot: &RefSnapshot) -> Option<ComparisonBase> {
+ let targets = self.integration_targets(snapshot);
let default_branch = self.default_branch();
let name = select_comparison_base(targets.as_ref(), default_branch.as_deref())?.to_string();
// No usable base if the name won't resolve (a stale
// `worktrunk.default-branch` pointing at a deleted branch) — return
// `None` so the panes skip the branch diff rather than render a
// guaranteed-empty range against a missing ref.
- let sha = snapshot_resolve(self, &snapshot, &name).ok()?;
+ let sha = snapshot_resolve(self, snapshot, &name).ok()?;
Some(ComparisonBase { name, sha })
}