Skip to content

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

refactor: combine commit progress messages and eliminate duplication

Maximilian Roos92fc46cc9fe6
diff --git a/src/commands/dev.rs b/src/commands/dev.rs
index d8e029e9d..99f5fa7a0 100644
--- a/src/commands/dev.rs
+++ b/src/commands/dev.rs
@@ -6,7 +6,7 @@ use worktrunk::styling::{
};
use super::merge::{
- commit_with_generated_message, execute_post_merge_commands, format_commit_message_for_display,
+ commit_staged_changes, execute_post_merge_commands, format_commit_message_for_display,
run_pre_commit_commands, run_pre_merge_commands, show_llm_config_hint_if_needed,
};
use super::worktree::{
@@ -146,7 +146,7 @@ pub fn handle_dev_commit(force: bool, no_verify: bool) -> Result<(), GitError> {
repo.run_command(&["add", "-A"])
.git_context("Failed to stage changes")?;
- commit_with_generated_message("Committing changes...", &config.commit_generation)
+ commit_staged_changes(&config.commit_generation)
}
/// Handle `wt dev squash` command
@@ -201,7 +201,7 @@ pub fn handle_dev_squash(
if commit_count == 0 && has_staged {
// Just staged changes, no commits - commit them directly (no squashing needed)
- commit_with_generated_message("Committing changes...", &config.commit_generation)?;
+ commit_staged_changes(&config.commit_generation)?;
return Ok(false);
}
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index c38429f66..d42107201 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -261,14 +261,7 @@ pub fn format_commit_message_for_display(message: &str) -> String {
pub fn show_llm_config_hint_if_needed(
commit_generation_config: &worktrunk::config::CommitGenerationConfig,
) -> Result<(), GitError> {
- // Check if LLM is NOT configured (matching llm.rs logic)
- let is_configured = commit_generation_config
- .command
- .as_ref()
- .map(|s| !s.trim().is_empty())
- .unwrap_or(false);
-
- if !is_configured {
+ if !commit_generation_config.is_configured() {
crate::output::hint(format!(
"{HINT_EMOJI} {HINT}Using fallback commit message. Run 'wt config help' to configure LLM-generated messages{HINT:#}"
))?;
@@ -276,9 +269,8 @@ pub fn show_llm_config_hint_if_needed(
Ok(())
}
-/// Commit already-staged changes with an LLM-generated message
-pub fn commit_with_generated_message(
- progress_msg: &str,
+/// Commit already-staged changes with LLM-generated or fallback message
+pub fn commit_staged_changes(
commit_generation_config: &worktrunk::config::CommitGenerationConfig,
) -> Result<(), GitError> {
let repo = Repository::current();
@@ -290,19 +282,19 @@ pub fn commit_with_generated_message(
let stats = parse_diff_shortstat(&diff_shortstat);
let stats_parts = stats.format_summary();
- // Format progress message with stats
- // Don't nest styles - stats already contain ADDITION/DELETION colors
+ // Format progress message based on whether we're using LLM or fallback
+ let action = if commit_generation_config.is_configured() {
+ "Generating commit message and committing..."
+ } else {
+ "Committing with default message..."
+ };
+
let full_progress_msg = match stats_parts.is_empty() {
- true => format!("πŸ”„ {CYAN}{progress_msg}{CYAN:#}"),
- false => format!(
- "πŸ”„ {CYAN}{}{CYAN:#} ({})",
- progress_msg,
- stats_parts.join(", ")
- ),
+ true => format!("πŸ”„ {CYAN}{action}{CYAN:#}"),
+ false => format!("πŸ”„ {CYAN}{action}{CYAN:#} ({})", stats_parts.join(", ")),
};
crate::output::progress(full_progress_msg)?;
- crate::output::progress(format!("πŸ”„ {CYAN}Generating commit message...{CYAN:#}"))?;
show_llm_config_hint_if_needed(commit_generation_config)?;
let commit_message = crate::llm::generate_commit_message(commit_generation_config)?;
@@ -369,7 +361,7 @@ fn handle_commit_changes(
.git_context("Failed to stage changes")?;
}
- commit_with_generated_message("Committing changes...", commit_generation_config)
+ commit_staged_changes(commit_generation_config)
}
fn handle_squash(target_branch: &str, no_verify: bool, force: bool) -> Result<bool, GitError> {
diff --git a/src/config.rs b/src/config.rs
index 0455ae1b8..5c35d121c 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -123,6 +123,16 @@ pub struct CommitGenerationConfig {
pub squash_template_file: Option<String>,
}
+impl CommitGenerationConfig {
+ /// Returns true if an LLM command is configured
+ pub fn is_configured(&self) -> bool {
+ self.command
+ .as_ref()
+ .map(|s| !s.trim().is_empty())
+ .unwrap_or(false)
+ }
+}
+
/// Project-specific configuration with hooks and commands.
///
/// This config is stored at `<repo>/.config/wt.toml` within the repository and
diff --git a/src/llm.rs b/src/llm.rs
index 52290d662..102a76b09 100644
--- a/src/llm.rs
+++ b/src/llm.rs
@@ -202,9 +202,8 @@ pub fn generate_commit_message(
commit_generation_config: &CommitGenerationConfig,
) -> Result<String, GitError> {
// Check if commit generation is configured (non-empty command)
- if let Some(ref command) = commit_generation_config.command
- && !command.trim().is_empty()
- {
+ if commit_generation_config.is_configured() {
+ let command = commit_generation_config.command.as_ref().unwrap();
// Commit generation is explicitly configured - fail if it doesn't work
return try_generate_commit_message(
command,
@@ -275,9 +274,8 @@ pub fn generate_squash_message(
commit_generation_config: &CommitGenerationConfig,
) -> Result<String, Box<dyn std::error::Error>> {
// Check if commit generation is configured (non-empty command)
- if let Some(ref command) = commit_generation_config.command
- && !command.trim().is_empty()
- {
+ if commit_generation_config.is_configured() {
+ let command = commit_generation_config.command.as_ref().unwrap();
// Commit generation is explicitly configured - fail if it doesn't work
let prompt = build_squash_prompt(
commit_generation_config,
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
index 37053b44a..bba880917 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
@@ -16,14 +16,13 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpvBvXoe/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp93y4Df/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
🟑 Auto-staging untracked files: dirty.txt
-πŸ”„ Committing changes... (1 file, +1)
-πŸ”„ Generating commit message...
+πŸ”„ Committing with default message... (1 file, +1)
πŸ’‘ Using fallback commit message. Run 'wt config help' to configure LLM-generated messages
  WIP: Auto-commit before merge
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
index 81be936d6..20cd7a4d5 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
@@ -17,7 +17,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpmY1rlZ/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpQpTGwv/test-config.toml
---
success: true
exit_code: 0
@@ -26,8 +26,7 @@ exit_code: 0
πŸ”„ Running pre-commit:
  echo 'Pre-commit check passed'
-πŸ”„ Committing changes... (1 file, +1)
-πŸ”„ Generating commit message...
+πŸ”„ Committing with default message... (1 file, +1)
πŸ’‘ Using fallback commit message. Run 'wt config help' to configure LLM-generated messages
  WIP: Auto-commit before merge