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.tomlindex 103bde892..7e4af67f1 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -45,7 +45,7 @@ askama = { version = "0.14", default-features = false, features = ["derive", "stchrono = "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.rsindex 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.rsindex 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 errorsnapshot_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.snapnew file mode 100644index 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 -----+🔄 [36mSquashing 2 commits into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m+🔄 [36mGenerating squash commit message...[39m+❌ [31mCommit generation command failed[39m+[107m [0m Failed to spawn LLM command++⚪ Ran command:+[107m [0m nonexistent-llm-commanddiff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snapdeleted file mode 100644index 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 ------🔄 [36mSquashing 2 commits into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m-🔄 [36mGenerating squash commit message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mSquash commits from main[22m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: new feature-[107m [0m - fix: bug fix-✅ [32mSquashed @ [2m091f0c5[22m[39m-🔄 [36mMerging 1 commit to [1mmain[22m @ [2m091f0c5[22m (no rebase needed)[39m-[107m [0m * [33m091f0c5[m Squash commits from main-[107m [0m file1.txt | 1 [32m+[m-[107m [0m file2.txt | 1 [32m+[m-[107m [0m 2 files changed, 2 insertions(+)-✅ [32mMerged to [1mmain[22m [90m(1 commit, 2 files, [32m+2[39m[39m[90m)[39m[39m-🔄 [36m[36mRemoving [1mfeature[22m worktree & branch in background (already in main)[39m[39mdiff --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.snapindex 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🔄 [36mSquashing 2 commits & working tree changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m💡 [2mBackup created @ 330ccd6[22m🔄 [36mGenerating squash commit message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mSquash commits from main[22m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add file 1-[107m [0m - feat: add file 2-✅ [32mSquashed @ [2md34e4c3[22m[39m-🔄 [36mMerging 1 commit to [1mmain[22m @ [2md34e4c3[22m (no rebase needed)[39m-[107m [0m * [33md34e4c3[m Squash commits from main+[107m [0m [1mfix: update files[22m+✅ [32mSquashed @ [2m49dd855[22m[39m+🔄 [36mMerging 1 commit to [1mmain[22m @ [2m49dd855[22m (no rebase needed)[39m+[107m [0m * [33m49dd855[m fix: update files[107m [0m file1.txt | 1 [32m+[m[107m [0m file2.txt | 1 [32m+[m[107m [0m 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.snapindex 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🔄 [36mSquashing 1 commit & working tree changes into a single commit [90m(3 files, [32m+3[39m[39m[90m)[39m...[39m💡 [2mBackup created @ 4967998[22m🔄 [36mGenerating squash commit message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mSquash commits from main[22m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add file 1-✅ [32mSquashed @ [2mfbfc726[22m[39m-🔄 [36mMerging 1 commit to [1mmain[22m @ [2mfbfc726[22m (no rebase needed)[39m-[107m [0m * [33mfbfc726[m Squash commits from main+[107m [0m [1mfix: commit changes[22m+✅ [32mSquashed @ [2mffd7e7b[22m[39m+🔄 [36mMerging 1 commit to [1mmain[22m @ [2mffd7e7b[22m (no rebase needed)[39m+[107m [0m * [33mffd7e7b[m fix: commit changes[107m [0m file1.txt | 1 [32m+[m[107m [0m untracked1.txt | 1 [32m+[m[107m [0m untracked2.txt | 1 [32m+[mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_both_flags.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_both_flags.snapindex 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 -----💡 [2mSkipping pre-commit hook ([90m--no-verify[39m)[22m-🔄 [36mCommitting tracked changes with default message... [90m(1 file, [32m+1[39m, [31m-1[39m[39m[90m)[39m[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mChanges to tracked.txt[22m-✅ [32mCommitted changes @ [2ma5890cb[22m[39m+🔄 [36mGenerating commit message and committing tracked changes... [90m(1 file, [32m+1[39m, [31m-1[39m[39m[90m)[39m[39m+[107m [0m [1mfix: update file[22m+✅ [32mCommitted changes @ [2m0b66aac[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_no_verify.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_no_verify.snapindex 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🟡 [33mAuto-staging 2 untracked paths:[39m[107m [0m .config/[107m [0m file1.txt-🔄 [36mCommitting changes with default message... [90m(2 files, [32m+2[39m[39m[90m)[39m[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mChanges to wt.toml & file1.txt[22m-✅ [32mCommitted changes @ [2m3dbcd5d[22m[39m+🔄 [36mGenerating commit message and committing changes... [90m(2 files, [32m+2[39m[39m[90m)[39m[39m+[107m [0m [1mfeat: add file[22m+✅ [32mCommitted changes @ [2m83bd654[22m[39mdiff --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.snapindex 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 ------🔄 [36mCommitting changes with default message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mWIP: Changes[22m+🔄 [36mGenerating commit message and committing changes...[39m+[107m [0m [1mfeat: this should fail[22m❌ [31mFailed to commit[39m[107m [0m On branch main[107m [0m nothing to commit, working tree cleandiff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_stage_tracked.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_stage_tracked.snapindex 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 ------🔄 [36mCommitting tracked changes with default message... [90m(1 file, [32m+1[39m, [31m-1[39m[39m[90m)[39m[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mChanges to tracked.txt[22m-✅ [32mCommitted changes @ [2m7d441f0[22m[39m+🔄 [36mGenerating commit message and committing tracked changes... [90m(1 file, [32m+1[39m, [31m-1[39m[39m[90m)[39m[39m+[107m [0m [1mfix: update tracked file[22m+✅ [32mCommitted changes @ [2m979ff99[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_both_flags.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_both_flags.snapindex 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🔄 [36mSquashing 2 commits & tracked changes into a single commit [90m(3 files, [32m+3[39m[39m[90m)[39m...[39m💡 [2mBackup created @ 31e06e8[22m🔄 [36mGenerating squash commit message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mSquash commits from main[22m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add file 1-[107m [0m - feat: add file 2-✅ [32mSquashed @ [2m9f7a3e2[22m[39m+[107m [0m [1msquash: combined commits[22m+✅ [32mSquashed @ [2mf5ad358[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_no_verify.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_no_verify.snapindex 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💡 [2mSkipping pre-commit hook ([90m--no-verify[39m)[22m🔄 [36mSquashing 2 commits into a single commit [90m(3 files, [32m+3[39m[39m[90m)[39m...[39m🔄 [36mGenerating squash commit message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mSquash commits from main[22m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add file 1-[107m [0m - feat: add file 2-✅ [32mSquashed @ [2m588aa53[22m[39m+[107m [0m [1msquash: combined commits[22m+✅ [32mSquashed @ [2mdb8c691[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_stage_tracked.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_stage_tracked.snapindex 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🔄 [36mSquashing 2 commits & tracked changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m💡 [2mBackup created @ ba1424b[22m🔄 [36mGenerating squash commit message...[39m-💡 [2mUsing fallback commit message. Run [90mwt config --help[39m for LLM setup guide[22m-[107m [0m [1mSquash commits from main[22m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add file 1-[107m [0m - feat: add file 2-✅ [32mSquashed @ [2m6c0b38b[22m[39m+[107m [0m [1msquash: combined commits[22m+✅ [32mSquashed @ [2m072b879[22m[39m