Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(completion): surface unborn worktree branches (#3097)
Worktrunk Botd824d30f3dd3
diff --git a/src/git/repository/branches.rs b/src/git/repository/branches.rsindex bd1d794e2..9efa6b004 100644--- a/src/git/repository/branches.rs+++ b/src/git/repository/branches.rs@@ -285,7 +285,25 @@ impl Repository {.collect();remote_only.sort_by_key(|b| std::cmp::Reverse(b.2));- let mut result = Vec::with_capacity(locals.len() + remote_only.len());+ let mut result = Vec::with_capacity(locals.len() + remote_only.len() + worktrees.len());++ // Unborn worktree branches first: a worktree whose branch isn't in+ // `refs/heads/` yet (a fresh `git init`'d repo whose default branch+ // exists only as a `symbolic-ref` target, or a `wt switch --create`+ // off such a repo). Pinned to `i64::MAX` so they sort to the top of+ // the Worktree category — there's no commit to pull a real timestamp+ // from, and the user typically just created them.+ for wt in worktrees {+ if let Some(branch) = &wt.branch+ && !local_names.contains(branch.as_str())+ {+ result.push(CompletionBranch {+ name: branch.clone(),+ timestamp: i64::MAX,+ category: BranchCategory::Worktree,+ });+ }+ }// Worktree branches (already sorted by recency via locals order).for branch in locals {@@ -404,6 +422,60 @@ mod tests {assert_eq!(repo.default_branch_sha(), None);}+ #[test]+ fn branches_for_completion_includes_unborn_default_branch() {+ // Regression for #3094: on a fresh `git init -b main` with no+ // commits, `refs/heads/` is empty (main exists only as a+ // `symbolic-ref` target), so the local-branch scan returns nothing.+ // The worktree fallback keeps tab-completion responsive by emitting+ // a Worktree-category candidate for each worktree whose branch+ // isn't yet a ref.+ let test = TestRepo::new();+ let repo = Repository::at(test.root_path()).unwrap();++ let branches = repo.branches_for_completion().unwrap();+ let names: Vec<&str> = branches.iter().map(|b| b.name.as_str()).collect();+ assert_eq!(names, vec!["main"], "completion candidates: {:?}", branches);+ assert!(+ matches!(branches[0].category, BranchCategory::Worktree),+ "category: {:?}",+ branches[0].category,+ );+ }++ #[test]+ fn branches_for_completion_includes_unborn_linked_worktree() {+ // After `git worktree add -b feature ../path` on a still-unborn+ // repo, both `main` and `feature` exist only in `worktree list+ // --porcelain`, not under `refs/heads/`. Both must surface in+ // completion.+ let test = TestRepo::new();+ let feature_path = test.root_path().parent().unwrap().join("feature");+ test.run_git(&[+ "worktree",+ "add",+ "-b",+ "feature",+ feature_path.to_str().unwrap(),+ ]);++ let repo = Repository::at(test.root_path()).unwrap();+ let branches = repo.branches_for_completion().unwrap();+ let names: Vec<&str> = branches.iter().map(|b| b.name.as_str()).collect();+ assert!(+ names.contains(&"main") && names.contains(&"feature"),+ "completion candidates: {:?}",+ branches,+ );+ for b in &branches {+ assert!(+ matches!(b.category, BranchCategory::Worktree),+ "non-worktree category for {:?}",+ b,+ );+ }+ }+#[test]fn default_branch_sha_is_snapshot_at_first_scan() {// Documenting the staleness contract: the inventory is scanned oncediff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex d30f491a6..9b123a986 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -182,6 +182,11 @@ fn test_complete_outside_git_repo() {#[rstest]fn test_complete_empty_repo() {+ // Regression for #3094: in a fresh `git init`'d repo, the unborn+ // default branch exists only as a `symbolic-ref` target, not under+ // `refs/heads/`. `branches_for_completion()` falls back to+ // `git worktree list --porcelain` so the primary worktree's branch+ // still surfaces.let repo = TestRepo::empty();let mut settings = Settings::clone_current();settings.set_snapshot_path("../snapshots");@@ -190,12 +195,10 @@ fn test_complete_empty_repo() {let output = repo.completion_cmd(&["wt", "switch", ""]).output().unwrap();assert!(output.status.success());let stdout = String::from_utf8_lossy(&output.stdout);+ let values = value_suggestions(&stdout);assert!(- stdout- .lines()- .filter(|line| !line.trim().is_empty())- .all(|line| line.starts_with('-')),- "expected only option suggestions in empty repo, got:\n{stdout}"+ values.contains(&"main"),+ "expected `main` to appear as a completion candidate in an empty repo, got:\n{stdout}");});}