Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Improve history preview for selected branch
Maximilian Roos5eb691a2f205
diff --git a/src/commands/select.rs b/src/commands/select.rsindex ce9419987..300bf45b6 100644--- a/src/commands/select.rs+++ b/src/commands/select.rs@@ -135,46 +135,69 @@ impl WorktreeSkimItem {/// Render Mode 2: History previewfn render_history_preview(&self) -> String {- const HISTORY_LIMIT: &str = "40";+ const HISTORY_LIMIT: &str = "10";let mut output = String::new();let repo = Repository::current();+ let head = self.item.head();++ // Get merge-base with main+ //+ // Note on error handling: This code runs in an interactive preview pane that updates+ // on every keystroke. We intentionally use silent fallbacks rather than propagating+ // errors to avoid disruptive error messages during navigation. The preview is+ // supplementary - users can still select worktrees even if preview fails.+ //+ // Alternative: Check specific conditions (main branch exists, valid HEAD, etc.) before+ // running git commands. This would provide better diagnostics but adds latency to+ // every preview render. Trade-off: simplicity + speed vs. detailed error messages.+ let Ok(merge_base_output) = repo.run_command(&["merge-base", "main", head]) else {+ output.push_str("No commits\n");+ return output;+ };- let counts = self.item.counts();+ let merge_base = merge_base_output.trim();- // Show commits since diverging from main, with merge-base boundary- if counts.ahead > 0 {- // Use --boundary to show the merge-base commit where we diverged- let head = self.item.head();- let range = format!("main..{}", head);+ let branch = self.item.branch_name();+ let is_main = branch == "main" || branch == "master";++ if is_main {+ // Viewing main itself - show history without dimmingif let Ok(log_output) = repo.run_command(&["log","--graph",- "--boundary","--decorate","--oneline","--color=always",- &range,+ "-n",+ HISTORY_LIMIT,+ head,]) {output.push_str(&log_output);- } else {- output.push_str("No commits\n");}} else {- // Not ahead of main - just show recent history+ // Not on main - show bright commits not on main, dimmed commits on main++ // Part 1: Bright commits (merge-base..HEAD)+ let range = format!("{}..{}", merge_base, head);+ if let Ok(log_output) =+ repo.run_command(&["log", "--graph", "--oneline", "--color=always", &range])+ {+ output.push_str(&log_output);+ }++ // Part 2: Dimmed commits on main (history before merge-base)if let Ok(log_output) = repo.run_command(&["log","--graph",- "--decorate","--oneline",+ "--format=%C(dim)%h %s%C(reset)","--color=always","-n",HISTORY_LIMIT,- self.item.head(),+ merge_base,]) {output.push_str(&log_output);- } else {- output.push_str("No commits\n");}}diff --git a/src/main.rs b/src/main.rsindex 33a38a0a5..b61a7134a 100644--- a/src/main.rs+++ b/src/main.rs@@ -151,6 +151,13 @@ enum BetaCommand {},/// Interactive worktree selector+ ///+ /// Preview modes (toggle with 1/2/3):+ /// - Mode 1: Working tree changes+ /// - Mode 2: History (commits not on main shown bright, commits on main dimmed)+ /// - Mode 3: Branch diff (line changes ahead of main)+ ///+ /// Note: When viewing main itself, all commits shown without dimmingSelect,}diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 6ec46f075..e74551bc5 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpLSFnWp/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpQ1zKBh/test-config.toml---success: trueexit_code: 0@@ -280,7 +280,7 @@ complete -c wt -n "__fish_wt_using_subcommand beta; and __fish_seen_subcommand_fcomplete -c wt -n "__fish_wt_using_subcommand beta; and __fish_seen_subcommand_from ask-approvals" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'complete -c wt -n "__fish_wt_using_subcommand beta; and __fish_seen_subcommand_from ask-approvals" -s h -l help -d 'Print help'complete -c wt -n "__fish_wt_using_subcommand beta; and __fish_seen_subcommand_from select" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'-complete -c wt -n "__fish_wt_using_subcommand beta; and __fish_seen_subcommand_from select" -s h -l help -d 'Print help'+complete -c wt -n "__fish_wt_using_subcommand beta; and __fish_seen_subcommand_from select" -s h -l help -d 'Print help (see more with \'--help\')'complete -c wt -n "__fish_wt_using_subcommand list" -l format -d 'Output format' -r -f -a "table\t'Human-readable table format'json\t'Machine-readable JSON with display fields (includes styled unicode for rendering)'"complete -c wt -n "__fish_wt_using_subcommand list" -l branches -d 'Include branches without worktrees'diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 903a5c4fd..33a95645f 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpm0uTSY/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpGm8Bss/test-config.toml---success: trueexit_code: 0@@ -348,8 +348,8 @@ _arguments "${_arguments_options[@]}" : \_arguments "${_arguments_options[@]}" : \'-v[Enable verbose output (show git commands and debug info)]' \'--verbose[Enable verbose output (show git commands and debug info)]' \-'-h[Print help]' \-'--help[Print help]' \+'-h[Print help (see more with '\''--help'\'')]' \+'--help[Print help (see more with '\''--help'\'')]' \&& ret=0;;esac