Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(switch): rank picker matches by the distinct worktree path (#3208)
Maximilian Roos0d0fcacd40b1
diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex 3f16a1362..8bd17906e 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -822,6 +822,34 @@ pub fn handle_picker(let mut options = SkimOptionsBuilder::default().height("90%".to_string()).reverse(true)+ // Rank matches by a row's *distinguishing* tail, not the shared+ // `~/workspace/` prefix every worktree path carries. `last_match` makes+ // the matcher prefer the query's rightmost occurrence, and front-loading+ // `PathName` in the tiebreak ranks leaf-segment matches (at/after the+ // last `/`) above parent-directory ones — so `feature/auth` ranks on+ // `auth`, and the worktree folder name ranks on its tail. This is skim's+ // `Path` scheme spelled out as its two underlying knobs: a+ // `.scheme(MatchScheme::Path)` call would also expand here (the builder's+ // `build()` runs `SkimOptions::build`, which expands the scheme — unlike+ // the clap-only `scrollbar` default), but it injects a duplicate `Score`+ // criterion, so setting the knobs directly is the same effect without the+ // artifact. (Default tiebreak is `[Score, Begin, End]`.) Paired with the+ // distinct-path `search_text` built in `progressive_handler::on_skeleton`.+ //+ // `PathName` reads the whole `search_text`, including the trailing gutter+ // glyph. Local-branch rows fold in `/` as that glyph (the gutter sigil),+ // which `PathName` then reads as a path separator, so on a *score tie* a+ // local-branch row sorts just under a worktree/remote row whose glyph+ // (`+`/`@`/`^`/`|`) isn't a separator. The effect is confined to exact+ // ties (`PathName` is the 2nd criterion) and only reorders rows, so it+ // rides along rather than warranting a change to the gutter sigils.+ .last_match(true)+ .tiebreak(vec![+ RankCriteria::Score,+ RankCriteria::PathName,+ RankCriteria::Begin,+ RankCriteria::End,+ ])// Fill the whole selected row with the `current` background (set via// `current_bg` in `.color(...)` below). skim 4.x applies the current-row// style at the line level only when this is on; without it the selectiondiff --git a/src/commands/picker/progressive_handler.rs b/src/commands/picker/progressive_handler.rsindex ff44b69cb..ca04233f2 100644--- a/src/commands/picker/progressive_handler.rs+++ b/src/commands/picker/progressive_handler.rs@@ -33,6 +33,7 @@//! workers' local deques during drain.use std::collections::HashSet;+use std::path::Path;use std::sync::atomic::AtomicBool;use std::sync::{Arc, Mutex, OnceLock};use std::time::{Duration, Instant};@@ -164,6 +165,25 @@ impl PickerProgressHandler for PickerHandler {.unwrap_or_default();let summaries_enabled = self.llm_command.is_some();+ // Parent of the main worktree — stripped from each row's matcher path+ // (below) so the fuzzy matcher indexes only the distinguishing tail+ // (`worktrunk.skim-features`), not the `~/workspace/` prefix every sibling+ // worktree shares. The base comes from `list_worktrees()` — the same+ // source the row paths come from — so it shares their exact+ // canonicalization (the strip can't miss on a `/private/var`-vs-`/var`+ // mismatch) and adds no network or uncached work (the skeleton was just+ // built from this list). `[0]` is the main worktree for normal repos and+ // the first linked worktree for bare ones; either way its parent is the+ // shared sibling parent. Computed once; a worktree outside that parent+ // keeps its full path via the `strip_prefix` fallback.+ let path_base = self+ .orchestrator+ .repo()+ .list_worktrees()+ .ok()+ .and_then(|worktrees| worktrees.first())+ .and_then(|wt| wt.path.parent().map(Path::to_path_buf));+// Header row — non-selectable via `header_lines(1)` on the options.// In `--prs` mode it shows a dim "loading open PRs…" line (in place of// the column labels) until the forge call's rows land — wording mirrors@@ -184,9 +204,21 @@ impl PickerProgressHandler for PickerHandler {for (item, rendered_line) in items.into_iter().zip(rendered) {let branch_name = item.branch_name().to_string();let has_upstream = upstream_branches.contains(&branch_name);+ // The *distinct* path (leaf relative to the shared worktree parent),+ // not the absolute path — see `path_base`. This feeds only the+ // matcher's `search_text`; the rendered Path column is a separate+ // field and is untouched.let path_str = item.worktree_path()- .map(|p| p.to_string_lossy().into_owned())+ .map(|p| {+ let p = p.as_path();+ path_base+ .as_deref()+ .and_then(|base| p.strip_prefix(base).ok())+ .unwrap_or(p)+ .to_string_lossy()+ .into_owned()+ }).unwrap_or_default();// `search_text` is what the matcher sees — fuzzy ranks stay// stable across progressive updates because this field only@@ -475,6 +507,64 @@ mod tests {assert!(text(2).contains("origin/remotebr"));}+ /// `on_skeleton` strips the shared main-worktree parent from each row's+ /// matcher `search_text`, so the fuzzy matcher indexes the distinguishing+ /// leaf (`repo.feat`) rather than the absolute prefix every sibling worktree+ /// carries. The inside row's path is read back from `worktree_for_branch`, so+ /// the strip runs against the real `list_worktrees()` path the rows carry, not+ /// `add_worktree`'s separately canonicalized return. A worktree outside the+ /// shared parent keeps its full path via the `strip_prefix` fallback.+ #[test]+ fn search_text_strips_shared_worktree_parent() {+ use crate::commands::list::model::{ItemKind, WorktreeData};++ let (handler, mut test, rx) = make_handler();+ // A genuine sibling worktree at `{temp}/repo.feat`; read its path back from+ // the worktree list (the production path source — both the row path and+ // the stripped base come from `list_worktrees()`, so they share one+ // canonicalization).+ test.add_worktree("feat");+ let inside_path = test+ .repo+ .worktree_for_branch("feat")+ .unwrap()+ .expect("feat worktree is registered");+ let outside_path = std::path::PathBuf::from("/nonexistent-root/external-wt");++ let worktree_item = |branch: &str, path: &Path| {+ let mut item = ListItem::new_branch("abc".into(), branch.into());+ item.kind = ItemKind::Worktree(Box::new(WorktreeData {+ path: path.to_path_buf(),+ ..Default::default()+ }));+ item+ };++ handler.on_skeleton(+ vec![+ worktree_item("inside", &inside_path),+ worktree_item("outside", &outside_path),+ ],+ vec!["skel-inside".into(), "skel-outside".into()],+ header("hdr"),+ grid(),+ );++ let received = rx.recv().expect("skeleton batch");+ let text = |i: usize| received[i].text().into_owned();+ let (inside, outside) = (text(1), text(2));++ // Inside the shared parent: only the leaf is indexed (gutter `+` for a+ // non-current linked worktree). The absolute prefix is gone.+ assert_eq!(inside, "inside repo.feat +", "leaf only: {inside:?}");++ // Outside the shared parent: the full path is retained (fallback).+ assert_eq!(+ outside, "outside /nonexistent-root/external-wt +",+ "out-of-tree path kept whole: {outside:?}"+ );+ }+/// Locks the gutter-sigil filtering contract against skim's default engine —/// an `AndOrEngineFactory` wrapping an `ExactOrFuzzyEngineFactory`, the/// factory `Matcher::create_engine_factory` builds when the picker sets