Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(config): resolve list timeout from per-project config (#1063)
Maximilian Roos37f04c20feb7
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rsindex 0934909d4..cb4993305 100644--- a/src/commands/list/collect/mod.rs+++ b/src/commands/list/collect/mod.rs@@ -211,7 +211,6 @@ pub enum ShowConfig {cli_branches: bool,cli_remotes: bool,cli_full: bool,- raw_timeout: Option<std::time::Duration>,},}@@ -230,7 +229,6 @@ pub fn collect(show_config: ShowConfig,show_progress: bool,render_table: bool,- config: &worktrunk::config::UserConfig,skip_expensive_for_stale: bool,) -> anyhow::Result<Option<super::model::ListData>> {use super::progressive_table::ProgressiveTable;@@ -276,7 +274,6 @@ pub fn collect(let worktrees_cell: OnceCell<anyhow::Result<Vec<WorktreeInfo>>> = OnceCell::new();let default_branch_cell: OnceCell<Option<String>> = OnceCell::new();let url_template_cell: OnceCell<Option<String>> = OnceCell::new();- let project_id_cell: OnceCell<Option<String>> = OnceCell::new();let local_branches_cell: OnceCell<anyhow::Result<Vec<(String, String)>>> = OnceCell::new();let remote_branches_cell: OnceCell<anyhow::Result<Vec<(String, String)>>> = OnceCell::new();@@ -295,10 +292,10 @@ pub fn collect(let _ = url_template_cell.set(repo.url_template());});s.spawn(|_| {- // Warm project_identifier cache — used by is_worktree_at_expected_path- // (via compute_worktree_path) and config resolution. Running this here+ // Warm project_identifier + user config caches — used by+ // is_worktree_at_expected_path and config resolution. Running this here// avoids sequential git commands later on the critical path.- let _ = project_id_cell.set(repo.project_identifier().ok());+ let _ = repo.config();});s.spawn(|_| {if fetch_branches {@@ -322,9 +319,8 @@ pub fn collect(}let default_branch = default_branch_cell.into_inner().unwrap();let url_template = url_template_cell.into_inner().unwrap();- let project_id = project_id_cell.into_inner().unwrap();- // Resolve show flags: merge CLI overrides with config (now that project_id is available)+ // Resolve show flags: merge CLI overrides with config (warmed in parallel phase)let (show_branches, show_remotes, skip_tasks, command_timeout) = match show_config {ShowConfig::Resolved {show_branches,@@ -336,12 +332,11 @@ pub fn collect(cli_branches,cli_remotes,cli_full,- raw_timeout,} => {- let resolved = config.resolved(project_id.as_deref());- let show_branches = cli_branches || resolved.list.branches();- let show_remotes = cli_remotes || resolved.list.remotes();- let show_full = cli_full || resolved.list.full();+ let config = repo.config();+ let show_branches = cli_branches || config.list.branches();+ let show_remotes = cli_remotes || config.list.remotes();+ let show_full = cli_full || config.list.full();let skip_tasks: HashSet<TaskKind> = if show_full {HashSet::new()} else {@@ -353,8 +348,16 @@ pub fn collect(.into_iter().collect()};- // --full disables timeout for complete data collection- let command_timeout = if show_full { None } else { raw_timeout };+ // Resolve timeout from merged config (--full disables timeout)+ let command_timeout = if show_full {+ None+ } else {+ config+ .list+ .timeout_ms()+ .filter(|&ms| ms > 0) // 0 means "no timeout" (explicit disable)+ .map(std::time::Duration::from_millis)+ };(show_branches, show_remotes, skip_tasks, command_timeout)}};@@ -476,7 +479,8 @@ pub fn collect(let is_previous = false;// Check if worktree is at its expected path based on config template- let branch_worktree_mismatch = !is_worktree_at_expected_path(wt, repo, config);+ let branch_worktree_mismatch =+ !is_worktree_at_expected_path(wt, repo, repo.user_config());let mut worktree_data =WorktreeData::from_worktree(wt, is_main, is_current, is_previous);diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rsindex 6e6e797aa..67d2aceaf 100644--- a/src/commands/list/mod.rs+++ b/src/commands/list/mod.rs@@ -150,7 +150,6 @@ pub fn handle_list(cli_remotes: bool,cli_full: bool,render_mode: RenderMode,- config: &worktrunk::config::UserConfig,) -> anyhow::Result<()> {// Progressive rendering only for table format with Progressive modelet show_progress = match format {@@ -169,27 +168,15 @@ pub fn handle_list(// For testing: allow enabling skip_expensive_for_stale via env varlet skip_expensive_for_stale = std::env::var("WORKTRUNK_TEST_SKIP_EXPENSIVE_THRESHOLD").is_ok();- // Raw timeout from global config (--full override applied in collect after- // config resolution, since show_full depends on project-specific config)- let raw_timeout = config- .configs- .list- .as_ref()- .and_then(|l| l.timeout_ms)- .filter(|&ms| ms > 0) // 0 means "no timeout" (explicit disable)- .map(std::time::Duration::from_millis);-let list_data = collect::collect(&repo,collect::ShowConfig::DeferredToParallel {cli_branches,cli_remotes,cli_full,- raw_timeout,},show_progress,render_table,- config,skip_expensive_for_stale,)?;diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rsindex ef5e8d3fa..1de3f095f 100644--- a/src/commands/select/mod.rs+++ b/src/commands/select/mod.rs@@ -13,7 +13,6 @@ use std::sync::Arc;use anyhow::Context;use dashmap::DashMap;use skim::prelude::*;-use worktrunk::config::UserConfig;use worktrunk::git::Repository;use super::handle_switch::{@@ -28,11 +27,7 @@ use crate::output::handle_switch_output;use items::{HeaderSkimItem, PreviewCache, WorktreeSkimItem};use preview::{PreviewLayout, PreviewMode, PreviewState};-pub fn handle_select(- show_branches: bool,- show_remotes: bool,- config: &UserConfig,-) -> anyhow::Result<()> {+pub fn handle_select(cli_branches: bool, cli_remotes: bool) -> anyhow::Result<()> {// Interactive picker requires a terminal for the TUIif !std::io::stdin().is_terminal() {anyhow::bail!("Interactive picker requires an interactive terminal");@@ -40,6 +35,11 @@ pub fn handle_select(let repo = Repository::current()?;+ // Merge CLI flags with resolved config+ let config = repo.config();+ let show_branches = cli_branches || config.list.branches();+ let show_remotes = cli_remotes || config.list.remotes();+// Initialize preview mode state file (auto-cleanup on drop)let state = PreviewState::new();@@ -69,8 +69,7 @@ pub fn handle_select(},false, // show_progress (no progress bars)false, // render_table (select renders its own UI)- config,- true, // skip_expensive_for_stale (faster for repos with many stale branches)+ true, // skip_expensive_for_stale (faster for repos with many stale branches))?else {return Ok(());@@ -274,18 +273,18 @@ pub fn handle_select(};// Load config- let config = UserConfig::load().context("Failed to load config")?;let repo = Repository::current().context("Failed to switch worktree")?;+ let config = repo.user_config();// Switch to existing worktree or create new one- let plan = plan_switch(&repo, &identifier, should_create, None, false, &config)?;- let skip_hooks = !approve_switch_hooks(&repo, &config, &plan, false, true)?;- let (result, branch_info) = execute_switch(&repo, plan, &config, false, skip_hooks)?;+ let plan = plan_switch(&repo, &identifier, should_create, None, false, config)?;+ let skip_hooks = !approve_switch_hooks(&repo, config, &plan, false, true)?;+ let (result, branch_info) = execute_switch(&repo, plan, config, false, skip_hooks)?;// Compute path mismatch lazily (deferred from plan_switch for existing worktrees)let branch_info = match &result {SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {- let expected_path = get_path_mismatch(&repo, &branch_info.branch, path, &config);+ let expected_path = get_path_mismatch(&repo, &branch_info.branch, path, config);SwitchBranchInfo {expected_path,..branch_info@@ -306,7 +305,7 @@ pub fn handle_select(let extra_vars = switch_extra_vars(&result);spawn_switch_background_hooks(&repo,- &config,+ config,&result,&branch_info.branch,false,diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex cb2b16bc5..fd2913b2f 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -33,7 +33,7 @@ use anyhow::{Context, bail};use dunce::canonicalize;-use crate::config::ProjectConfig;+use crate::config::{ProjectConfig, ResolvedConfig, UserConfig};// Import types from parent moduleuse super::{DefaultBranchName, GitError, LineDiff, WorktreeInfo};@@ -114,6 +114,12 @@ pub(super) struct RepoCache {pub(super) project_identifier: OnceCell<String>,/// Project config (loaded from .config/wt.toml in main worktree)pub(super) project_config: OnceCell<Option<ProjectConfig>>,+ /// User config (raw, as loaded from disk).+ /// Lazily loaded on first access.+ pub(super) user_config: OnceCell<UserConfig>,+ /// Resolved user config (global merged with per-project overrides, defaults applied).+ /// Lazily loaded on first access via `Repository::config()`.+ pub(super) resolved_config: OnceCell<ResolvedConfig>,/// Sparse checkout paths (empty if not a sparse checkout)pub(super) sparse_checkout_paths: OnceCell<Vec<String>>,/// Merge-base cache: (commit1, commit2) -> merge_base_sha (None = no common ancestor)@@ -239,6 +245,32 @@ impl Repository {})}+ /// Resolved user config (global merged with per-project overrides, defaults applied).+ ///+ /// Lazily loads `UserConfig` and resolves it using this repository's project identifier.+ /// Cached for the lifetime of the repository (shared across clones via Arc).+ ///+ /// Falls back to default config if loading fails (e.g., no config file).+ pub fn config(&self) -> &ResolvedConfig {+ self.cache.resolved_config.get_or_init(|| {+ let project_id = self.project_identifier().ok();+ self.user_config().resolved(project_id.as_deref())+ })+ }++ /// Raw user config (as loaded from disk, before project-specific resolution).+ ///+ /// Prefer [`config()`](Self::config) for behavior settings. This is only needed+ /// for operations that require the full `UserConfig` (e.g., path template formatting,+ /// approval state, hook resolution).+ pub fn user_config(&self) -> &UserConfig {+ self.cache.user_config.get_or_init(|| {+ UserConfig::load()+ .inspect_err(|err| log::warn!("Failed to load user config, using defaults: {err}"))+ .unwrap_or_default()+ })+ }+/// Check if this repository shares its cache with another.////// Returns true if both repositories point to the same underlying cache.diff --git a/src/main.rs b/src/main.rsindex 461b168ea..4c59aa1a5 100644--- a/src/main.rs+++ b/src/main.rs@@ -635,21 +635,7 @@ fn main() {warning_message("wt select is deprecated; use wt switch instead"));- UserConfig::load()- .context("Failed to load config")- .and_then(|config| {- // Get effective settings (project-specific merged with global, defaults applied)- let project_id = Repository::current()- .ok()- .and_then(|r| r.project_identifier().ok());- let resolved = config.resolved(project_id.as_deref());-- // CLI flags override config- let show_branches = branches || resolved.list.branches();- let show_remotes = remotes || resolved.list.remotes();-- handle_select(show_branches, show_remotes, &config)- })+ handle_select(branches, remotes)}#[cfg(not(unix))]Commands::Select { .. } => {@@ -691,24 +677,17 @@ fn main() {};commands::statusline::run(effective_format)}- None => {- // Config resolution is deferred to collect's parallel phase so- // project_identifier runs concurrently with other git commands- // instead of blocking the critical path.- UserConfig::load()- .context("Failed to load config")- .and_then(|config| {- let repo = Repository::current()?;-- let progressive_opt = match (progressive, no_progressive) {- (true, _) => Some(true),- (_, true) => Some(false),- _ => None,- };- let render_mode = RenderMode::detect(progressive_opt);- handle_list(repo, format, branches, remotes, full, render_mode, &config)- })- }+ None => (|| {+ let repo = Repository::current()?;++ let progressive_opt = match (progressive, no_progressive) {+ (true, _) => Some(true),+ (_, true) => Some(false),+ _ => None,+ };+ let render_mode = RenderMode::detect(progressive_opt);+ handle_list(repo, format, branches, remotes, full, render_mode)+ })(),},Commands::Switch {branch,@@ -729,22 +708,7 @@ fn main() {let Some(branch) = branch else {#[cfg(unix)]{- // Get project ID for per-project config lookup- let project_id = Repository::current()- .ok()- .and_then(|r| r.project_identifier().ok());-- // Get effective list config (merges project-specific with global)- let (show_branches_config, show_remotes_config) = config- .list(project_id.as_deref())- .map(|l| (l.branches.unwrap_or(false), l.remotes.unwrap_or(false)))- .unwrap_or((false, false));-- // CLI flags override config- let show_branches = branches || show_branches_config;- let show_remotes = remotes || show_remotes_config;-- return handle_select(show_branches, show_remotes, &config);+ return handle_select(branches, remotes);}#[cfg(not(unix))]