1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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::warn_about_untracked_files;
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.
pub struct CommitOutcome {
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 warn_about_untracked: bool,
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> {
/// Convenience constructor for the common case where untracked files should trigger a warning.
pub fn new(ctx: &'a CommandContext<'a>) -> Self {
Self {
ctx,
target_branch: None,
hooks: HookGate::Run,
stage_mode: StageMode::All,
warn_about_untracked: true,
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,
) -> anyhow::Result<CommitOutcome> {
// Fail early if nothing is staged (avoids confusing LLM prompt with empty diff)
if !wt.has_staged_changes()? {
anyhow::bail!("Nothing to commit");
}
if show_progress {
let stats_parts = wt
.repo()
.diff_stats_summary(&["diff", "--staged", "--shortstat"]);
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();
let commit_message =
crate::llm::generate_commit_message(self.config, 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();
// Display uses `Repository::short_sha`; the JSON payload carries the full SHA.
let commit_hash = wt.repo().short_sha(&commit_sha)?;
eprintln!(
"{}",
success_message(cformat!("Committed changes @ <dim>{commit_hash}</>"))
);
Ok(CommitOutcome {
sha: commit_sha,
message: commit_message,
stage_mode,
})
}
}
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<'_>) -> anyhow::Result<CommitOutcome> {
let project_config = self.ctx.repo.load_project_config()?;
let user_hooks = self.ctx.config.hooks(self.ctx.project_id().as_deref());
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));
if self.hooks.run() {
// Run pre-commit hooks (user first, then project).
execute_hook(
self.ctx,
HookType::PreCommit,
&template_vars.as_extra_vars(),
FailureStrategy::FailFast,
)?;
}
// 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);
if self.warn_about_untracked && self.stage_mode == StageMode::All {
let status = wt
.run_command(&["status", "--porcelain", "-z"])
.context("Failed to get status")?;
warn_about_untracked_files(&status)?;
}
// Stage changes based on mode
match self.stage_mode {
StageMode::All => {
// Stage everything: tracked modifications + untracked files
wt.run_command(&["add", "-A"])
.context("Failed to stage changes")?;
}
StageMode::Tracked => {
// Stage tracked modifications only (no untracked files)
wt.run_command(&["add", "-u"])
.context("Failed to stage tracked changes")?;
}
StageMode::None => {
// Stage nothing - commit only what's already in the index
}
}
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();
announcer.register(self.ctx, HookType::PostCommit, &extra_vars, None)?;
}
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, "");
}
}