Skip to content

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

Enable environment variable config with proper case conversion

Maximilian Roos48ba46a25a7c
diff --git a/Cargo.toml b/Cargo.toml
index 103bde892..7e4af67f1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -45,7 +45,7 @@ askama = { version = "0.14", default-features = false, features = ["derive", "st
chrono = "0.4"
clap = { version = "4.5", features = ["derive", "unstable-ext"] }
clap_complete = { version = "4.5", features = ["unstable-dynamic"] }
-config = "0.15"
+config = { version = "0.15", features = ["convert-case"] }
crossbeam-channel = "0.5"
crossterm = "0.29"
env_logger = "0.11"
diff --git a/src/config/user.rs b/src/config/user.rs
index 1b0d467c4..1e5ba4cd8 100644
--- a/src/config/user.rs
+++ b/src/config/user.rs
@@ -2,11 +2,50 @@
//!
//! Personal preferences and per-project approved commands, not checked into git.
-use config::{Config, ConfigError, File};
-use serde::{Deserialize, Serialize};
+use config::{Case, Config, ConfigError, File};
+use serde::{Deserialize, Deserializer, Serialize};
use std::path::PathBuf;
use std::sync::OnceLock;
+/// Deserialize a Vec<String> that can also accept a single String
+/// This enables setting array config fields via environment variables
+fn deserialize_string_or_vec<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ use serde::de;
+
+ struct StringOrVec;
+
+ impl<'de> de::Visitor<'de> for StringOrVec {
+ type Value = Vec<String>;
+
+ fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+ formatter.write_str("string or array of strings")
+ }
+
+ fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(vec![value.to_string()])
+ }
+
+ fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
+ where
+ A: de::SeqAccess<'de>,
+ {
+ let mut vec = Vec::new();
+ while let Some(elem) = seq.next_element()? {
+ vec.push(elem);
+ }
+ Ok(vec)
+ }
+ }
+
+ deserializer.deserialize_any(StringOrVec)
+}
+
#[cfg(not(test))]
use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};
@@ -120,7 +159,8 @@ pub struct CommitGenerationConfig {
pub command: Option<String>,
/// Arguments to pass to the command
- #[serde(default)]
+ /// Accepts either an array or a single string (for env var compatibility)
+ #[serde(default, deserialize_with = "deserialize_string_or_vec")]
pub args: Vec<String>,
/// Inline template for commit message prompt
@@ -275,12 +315,16 @@ impl WorktrunkConfig {
}
// Add environment variables with WORKTRUNK prefix
- // Uses "__" separator (default) to support field names with underscores
- // TODO: This doesn't work for nested fields due to serde rename mismatch.
- // The config crate maps WORKTRUNK_COMMIT_GENERATION__COMMAND to `commit_generation.command`
- // (snake_case), but serde expects `commit-generation.command` (kebab-case).
- // Only WORKTRUNK_CONFIG_PATH works reliably (handled separately in get_config_path).
- builder = builder.add_source(config::Environment::with_prefix("WORKTRUNK").separator("__"));
+ // - prefix_separator("_"): strip prefix with single underscore (WORKTRUNK_ → key)
+ // - separator("__"): double underscore for nested fields (COMMIT_GENERATION__COMMAND → commit-generation.command)
+ // - convert_case(Kebab): converts snake_case to kebab-case to match serde field names
+ // Example: WORKTRUNK_WORKTREE_PATH → worktree-path
+ builder = builder.add_source(
+ config::Environment::with_prefix("WORKTRUNK")
+ .prefix_separator("_")
+ .separator("__")
+ .convert_case(Case::Kebab),
+ );
let config: Self = builder.build()?.try_deserialize()?;
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index de4925314..ab2f6f8f3 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -599,7 +599,7 @@ args = ["-c", "cat >/dev/null && echo 'feat: implement user authentication syste
}
#[test]
-fn test_merge_squash_llm_fallback() {
+fn test_merge_squash_llm_command_not_found() {
let mut repo = TestRepo::new();
repo.commit("Initial commit");
repo.setup_remote("main");
@@ -644,11 +644,9 @@ fn test_merge_squash_llm_fallback() {
.output()
.unwrap();
- // Configure LLM command that will fail (non-existent command)
- // Should now error instead of falling back
- // (squashing is now the default, no need for --squash flag)
+ // Configure LLM command that doesn't exist - should error
snapshot_merge_with_env(
- "merge_squash_llm_fallback",
+ "merge_squash_llm_command_not_found",
&repo,
&["main"],
Some(&feature_wt),
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_command_not_found.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_command_not_found.snap
new file mode 100644
index 000000000..5c9231ed7
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_command_not_found.snap
@@ -0,0 +1,33 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1735776000"
+ WORKTRUNK_COMMIT_GENERATION__COMMAND: nonexistent-llm-command
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+---
+success: false
+exit_code: 1
+----- stdout -----
+
+----- stderr -----
+🔄 Squashing 2 commits into a single commit (2 files, +2)...
+🔄 Generating squash commit message...
+❌ Commit generation command failed
+  Failed to spawn LLM command
+
+⚪ Ran command:
+  nonexistent-llm-command
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap
deleted file mode 100644
index 3b3fce376..000000000
--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap
+++ /dev/null
@@ -1,42 +0,0 @@
----
-source: tests/integration_tests/merge.rs
-info:
- program: wt
- args:
- - merge
- - main
- env:
- CLICOLOR_FORCE: "1"
- COLUMNS: "150"
- GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
- GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
- GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
- GIT_CONFIG_SYSTEM: /dev/null
- GIT_EDITOR: ""
- LANG: C
- LC_ALL: C
- SOURCE_DATE_EPOCH: "1735776000"
- WORKTRUNK_COMMIT_GENERATION__COMMAND: nonexistent-llm-command
- WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
----
-success: true
-exit_code: 0
------ stdout -----
-
------ stderr -----
-🔄 Squashing 2 commits into a single commit (2 files, +2)...
-🔄 Generating squash commit message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: new feature
-  - fix: bug fix
-✅ Squashed @ 091f0c5
-🔄 Merging 1 commit to main @ 091f0c5 (no rebase needed)
-  * 091f0c5 Squash commits from main
-  file1.txt | 1 +
-  file2.txt | 1 +
-  2 files changed, 2 insertions(+)
-✅ Merged to main (1 commit, 2 files, +2)
-🔄 Removing feature worktree & branch in background (already in main)
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap
index 6e61e0382..311119aa7 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap
@@ -28,15 +28,10 @@ exit_code: 0
🔄 Squashing 2 commits & working tree changes into a single commit (2 files, +2)...
💡 Backup created @ 330ccd6
🔄 Generating squash commit message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add file 1
-  - feat: add file 2
-✅ Squashed @ d34e4c3
-🔄 Merging 1 commit to main @ d34e4c3 (no rebase needed)
-  * d34e4c3 Squash commits from main
+  fix: update files
+✅ Squashed @ 49dd855
+🔄 Merging 1 commit to main @ 49dd855 (no rebase needed)
+  * 49dd855 fix: update files
  file1.txt | 1 +
  file2.txt | 1 +
  2 files changed, 2 insertions(+)
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap
index eea667bc8..7f7fe4804 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap
@@ -31,14 +31,10 @@ exit_code: 0
🔄 Squashing 1 commit & working tree changes into a single commit (3 files, +3)...
💡 Backup created @ 4967998
🔄 Generating squash commit message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add file 1
-✅ Squashed @ fbfc726
-🔄 Merging 1 commit to main @ fbfc726 (no rebase needed)
-  * fbfc726 Squash commits from main
+  fix: commit changes
+✅ Squashed @ ffd7e7b
+🔄 Merging 1 commit to main @ ffd7e7b (no rebase needed)
+  * ffd7e7b fix: commit changes
  file1.txt | 1 +
  untracked1.txt | 1 +
  untracked2.txt | 1 +
diff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_both_flags.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_both_flags.snap
index d55dda100..3a6973cae 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_commit_both_flags.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_commit_both_flags.snap
@@ -28,7 +28,6 @@ exit_code: 0
----- stderr -----
💡 Skipping pre-commit hook (--no-verify)
-🔄 Committing tracked changes with default message... (1 file, +1, -1)
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Changes to tracked.txt
-✅ Committed changes @ a5890cb
+🔄 Generating commit message and committing tracked changes... (1 file, +1, -1)
+  fix: update file
+✅ Committed changes @ 0b66aac
diff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_no_verify.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_no_verify.snap
index 8830c7258..dcd77c242 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_commit_no_verify.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_commit_no_verify.snap
@@ -30,7 +30,6 @@ exit_code: 0
🟡 Auto-staging 2 untracked paths:
  .config/
  file1.txt
-🔄 Committing changes with default message... (2 files, +2)
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Changes to wt.toml & file1.txt
-✅ Committed changes @ 3dbcd5d
+🔄 Generating commit message and committing changes... (2 files, +2)
+  feat: add file
+✅ Committed changes @ 83bd654
diff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap
index f3dfced21..a4fc641e1 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap
@@ -26,9 +26,8 @@ exit_code: 1
----- stdout -----
----- stderr -----
-🔄 Committing changes with default message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  WIP: Changes
+🔄 Generating commit message and committing changes...
+  feat: this should fail
❌ Failed to commit
  On branch main
  nothing to commit, working tree clean
diff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_stage_tracked.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_stage_tracked.snap
index bf1ad0f8e..8907c595e 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_commit_stage_tracked.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_commit_stage_tracked.snap
@@ -26,7 +26,6 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🔄 Committing tracked changes with default message... (1 file, +1, -1)
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Changes to tracked.txt
-✅ Committed changes @ 7d441f0
+🔄 Generating commit message and committing tracked changes... (1 file, +1, -1)
+  fix: update tracked file
+✅ Committed changes @ 979ff99
diff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_both_flags.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_both_flags.snap
index cb3078f50..dead9b8a1 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_squash_both_flags.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_both_flags.snap
@@ -31,10 +31,5 @@ exit_code: 0
🔄 Squashing 2 commits & tracked changes into a single commit (3 files, +3)...
💡 Backup created @ 31e06e8
🔄 Generating squash commit message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add file 1
-  - feat: add file 2
-✅ Squashed @ 9f7a3e2
+  squash: combined commits
+✅ Squashed @ f5ad358
diff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_no_verify.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_no_verify.snap
index 37a88194f..9f5dc5a6f 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_squash_no_verify.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_no_verify.snap
@@ -29,10 +29,5 @@ exit_code: 0
💡 Skipping pre-commit hook (--no-verify)
🔄 Squashing 2 commits into a single commit (3 files, +3)...
🔄 Generating squash commit message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add file 1
-  - feat: add file 2
-✅ Squashed @ 588aa53
+  squash: combined commits
+✅ Squashed @ db8c691
diff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_stage_tracked.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_stage_tracked.snap
index 1209c4457..1db40856b 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_squash_stage_tracked.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_stage_tracked.snap
@@ -29,10 +29,5 @@ exit_code: 0
🔄 Squashing 2 commits & tracked changes into a single commit (2 files, +2)...
💡 Backup created @ ba1424b
🔄 Generating squash commit message...
-💡 Using fallback commit message. Run wt config --help for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add file 1
-  - feat: add file 2
-✅ Squashed @ 6c0b38b
+  squash: combined commits
+✅ Squashed @ 072b879