Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor(CommitOptions): Unify context to use CommandContext

Maximilian Roos7b1a4ca8716c
diff --git a/src/commands/commit.rs b/src/commands/commit.rs
index 4327d3add..ee92a9b6a 100644
--- a/src/commands/commit.rs
+++ b/src/commands/commit.rs
@@ -1,21 +1,17 @@
-use std::path::Path;
use worktrunk::HookType;
-use worktrunk::config::{CommandPhase, CommitGenerationConfig, ProjectConfig, WorktrunkConfig};
+use worktrunk::config::{CommandPhase, CommitGenerationConfig, ProjectConfig};
use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{AnstyleStyle, CYAN, GREEN, HINT, WARNING, format_with_gutter};
+use super::command_executor::CommandContext;
use super::hooks::{HookFailureStrategy, HookPipeline};
use super::project_config::load_project_config;
/// Options for committing current changes.
pub struct CommitOptions<'a> {
- pub repo: &'a Repository,
- pub config: &'a WorktrunkConfig,
- pub worktree_path: &'a Path,
- pub current_branch: &'a str,
+ pub ctx: &'a CommandContext<'a>,
pub target_branch: Option<&'a str>,
pub no_verify: bool,
- pub force: bool,
pub tracked_only: bool,
pub auto_trust: bool,
pub warn_about_untracked: bool,
@@ -24,20 +20,11 @@ pub struct CommitOptions<'a> {
impl<'a> CommitOptions<'a> {
/// Convenience constructor for the common case where untracked files should trigger a warning.
- pub fn new(
- repo: &'a Repository,
- config: &'a WorktrunkConfig,
- worktree_path: &'a Path,
- current_branch: &'a str,
- ) -> Self {
+ pub fn new(ctx: &'a CommandContext<'a>) -> Self {
Self {
- repo,
- config,
- worktree_path,
- current_branch,
+ ctx,
target_branch: None,
no_verify: false,
- force: false,
tracked_only: false,
auto_trust: false,
warn_about_untracked: true,
@@ -162,51 +149,44 @@ pub fn commit_staged_changes(
/// Commit uncommitted changes with the shared commit pipeline.
pub fn commit_changes(options: CommitOptions<'_>) -> Result<(), GitError> {
if !options.no_verify
- && let Some(project_config) = load_project_config(options.repo)?
+ && let Some(project_config) = load_project_config(options.ctx.repo)?
{
run_pre_commit_commands(
&project_config,
- options.current_branch,
- options.worktree_path,
- options.repo,
- options.config,
- options.force,
+ options.ctx,
options.target_branch,
options.auto_trust,
)?;
}
if options.warn_about_untracked && !options.tracked_only {
- warn_untracked_auto_stage(options.repo)?;
+ warn_untracked_auto_stage(options.ctx.repo)?;
}
if options.tracked_only {
options
+ .ctx
.repo
.run_command(&["add", "-u"])
.git_context("Failed to stage tracked changes")?;
} else {
options
+ .ctx
.repo
.run_command(&["add", "-A"])
.git_context("Failed to stage changes")?;
}
commit_staged_changes(
- &options.config.commit_generation,
+ &options.ctx.config.commit_generation,
options.show_no_squash_note,
)
}
/// Run pre-commit commands sequentially (blocking, fail-fast).
-#[allow(clippy::too_many_arguments)]
pub fn run_pre_commit_commands(
project_config: &ProjectConfig,
- current_branch: &str,
- worktree_path: &Path,
- repo: &Repository,
- config: &WorktrunkConfig,
- force: bool,
+ ctx: &CommandContext,
target_branch: Option<&str>,
auto_trust: bool,
) -> Result<(), GitError> {
@@ -214,14 +194,13 @@ pub fn run_pre_commit_commands(
return Ok(());
};
- let repo_root = repo.worktree_base()?;
let pipeline = HookPipeline::new(
- repo,
- config,
- current_branch,
- worktree_path,
- &repo_root,
- force,
+ ctx.repo,
+ ctx.config,
+ ctx.branch,
+ ctx.worktree_path,
+ ctx.repo_root,
+ ctx.force,
);
let extra_vars: Vec<(&str, &str)> = target_branch
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index f0fe3f1fb..0edba2851 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -4,6 +4,7 @@ use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{CYAN, CYAN_BOLD, ERROR, ERROR_EMOJI, GREEN_BOLD, HINT, HINT_EMOJI};
use super::command_approval::approve_command_batch;
+use super::command_executor::CommandContext;
use super::commit::{CommitOptions, commit_changes, warn_untracked_auto_stage};
use super::context::CommandEnv;
use super::hooks::{HookFailureStrategy, HookPipeline};
@@ -129,9 +130,18 @@ pub fn handle_merge(
false // Staged but didn't commit (will squash later)
} else {
// Commit immediately when not squashing
- let mut options = CommitOptions::new(&repo, &config, &worktree_path, &current_branch);
+ let repo_root = repo.worktree_base()?;
+ let commit_ctx = CommandContext::new(
+ &repo,
+ &config,
+ &current_branch,
+ &worktree_path,
+ &repo_root,
+ force,
+ );
+ let mut options = CommitOptions::new(&commit_ctx);
options.target_branch = Some(&target_branch);
- options.force = force;
+ options.no_verify = no_verify;
options.tracked_only = tracked_only;
options.auto_trust = true; // commands already approved in merge batch
options.warn_about_untracked = !tracked_only;
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index c574b60d3..f87571d65 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -4,6 +4,7 @@ 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,
@@ -43,13 +44,12 @@ pub fn handle_standalone_run_hook(hook_type: HookType, force: bool) -> Result<()
check_hook_configured(&project_config.pre_commit_command, hook_type)?;
// Pre-commit hook can optionally use target branch context
let 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,
- &branch,
- &worktree_path,
- &repo,
- &config,
- force,
+ &ctx,
target_branch.as_deref(),
false, // auto_trust: standalone command, needs approval
)
@@ -103,9 +103,17 @@ pub fn handle_standalone_commit(force: bool, no_verify: bool) -> Result<(), GitE
worktree_path,
} = CommandEnv::current()?;
- let mut options = CommitOptions::new(&repo, &config, &worktree_path, &current_branch);
+ let repo_root = repo.worktree_base()?;
+ let commit_ctx = CommandContext::new(
+ &repo,
+ &config,
+ &current_branch,
+ &worktree_path,
+ &repo_root,
+ force,
+ );
+ let mut options = CommitOptions::new(&commit_ctx);
options.no_verify = no_verify;
- options.force = force;
options.auto_trust = false;
options.show_no_squash_note = false;
@@ -136,16 +144,16 @@ pub fn handle_standalone_squash(
// Run pre-commit hook unless explicitly skipped
if !skip_pre_commit && let Some(project_config) = load_project_config(&repo)? {
- run_pre_commit_commands(
- &project_config,
- &current_branch,
- &worktree_path,
+ let repo_root = repo.worktree_base()?;
+ let ctx = CommandContext::new(
&repo,
&config,
+ &current_branch,
+ &worktree_path,
+ &repo_root,
force,
- Some(&target_branch),
- auto_trust,
- )?;
+ );
+ run_pre_commit_commands(&project_config, &ctx, Some(&target_branch), auto_trust)?;
}
// Get merge base with target branch