Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: cache load_project_config() in Repository with OnceCell (#402)
Maximilian Roos4512ec0e80d6
diff --git a/src/commands/command_approval.rs b/src/commands/command_approval.rsindex fb032b476..93b1f8e9b 100644--- a/src/commands/command_approval.rs+++ b/src/commands/command_approval.rs@@ -19,7 +19,6 @@use super::hooks::{HookSource, ParsedFilter};use super::project_config::{HookCommand, collect_commands_for_hooks};-use super::repository_ext::RepositoryCliExt;use crate::output;use anyhow::Context;use color_print::cformat;diff --git a/src/commands/hook_commands.rs b/src/commands/hook_commands.rsindex 0dc26d676..23a134b40 100644--- a/src/commands/hook_commands.rs+++ b/src/commands/hook_commands.rs@@ -28,7 +28,6 @@ use super::merge::{execute_post_merge_commands, execute_pre_remove_commands, run_pre_merge_commands,};use super::project_config::collect_commands_for_hooks;-use super::repository_ext::RepositoryCliExt;/// Handle `wt hook` command///diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex c04e23271..650e71d37 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -5,7 +5,6 @@ use std::time::{SystemTime, UNIX_EPOCH};use super::worktree::{BranchDeletionMode, RemoveResult};use anyhow::Context;use color_print::cformat;-use worktrunk::config::ProjectConfig;use worktrunk::git::{GitError, IntegrationReason, Repository, parse_porcelain_z, parse_untracked_files,};@@ -24,9 +23,6 @@ pub enum RemoveTarget<'a> {/// CLI-only helpers implemented on [`Repository`] via an extension trait so we can keep orphan/// implementations inside the binary crate.pub trait RepositoryCliExt {- /// Load the project configuration if it exists.- fn load_project_config(&self) -> anyhow::Result<Option<ProjectConfig>>;-/// Warn about untracked files being auto-staged.fn warn_if_auto_staging_untracked(&self) -> anyhow::Result<()>;@@ -60,13 +56,6 @@ pub trait RepositoryCliExt {}impl RepositoryCliExt for Repository {- fn load_project_config(&self) -> anyhow::Result<Option<ProjectConfig>> {- match self.worktree_root() {- Ok(root) => ProjectConfig::load(root).context("Failed to load project config"),- Err(_) => Ok(None), // Not in a worktree, no project config- }- }-fn warn_if_auto_staging_untracked(&self) -> anyhow::Result<()> {// Use -z for NUL-separated output to handle filenames with spaces/newlineslet status = selfdiff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex 33f2f8b47..4315b64d0 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -9,6 +9,8 @@ use normalize_path::NormalizePath;use dunce::canonicalize;+use crate::config::ProjectConfig;+// Import types and functions from parent module (mod.rs)use super::{BranchCategory, CompletionBranch, DefaultBranchName, DiffStats, GitError, GitRemoteUrl,@@ -71,6 +73,8 @@ struct RepoCache {worktree_base: OnceCell<PathBuf>,/// Whether this is a bare repositoryis_bare: OnceCell<bool>,+ /// Project config (loaded from .config/wt.toml, cached per Repository instance)+ project_config: OnceCell<Option<ProjectConfig>>,}/// Repository context for git operations.@@ -1653,6 +1657,22 @@ impl Repository {.map(String::as_str)}+ /// Load the project configuration (.config/wt.toml) if it exists.+ ///+ /// Result is cached for the lifetime of this Repository instance.+ /// Returns `None` if not in a worktree or if no config file exists.+ pub fn load_project_config(&self) -> anyhow::Result<Option<ProjectConfig>> {+ self.cache+ .project_config+ .get_or_try_init(|| {+ match self.worktree_root() {+ Ok(root) => ProjectConfig::load(root).context("Failed to load project config"),+ Err(_) => Ok(None), // Not in a worktree, no project config+ }+ })+ .cloned()+ }+/// Get a short display name for this repository, used in logging context.////// Returns "." for the current directory, or the directory name otherwise.