Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(commands): Introduce `CommandEnv::context` helper
Maximilian Roos036729b34faa
diff --git a/src/commands/context.rs b/src/commands/context.rsindex 92b99726c..f0d4f4275 100644--- a/src/commands/context.rs+++ b/src/commands/context.rs@@ -2,6 +2,8 @@ use std::path::PathBuf;use worktrunk::config::WorktrunkConfig;use worktrunk::git::{GitError, GitResultExt, Repository};+use super::command_executor::CommandContext;+/// Shared execution context for command handlers that operate on the current worktree.////// Centralizes the common “repo + branch + config + cwd” setup so individual handlers@@ -17,6 +19,7 @@ pub struct CommandEnv {pub branch: String,pub config: WorktrunkConfig,pub worktree_path: PathBuf,+ pub repo_root: PathBuf,}impl CommandEnv {@@ -31,12 +34,26 @@ impl CommandEnv {.git_context("Failed to get current branch")?.ok_or(GitError::DetachedHead)?;let config = WorktrunkConfig::load().git_context("Failed to load config")?;+ let repo_root = repo.worktree_base()?;Ok(Self {repo,branch,config,worktree_path,+ repo_root,})}++ /// Build a `CommandContext` tied to this environment.+ pub fn context(&self, force: bool) -> CommandContext<'_> {+ CommandContext::new(+ &self.repo,+ &self.config,+ &self.branch,+ &self.worktree_path,+ &self.repo_root,+ force,+ )+ }}diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 5ee4f91eb..5109ba6ef 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -67,12 +67,10 @@ pub fn handle_merge(force: bool,tracked_only: bool,) -> Result<(), GitError> {- let CommandEnv {- repo,- branch: current_branch,- config,- worktree_path,- } = CommandEnv::current()?;+ let env = CommandEnv::current()?;+ let repo = &env.repo;+ let config = &env.config;+ let current_branch = env.branch.clone();// Validate --no-commit: requires clean working treeif no_commit && repo.is_dirty()? {@@ -97,7 +95,7 @@ pub fn handle_merge(// Collect and approve all commands upfront for batch permission requestlet (all_commands, project_id) = MergeCommandCollector {- repo: &repo,+ repo,no_commit,no_verify,}@@ -105,22 +103,14 @@ pub fn handle_merge(// Approve all commands in a single batch// Commands collected here are not yet expanded - expansion happens later in prepare_project_commands- approve_command_batch(&all_commands, &project_id, &config, force, false)?;+ approve_command_batch(&all_commands, &project_id, config, force, false)?;// Handle uncommitted changes (skip if --no-commit) - track whether commit occurredlet committed = if !no_commit && repo.is_dirty()? {if squash_enabled {false // Squash path handles staging and committing} else {- let repo_root = repo.worktree_base()?;- let ctx = CommandContext::new(- &repo,- &config,- ¤t_branch,- &worktree_path,- &repo_root,- force,- );+ let ctx = env.context(force);let mut options = CommitOptions::new(&ctx);options.target_branch = Some(&target_branch);options.no_verify = no_verify;@@ -159,16 +149,8 @@ pub fn handle_merge(// Run pre-merge checks unless --no-verify was specified// Do this after commit/squash/rebase to validate the final state that will be pushed- if !no_verify && let Some(project_config) = load_project_config(&repo)? {- let repo_root = repo.worktree_base()?;- let ctx = CommandContext::new(- &repo,- &config,- ¤t_branch,- &worktree_path,- &repo_root,- force,- );+ if !no_verify && let Some(project_config) = load_project_config(repo)? {+ let ctx = env.context(force);run_pre_merge_commands(&project_config, &ctx, &target_branch)?;}@@ -224,12 +206,13 @@ pub fn handle_merge(// This runs after cleanup so the context is clear to the user// Create a fresh Repository instance at the primary worktree (the old repo may be invalid)let primary_repo = Repository::at(&primary_worktree_dir);+ let primary_repo_root = primary_worktree_dir.clone();let ctx = CommandContext::new(&primary_repo,- &config,+ config,¤t_branch,&primary_worktree_dir,- &primary_worktree_dir, // For primary worktree, worktree_path == repo_root+ &primary_repo_root,force,);execute_post_merge_commands(&ctx, &target_branch)?;diff --git a/src/commands/standalone.rs b/src/commands/standalone.rsindex b882e9673..96326cb8b 100644--- a/src/commands/standalone.rs+++ b/src/commands/standalone.rs@@ -4,7 +4,6 @@ use worktrunk::styling::{AnstyleStyle, CYAN, CYAN_BOLD, GREEN_BOLD, HINT, HINT_EMOJI, eprintln, format_with_gutter,};-use super::command_executor::CommandContext;use super::commit::{CommitOptions, commit_changes, commit_staged_changes, format_commit_message_for_display,run_pre_commit_commands, show_llm_config_hint_if_needed, warn_untracked_auto_stage,@@ -19,15 +18,12 @@ use super::worktree::{execute_post_create_commands, execute_post_start_commands_/// Handle `wt beta run-hook` commandpub fn handle_standalone_run_hook(hook_type: HookType, force: bool) -> Result<(), GitError> {// Derive context from current environment- let CommandEnv {- repo,- branch,- config,- worktree_path,- } = CommandEnv::current()?;+ let env = CommandEnv::current()?;+ let repo = &env.repo;+ let ctx = env.context(force);// Load project config (show helpful error if missing)- let project_config = require_project_config(&repo)?;+ let project_config = require_project_config(repo)?;// TODO: Add support for custom variable overrides (e.g., --var key=value)// This would allow testing hooks with different contexts without being in that context@@ -36,25 +32,16 @@ pub fn handle_standalone_run_hook(hook_type: HookType, force: bool) -> Result<()match hook_type {HookType::PostCreate => {check_hook_configured(&project_config.post_create_command, hook_type)?;- let repo_root = repo.worktree_base()?;- let ctx =- CommandContext::new(&repo, &config, &branch, &worktree_path, &repo_root, force);execute_post_create_commands(&ctx)}HookType::PostStart => {check_hook_configured(&project_config.post_start_command, hook_type)?;- let repo_root = repo.worktree_base()?;- let ctx =- CommandContext::new(&repo, &config, &branch, &worktree_path, &repo_root, force);execute_post_start_commands_sequential(&ctx)}HookType::PreCommit => {check_hook_configured(&project_config.pre_commit_command, hook_type)?;// Pre-commit hook can optionally use target branch contextlet target_branch = repo.default_branch().ok();- let repo_root = repo.worktree_base()?;- let ctx =- CommandContext::new(&repo, &config, &branch, &worktree_path, &repo_root, force);run_pre_commit_commands(&project_config,&ctx,@@ -65,17 +52,11 @@ pub fn handle_standalone_run_hook(hook_type: HookType, force: bool) -> Result<()HookType::PreMerge => {check_hook_configured(&project_config.pre_merge_command, hook_type)?;let target_branch = repo.default_branch().unwrap_or_else(|_| "main".to_string());- let repo_root = repo.worktree_base()?;- let ctx =- CommandContext::new(&repo, &config, &branch, &worktree_path, &repo_root, force);run_pre_merge_commands(&project_config, &ctx, &target_branch)}HookType::PostMerge => {check_hook_configured(&project_config.post_merge_command, hook_type)?;let target_branch = repo.default_branch().unwrap_or_else(|_| "main".to_string());- let repo_root = repo.worktree_base()?;- let ctx =- CommandContext::new(&repo, &config, &branch, &worktree_path, &repo_root, force);execute_post_merge_commands(&ctx, &target_branch)}}@@ -96,15 +77,7 @@ fn check_hook_configured<T>(hook: &Option<T>, hook_type: HookType) -> Result<(),/// Handle `wt beta commit` commandpub fn handle_standalone_commit(force: bool, no_verify: bool) -> Result<(), GitError> {let env = CommandEnv::current()?;- let repo_root = env.repo.worktree_base()?;- let ctx = CommandContext::new(- &env.repo,- &env.config,- &env.branch,- &env.worktree_path,- &repo_root,- force,- );+ let ctx = env.context(force);let mut options = CommitOptions::new(&ctx);options.no_verify = no_verify;options.auto_trust = false;@@ -129,19 +102,17 @@ pub fn handle_squash(tracked_only: bool,warn_about_untracked: bool,) -> Result<bool, GitError> {- let CommandEnv {- repo,- branch: current_branch,- config,- worktree_path,- } = CommandEnv::current()?;+ let env = CommandEnv::current()?;+ let repo = &env.repo;+ let current_branch = env.branch.clone();+ let ctx = env.context(force);// Get target branch (default to default branch if not provided)let target_branch = repo.resolve_target_branch(target)?;// Auto-stage changes before running pre-commit hooks so both beta and merge paths behave identicallyif warn_about_untracked && !tracked_only {- warn_untracked_auto_stage(&repo)?;+ warn_untracked_auto_stage(repo)?;}if tracked_only {@@ -153,16 +124,7 @@ pub fn handle_squash(}// Run pre-commit hook unless explicitly skipped- if !skip_pre_commit && let Some(project_config) = load_project_config(&repo)? {- let repo_root = repo.worktree_base()?;- let ctx = CommandContext::new(- &repo,- &config,- ¤t_branch,- &worktree_path,- &repo_root,- force,- );+ if !skip_pre_commit && let Some(project_config) = load_project_config(repo)? {run_pre_commit_commands(&project_config, &ctx, Some(&target_branch), auto_trust)?;}@@ -183,7 +145,7 @@ pub fn handle_squash(if commit_count == 0 && has_staged {// Just staged changes, no commits - commit them directly (no squashing needed)- commit_staged_changes(&config.commit_generation, true)?;+ commit_staged_changes(&env.config.commit_generation, true)?;return Ok(true);}@@ -224,7 +186,7 @@ pub fn handle_squash(// Generate squash commit messagecrate::output::progress(format!("{CYAN}Generating squash commit message...{CYAN:#}"))?;- show_llm_config_hint_if_needed(&config.commit_generation)?;+ show_llm_config_hint_if_needed(&env.config.commit_generation)?;// Get current branch and repo name for template variableslet repo_root = repo.worktree_root()?;@@ -238,7 +200,7 @@ pub fn handle_squash(&subjects,¤t_branch,repo_name,- &config.commit_generation,+ &env.config.commit_generation,).git_context("Failed to generate commit message")?;