Skip to content

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

test(bench): detach six worktrees in the `full` list fixture (#4285)

Maximilian Roos6f5a38184ad7
diff --git a/benches/AGENTS.md b/benches/AGENTS.md
index 0d8dba569..960093c2e 100644
--- a/benches/AGENTS.md
+++ b/benches/AGENTS.md
@@ -4,7 +4,7 @@ Bench groups and examples are documented at the top of their Rust files. Criteri
## Fixtures and benches
-Generated fixtures vary linked worktrees, branchless branches, and remote-tracking refs. Imported fixtures copy the pinned corpus in benches/imported-fixture. Prune candidates and backdrop are overlays on either base, not new fixture identities. wt-perf setup --help lists recipes. Each benchmark uses FixtureRepo for lifecycle and wt_command for subprocess isolation.
+Generated fixtures vary linked worktrees, branchless branches, remote-tracking refs, and how many linked worktrees are on a detached HEAD. Imported fixtures copy the pinned corpus in benches/imported-fixture. Prune candidates and backdrop are overlays on either base, not new fixture identities. wt-perf setup --help lists recipes. Each benchmark uses FixtureRepo for lifecycle and wt_command for subprocess isolation.
## Cache handling
diff --git a/benches/alias.rs b/benches/alias.rs
index ae1148951..e85f5d878 100644
--- a/benches/alias.rs
+++ b/benches/alias.rs
@@ -67,6 +67,7 @@ fn bench_dispatch(c: &mut Criterion) {
linked_worktrees: total_worktrees - 1,
branchless_branches: 0,
remote_tracking_refs: 0,
+ detached_worktrees: 0,
}
.create();
let user_config = fixture.root().join("config.toml");
diff --git a/benches/completion.rs b/benches/completion.rs
index a66004014..7567d9a67 100644
--- a/benches/completion.rs
+++ b/benches/completion.rs
@@ -17,12 +17,12 @@
//! batch and so is not quite the run users get.
//!
//! The benchmark uses the same 24 linked worktrees and 120 branchless branches
-//! as `full`, plus 1400 remote-tracking refs. It covers all three categories
-//! `BranchCompleter` distinguishes and runs both candidate slow calls
-//! (`for-each-ref refs/remotes/` and `worktree list --porcelain`) in one
-//! process at a size where each is expensive. Its 145 local candidates also
-//! exceed the completer's 100-entry threshold, where remote refs are scanned
-//! and then discarded as they are on a long-lived clone.
+//! as `full`, all on their branches, plus 1400 remote-tracking refs. It covers
+//! all three categories `BranchCompleter` distinguishes and runs both candidate
+//! slow calls (`for-each-ref refs/remotes/` and `worktree list --porcelain`)
+//! in one process at a size where each is expensive. Its 145 local candidates
+//! also exceed the completer's 100-entry threshold, where remote refs are
+//! scanned and then discarded as they are on a long-lived clone.
//!
//! The remote-ref count is what this bench adds to the shared fixture; `full`
//! passes 0 and is unaffected.
@@ -81,6 +81,7 @@ fn bench_completion_switch(c: &mut Criterion) {
linked_worktrees,
branchless_branches,
remote_tracking_refs: 1400,
+ detached_worktrees: 0,
}
.create();
let expected = expected_branches(branchless_branches, linked_worktrees);
diff --git a/benches/list.rs b/benches/list.rs
index a8ac0c5b4..39f5cfefa 100644
--- a/benches/list.rs
+++ b/benches/list.rs
@@ -4,8 +4,9 @@
// - skeleton: Time until the skeleton paints (1 and 8 worktrees; warm + cold)
// - worktree_scaling: Full execution at 1 and 8 worktrees (warm + cold)
// - full: One combined full-surface fixture — many worktrees AND many branches
-// in varied states, with branch divergence spread across history depth.
-// The realistic "everything at once" workload (warm + cold).
+// in varied states, some worktrees on a detached HEAD, with branch
+// divergence spread across history depth. The realistic "everything at
+// once" workload (warm + cold).
// - divergent_branches: 200 branches spread across history depth / GH #461 stress (warm + cold)
// - large_repository: one pinned upstream corpus with 8 worktrees and 50 branches
// spread through history; default list (warm + cold) and --branches (warm)
@@ -161,6 +162,7 @@ fn bench_divergent_branches(c: &mut Criterion) {
linked_worktrees: 0,
branchless_branches: 200,
remote_tracking_refs: 0,
+ detached_worktrees: 0,
}
.create();
run_git(fixture.path(), &["status"]);
@@ -187,7 +189,9 @@ fn bench_divergent_branches(c: &mut Criterion) {
/// clean/dirty/staged working trees, merged/ahead/diverged branches, and the
/// GH #461 deep-divergence shape (branches forking at points spread across
/// history depth, so the `git for-each-ref %(ahead-behind)` walk has real
-/// history to traverse).
+/// history to traverse). Six of the worktrees are on a detached HEAD: each is
+/// a row with no branch, its branch becomes a branch-only row, and resolving
+/// its branch from rebase state costs a `git rev-parse --git-dir` in it.
///
/// To see *where* a regression lands, trace one invocation and read the
/// profile's BY CONTEXT / BY COMMAND TYPE tables — see `benches/AGENTS.md`
@@ -211,22 +215,21 @@ fn bench_divergent_branches(c: &mut Criterion) {
/// network-touching `ci` column that `--full` would add.
fn bench_full(c: &mut Criterion) {
let mut group = c.benchmark_group("full");
- // Heavy fixture (24 linked worktrees + 120 branchless branches, deep
- // history): the cold variant runs well over the inherited 30-sample / 15s
- // budget. Cap samples at criterion's minimum and give a 20s window (≈ a
- // few iters per sample), matching the other heavy groups.
+ // Heavy fixture (24 linked worktrees, 6 of them detached, + 120 branchless
+ // branches, deep history): the cold variant runs well over the inherited
+ // 30-sample / 15s budget. Cap samples at criterion's minimum and give a 20s
+ // window (≈ a few iters per sample), matching the other heavy groups.
group.measurement_time(std::time::Duration::from_secs(20));
group.sample_size(10);
let binary = &worktrunk::testing::wt_bin();
- let (linked_worktrees, branchless_branches) = (24usize, 120usize);
-
for cache in CacheState::WARM_AND_COLD {
group.bench_function(cache.label(), |b| {
let fixture = FixtureRecipe::Generated {
- linked_worktrees,
- branchless_branches,
+ linked_worktrees: 24,
+ branchless_branches: 120,
remote_tracking_refs: 0,
+ detached_worktrees: 6,
}
.create();
run_benchmark(
diff --git a/benches/prune.rs b/benches/prune.rs
index cadfed0b8..a4f57afb7 100644
--- a/benches/prune.rs
+++ b/benches/prune.rs
@@ -85,6 +85,7 @@ fn generated_prune_fixture() -> FixtureRepo {
linked_worktrees: 0,
branchless_branches: 0,
remote_tracking_refs: 0,
+ detached_worktrees: 0,
}
.create();
add_prune_populations(fixture.path(), MERGED, UNMERGED);
diff --git a/tests/helpers/wt-perf/src/lib.rs b/tests/helpers/wt-perf/src/lib.rs
index 086b0561d..62cf22fc6 100644
--- a/tests/helpers/wt-perf/src/lib.rs
+++ b/tests/helpers/wt-perf/src/lib.rs
@@ -148,6 +148,10 @@ pub enum FixtureRecipe {
/// Additional remote-tracking refs.
#[arg(default_value_t = 0)]
remote_tracking_refs: usize,
+ /// Linked worktrees on a detached HEAD, counted within the linked
+ /// worktrees.
+ #[arg(default_value_t = 0)]
+ detached_worktrees: usize,
},
/// Pinned rust-lang/rust corpus with heterogeneous history-spread state.
Imported,
@@ -161,6 +165,7 @@ impl FixtureRecipe {
linked_worktrees,
branchless_branches: GENERATED_DEFAULT_BRANCHES,
remote_tracking_refs: 0,
+ detached_worktrees: 0,
}
}
@@ -181,11 +186,13 @@ impl FixtureRecipe {
linked_worktrees,
branchless_branches,
remote_tracking_refs,
+ detached_worktrees,
} => {
build_generated_repo_at(
linked_worktrees,
branchless_branches,
remote_tracking_refs,
+ detached_worktrees,
base_path,
);
}
@@ -1167,6 +1174,8 @@ fn append_line(path: &Path, rel: &str, line: &str) {
/// 2. staged + unstaged + untracked (full dirty mix)
/// 3. clean, sitting exactly at base
///
+/// The last `detached_worktrees` of them then detach HEAD in place.
+///
/// Branch states cycle by index % 4 (states 0 and 2 fork at a checkpoint that
/// slides from the oldest base commit toward the tip as the index grows, so
/// fork depth fans out across the whole history — the GH #461 deep-divergence
@@ -1181,8 +1190,13 @@ fn build_generated_repo_at(
linked_worktrees: usize,
branchless_branches: usize,
remote_tracking_refs: usize,
+ detached_worktrees: usize,
repo: &Path,
) {
+ assert!(
+ detached_worktrees <= linked_worktrees,
+ "{detached_worktrees} detached worktrees exceed {linked_worktrees} linked worktrees"
+ );
const FILES: usize = 50;
// Deep enough that fork points spread across history give the
// `%(ahead-behind)` walk real commits to traverse (GH #461 shape).
@@ -1253,6 +1267,15 @@ fn build_generated_repo_at(
run_git(&repo, &["gc", "-q"]);
add_heterogeneous_worktrees(&repo, linked_worktrees, &base_tip);
+
+ // Detach the last worktrees at their own commit, the shape a mid-rebase or
+ // scratch checkout leaves. Detaching after creation keeps each worktree's
+ // rotation state, consecutive indices spread the detached ones across that
+ // rotation, and each `wt-*` branch stays behind as a branch-only row.
+ for j in linked_worktrees - detached_worktrees..linked_worktrees {
+ let wt = linked_worktree_path(&repo, &format!("wt-{j:04}"));
+ run_git(&wt, &["checkout", "-q", "--detach"]);
+ }
}
/// Add linked worktrees in the canonical four-state rotation.
@@ -1631,6 +1654,7 @@ mod tests {
linked_worktrees: N,
branchless_branches: N,
remote_tracking_refs: 0,
+ detached_worktrees: 0,
}
.create();
let repo = fixture.path().to_path_buf();
@@ -1839,8 +1863,10 @@ mod tests {
}
/// The generated fixture's population contract. Either local dimension may
- /// be zero, and the requested remote-tracking refs are additive to the
- /// `origin/main` and `origin/HEAD` pair every generated fixture carries.
+ /// be zero, the requested remote-tracking refs are additive to the
+ /// `origin/main` and `origin/HEAD` pair every generated fixture carries,
+ /// and detached worktrees are a subset of the linked worktrees whose
+ /// branches remain.
#[test]
fn generated_fixture_preserves_each_population() {
let refs = |repo: &Path, glob: &str| {
@@ -1848,20 +1874,23 @@ mod tests {
.lines()
.count()
};
- // `git worktree list` always includes the main worktree itself.
- let linked = |repo: &Path| {
+ let porcelain_lines = |repo: &Path, prefix: &str| {
capture_git(repo, &["worktree", "list", "--porcelain"])
.lines()
- .filter(|l| l.starts_with("worktree "))
+ .filter(|l| l.starts_with(prefix))
.count()
- - 1
};
+ // `git worktree list` always includes the main worktree itself.
+ let linked = |repo: &Path| porcelain_lines(repo, "worktree ") - 1;
+ let detached = |repo: &Path| porcelain_lines(repo, "detached");
- // Cover each local zero once and add remote refs to one fixture.
+ // Cover each local zero once, and add remote refs and a detached
+ // worktree to one fixture.
let fixture = FixtureRecipe::Generated {
linked_worktrees: 3,
branchless_branches: 0,
remote_tracking_refs: 5,
+ detached_worktrees: 1,
}
.create();
let repo = fixture.path().to_path_buf();
@@ -1869,11 +1898,24 @@ mod tests {
assert_eq!(refs(&repo, "refs/heads/wt-*"), 3);
assert_eq!(refs(&repo, "refs/remotes/origin/remote-only-*"), 5);
assert_eq!(linked(&repo), 3);
+ assert_eq!(detached(&repo), 1);
+ let detached_wt = fixture.worktree_path("wt-0002");
+ assert_eq!(
+ capture_git(&detached_wt, &["rev-parse", "--abbrev-ref", "HEAD"]),
+ "HEAD",
+ "the last linked worktree is the detached one"
+ );
+ assert_eq!(
+ status_lines(&detached_wt),
+ [" M src/file_2.rs", "?? untracked_2.txt", "M src/file_1.rs"],
+ "a detached worktree keeps its rotation state"
+ );
let fixture = FixtureRecipe::Generated {
linked_worktrees: 0,
branchless_branches: 3,
remote_tracking_refs: 0,
+ detached_worktrees: 0,
}
.create();
let repo = fixture.path().to_path_buf();
@@ -1881,6 +1923,7 @@ mod tests {
assert_eq!(refs(&repo, "refs/heads/wt-*"), 0, "no worktree branches");
assert_eq!(refs(&repo, "refs/remotes/origin/remote-only-*"), 0);
assert_eq!(linked(&repo), 0);
+ assert_eq!(detached(&repo), 0);
}
/// [`add_diverged_backdrop`]'s own wiring — the half of the prune fixture