Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
use anyhow::Context;
use color_print::cformat;
use worktrunk::HookType;
use worktrunk::config::CommitGenerationConfig;
use worktrunk::styling::{
eprintln, format_with_gutter, hint_message, info_message, progress_message, success_message,
};
use super::command_executor::CommandContext;
use super::command_executor::FailureStrategy;
use super::hooks::{HookAnnouncer, execute_hook};
use super::repository_ext::auto_stage;
use super::template_vars::TemplateVars;
// Re-export StageMode from config for use by CLI
pub use worktrunk::config::StageMode;
/// Outcome of a successful commit operation. Returned so callers (e.g.
/// `step commit --format=json`) can render structured output.
///
/// `stage_mode` is the *resolved* mode that was actually applied (CLI flag
/// merged with config defaults), not the user-supplied flag.
#[derive(serde::Serialize)]
pub struct CommitOutcome {
#[serde(rename = "commit")]
pub sha: String,
pub message: String,
pub stage_mode: StageMode,
}
/// Whether pre/post-commit hooks run, and — if not — whether to print the skip message.
/// Two distinct paths disable hooks: `--no-hooks` (we own the skip message) and declined
/// approval (the caller already printed its own message).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HookGate {
/// Run pre-commit and post-commit hooks normally.
Run,
/// Skip hooks because the user passed `--no-hooks`. `commit()` prints the skip message.
NoHooksFlag,
/// Skip hooks silently — caller has already explained why.
Silent,
}
impl HookGate {
/// Whether pre/post-commit hooks should execute.
pub(crate) fn run(self) -> bool {
matches!(self, Self::Run)
}
/// Build from an entry-point `verify` flag plus an approval-prompt result.
/// `verify=false` → `NoHooksFlag`; declined → `Silent`; approved → `Run`.
pub(crate) fn from_approval(verify: bool, approved: bool) -> Self {
if !verify {
Self::NoHooksFlag
} else if approved {
Self::Run
} else {
Self::Silent
}
}
}
/// Options for committing current changes.
pub struct CommitOptions<'a> {
pub ctx: &'a CommandContext<'a>,
pub target_branch: Option<&'a str>,
pub hooks: HookGate,
pub stage_mode: StageMode,
pub show_no_squash_note: bool,
/// Whether `commit()` runs its own guidance approval gate or trusts a
/// value supplied by the caller (`wt merge` resolves the guidance via
/// `approve_commit_template_append` up front and passes it through).
pub guidance: super::step::PreApprovedGuidance,
}
impl<'a> CommitOptions<'a> {
pub fn new(ctx: &'a CommandContext<'a>) -> Self {
Self {
ctx,
target_branch: None,
hooks: HookGate::Run,
stage_mode: StageMode::All,
show_no_squash_note: false,
guidance: super::step::PreApprovedGuidance::RunOwnGate,
}
}
}
pub(crate) struct CommitGenerator<'a> {
config: &'a CommitGenerationConfig,
/// Approved project-level append fragment for the LLM prompt. `None` if
/// not configured or the user declined approval. The user-level
/// `template-append` rides along inside `config` and needs no approval.
project_append: Option<&'a str>,
}
impl<'a> CommitGenerator<'a> {
pub fn new(config: &'a CommitGenerationConfig, project_append: Option<&'a str>) -> Self {
Self {
config,
project_append,
}
}
pub fn format_message_for_display(&self, message: &str) -> String {
let lines: Vec<&str> = message.lines().collect();
if lines.is_empty() {
return String::new();
}
let mut result = cformat!("<bold>{}</>", lines[0]);
if lines.len() > 1 {
for line in &lines[1..] {
result.push('\n');
result.push_str(line);
}
}
result
}
pub fn emit_hint_if_needed(&self) {
if !self.config.is_configured() {
eprintln!(
"{}",
hint_message(cformat!(
"Using fallback commit message. For LLM setup guide, run <underline>wt config --help</>"
))
);
}
}
/// Commit staged changes in the given worktree.
///
/// When `show_progress` is true, displays a progress message with diff stats
/// before committing. Set to false for bulk operations where each worktree
/// is handled individually (e.g., `step relocate --commit`).
pub fn commit_staged_changes(
&self,
wt: &worktrunk::git::WorkingTree<'_>,
show_progress: bool,
show_no_squash_note: bool,
stage_mode: StageMode,
) -> crate::output::CommandResult<CommitOutcome> {
// Fail early if nothing is staged (avoids confusing LLM prompt with empty diff)
if !wt.has_staged_changes()? {
return Err(anyhow::anyhow!("Nothing to commit").into());
}
if show_progress {
let stats_parts = wt.prepare_staged_diff(wt.index_base()?).stats_summary();
let changes_type = match stage_mode {
StageMode::Tracked => "tracked changes",
_ => "changes",
};
let action = if self.config.is_configured() {
format!("Generating commit message and committing {changes_type}...")
} else {
format!("Committing {changes_type} with default message...")
};
let mut parts = vec![];
if !stats_parts.is_empty() {
parts.extend(stats_parts);
}
if show_no_squash_note {
parts.push("no squashing needed".to_string());
}
let full_progress_msg = if parts.is_empty() {
action
} else {
// Gray parenthetical with separate cformat for closing paren (avoids optimizer)
let parts_str = parts.join(", ");
let paren_close = cformat!("<bright-black>)</>");
cformat!("{action} <bright-black>({parts_str}</>{paren_close}")
};
eprintln!("{}", progress_message(full_progress_msg));
}
self.emit_hint_if_needed();
// `wt` — not the invoking worktree: `--branch <b>` and `step relocate
// --commit` both commit elsewhere, and the prompt must describe the
// diff that is about to be committed.
let commit_message =
crate::llm::generate_commit_message(self.config, wt, None, self.project_append)?;
let formatted_message = self.format_message_for_display(&commit_message);
eprintln!("{}", format_with_gutter(&formatted_message, None));
wt.run_command(&["commit", "-m", &commit_message])
.context("Failed to commit")?;
let commit_sha = wt.run_command(&["rev-parse", "HEAD"])?.trim().to_string();
let outcome = CommitOutcome {
sha: commit_sha,
message: commit_message,
stage_mode,
};
// The commit identity is known even if display preparation fails.
let commit_hash = match wt.repo().short_sha(&outcome.sha) {
Ok(hash) => hash,
Err(error) => return Err(crate::output::CommandFailure::with_result(error, outcome)),
};
eprintln!(
"{}",
success_message(cformat!("Committed changes @ <dim>{commit_hash}</>"))
);
Ok(outcome)
}
}
impl CommitOptions<'_> {
/// Commit uncommitted changes with the shared commit pipeline.
///
/// Post-commit pipelines are registered onto the caller's announcer; the
/// caller decides when to flush. Multi-phase callers (e.g. `wt merge
/// --squash` batching post-commit + post-remove + post-switch + post-merge)
/// share one announce line; standalone callers (e.g. `wt commit`)
/// construct an announcer of their own and flush right after.
pub fn commit(
self,
announcer: &mut HookAnnouncer<'_>,
) -> crate::output::CommandResult<CommitOutcome> {
// Use the worktree path from context — this is the target worktree when
// --branch is specified, or the current worktree otherwise.
let wt = self.ctx.repo.worktree_at(self.ctx.worktree_path);
// Refuse before hooks, staging, or any announcement: auto-staging an
// unmerged path is what would put conflict markers in the commit, and
// nothing below is worth doing for a commit that cannot happen.
wt.ensure_no_unmerged_paths("commit")?;
let project_config = self.ctx.repo.load_project_config()?;
let user_hooks = self.ctx.config.hooks(self.ctx.project_id().as_ref());
let any_hooks_exist = user_hooks.get(HookType::PreCommit).is_some()
|| project_config
.as_ref()
.is_some_and(|c| c.hooks.get(HookType::PreCommit).is_some());
// Only print "Skipping pre-commit hooks (--no-hooks)" when --no-hooks was actually
// passed. If hooks are disabled because the caller declined an approval prompt
// (HookGate::Silent), the caller has already printed its own message.
if self.hooks == HookGate::NoHooksFlag && any_hooks_exist {
eprintln!("{}", info_message("Skipping pre-commit hooks (--no-hooks)"));
}
let template_vars = self
.target_branch
.map_or_else(TemplateVars::new, |t| TemplateVars::new().with_target(t));
// Stage on both sides of the pre-commit hooks (user first, then
// project); `auto_stage` says why. Each stage re-checks for unmerged
// paths, since a hook is free to leave them behind.
auto_stage(&wt, self.stage_mode)?;
if self.hooks.run()
&& execute_hook(
self.ctx,
HookType::PreCommit,
&template_vars.as_extra_vars(),
FailureStrategy::FailFast,
)?
{
auto_stage(&wt, self.stage_mode)?;
}
let effective_config = self.ctx.commit_generation();
// Skip the approval gate when the LLM isn't configured — the fallback
// message generator doesn't render the prompt template, so guidance
// would never reach an LLM anyway.
let project_append = match self.guidance {
super::step::PreApprovedGuidance::Resolved(value) => value,
super::step::PreApprovedGuidance::RunOwnGate if effective_config.is_configured() => {
super::command_approval::approve_commit_template_append(self.ctx)?
}
super::step::PreApprovedGuidance::RunOwnGate => None,
};
let outcome = CommitGenerator::new(&effective_config, project_append.as_deref())
.commit_staged_changes(
&wt,
true, // show_progress
self.show_no_squash_note,
self.stage_mode,
)?;
// Register post-commit hooks onto the caller's announcer (respects --no-hooks).
if self.hooks.run() {
let extra_vars = template_vars.as_extra_vars();
if let Err(error) =
announcer.register(self.ctx, HookType::PostCommit, &extra_vars, None)
{
return Err(crate::output::CommandFailure::with_result(error, outcome));
}
}
Ok(outcome)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_message_for_display() {
use insta::assert_snapshot;
let config = CommitGenerationConfig::default();
let generator = CommitGenerator::new(&config, None);
assert_snapshot!(generator.format_message_for_display("Simple commit message"), @"[1mSimple commit message[22m");
assert_snapshot!(generator.format_message_for_display("First line\nSecond line\nThird line"), @"
[1mFirst line[22m
Second line
Third line
");
}
#[test]
fn test_format_message_for_display_empty() {
let config = CommitGenerationConfig::default();
let generator = CommitGenerator::new(&config, None);
let result = generator.format_message_for_display("");
assert_eq!(result, "");
}
}