Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Add a commit-generation command accessor and drop checked unwraps (#4558)
Maximilian Roos5c82218f2878
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rsindex c242dbacf..e34a6ae81 100644--- a/src/commands/config/show.rs+++ b/src/commands/config/show.rs@@ -673,14 +673,7 @@ fn render_diagnostics(out: &mut String, repo: Option<&Repository>) -> anyhow::Relet project_id = repo.and_then(|repo| repo.project_identifier().ok());let commit_config = config.commit_generation(project_id.as_ref());- if !commit_config.is_configured() {- writeln!(out, "{}", info_message("Commit generation not configured"))?;- } else {- // `is_configured()` guarantees `command` is `Some` and non-empty here;- // `unwrap_or_default()` avoids a panic-prone `unwrap()` in this- // `Result`-returning function (the default is unreachable).- let command_display = commit_config.command.clone().unwrap_or_default();-+ if let Some(command_display) = commit_config.command() {match test_commit_generation(&commit_config) {Ok(message) => {writeln!(@@ -706,6 +699,8 @@ fn render_diagnostics(out: &mut String, repo: Option<&Repository>) -> anyhow::Rewriteln!(out, "{}", format_with_gutter(&body, None))?;}}+ } else {+ writeln!(out, "{}", info_message("Commit generation not configured"))?;}Ok(())diff --git a/src/commands/step/shared.rs b/src/commands/step/shared.rsindex ff0c19867..39807a934 100644--- a/src/commands/step/shared.rs+++ b/src/commands/step/shared.rs@@ -29,11 +29,7 @@ pub(super) fn print_dry_run(commit_config: &worktrunk::config::CommitGenerationConfig,message: &str,) -> anyhow::Result<()> {- let command_block = match commit_config- .command- .as_deref()- .filter(|s| !s.trim().is_empty())- {+ let command_block = match commit_config.command() {Some(cmd) => format_bash_with_gutter(&crate::llm::render_llm_invocation(cmd)?),None => format_with_gutter("(LLM not configured — using built-in fallback)", None),};diff --git a/src/config/user/sections.rs b/src/config/user/sections.rsindex 107653850..ad8a1ba3f 100644--- a/src/config/user/sections.rs+++ b/src/config/user/sections.rs@@ -88,10 +88,12 @@ pub struct CommitGenerationConfig {impl CommitGenerationConfig {/// Returns true if an LLM command is configuredpub fn is_configured(&self) -> bool {- self.command- .as_ref()- .map(|s| !s.trim().is_empty())- .unwrap_or(false)+ self.command().is_some()+ }++ /// The configured LLM command, excluding empty or whitespace-only values.+ pub fn command(&self) -> Option<&str> {+ self.command.as_deref().filter(|s| !s.trim().is_empty())}}diff --git a/src/llm.rs b/src/llm.rsindex 84b5c5c83..bfebd0867 100644--- a/src/llm.rs+++ b/src/llm.rs@@ -713,8 +713,7 @@ pub(crate) fn generate_commit_message(project_append: Option<&str>,) -> anyhow::Result<String> {// Check if commit generation is configured (non-empty command)- if commit_generation_config.is_configured() {- let command = commit_generation_config.command.as_ref().unwrap();+ if let Some(command) = commit_generation_config.command() {// Prompt-build failures (git plumbing) propagate as-is; only a// failure of the LLM command itself gets the `LlmCommandFailed`// wrapper — mirroring `SquashInputs::generate_message`.@@ -842,8 +841,7 @@ impl SquashInputs<'_> {/// The squash commit's message: generated by the configured LLM command, or/// a deterministic list of the folded-in subjects when none is configured.pub(crate) fn generate_message(&self) -> anyhow::Result<String> {- if self.config.is_configured() {- let command = self.config.command.as_ref().unwrap();+ if let Some(command) = self.config.command() {let prompt = self.prompt()?;return execute_generation_command(@@ -932,13 +930,11 @@ const SYNTHETIC_DIFF_STAT: &str = " src/main.rs | 4 ++++pub(crate) fn test_commit_generation(commit_generation_config: &CommitGenerationConfig,) -> anyhow::Result<String> {- if !commit_generation_config.is_configured() {+ let Some(command) = commit_generation_config.command() else {anyhow::bail!("Commit generation is not configured. Add [commit.generation] to the config.");- }-- let command = commit_generation_config.command.as_ref().unwrap();+ };// Build prompt with synthetic datalet recent_commits = vec![