Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Support template files for commit messages
Maximilian Roosfbb16cf065d7
diff --git a/dev/config.example.toml b/dev/config.example.tomlindex bbae004f0..540cce9a9 100644--- a/dev/config.example.toml+++ b/dev/config.example.toml@@ -108,9 +108,15 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"# Available variables: {{ commits }}, {{ target_branch }}, {{ branch }}, {{ repo }}# If not specified, uses the default template:# squash-template = """-# Generate a conventional commit message (feat/fix/docs/style/refactor) that combines these changes into one cohesive message. Output only the commit message without any explanation.+# Generate a commit message that combines these changes into one cohesive message. Output only the commit message without any explanation.#-# Squashing commits on current branch since branching from {{ target_branch }}+# Format+# - First line: <50 chars, present tense, describes WHAT and WHY (not HOW).+# - Blank line after first line.+# - Optional details with proper line breaks explaining context.+# - Match the style of the existing commits being squashed (e.g., if they use conventional commits, use conventional commits).+#+# Squashing commits from {{ branch }} to merge into {{ target_branch }}## Commits being combined:# {% for commit in commits %}diff --git a/src/llm.rs b/src/llm.rsindex af8a1f63e..9304808b7 100644--- a/src/llm.rs+++ b/src/llm.rs@@ -56,10 +56,16 @@ The following is the context for your task:/// Default template for squash commit message prompts////// TODO: Generate config.example.toml dynamically to keep templates in sync.-/// Currently this constant must be manually copied to config.example.toml (lines 109-118).-const DEFAULT_SQUASH_TEMPLATE: &str = r#"Generate a conventional commit message (feat/fix/docs/style/refactor) that combines these changes into one cohesive message. Output only the commit message without any explanation.+/// Currently this constant must be manually copied to config.example.toml (lines 110-125).+const DEFAULT_SQUASH_TEMPLATE: &str = r#"Generate a commit message that combines these changes into one cohesive message. Output only the commit message without any explanation.-Squashing commits on current branch since branching from {{ target_branch }}+Format+- First line: <50 chars, present tense, describes WHAT and WHY (not HOW).+- Blank line after first line.+- Optional details with proper line breaks explaining context.+- Match the style of the existing commits being squashed (e.g., if they use conventional commits, use conventional commits).++Squashing commits from {{ branch }} to merge into {{ target_branch }}Commits being combined:{% for commit in commits %}@@ -720,4 +726,113 @@ Single commit: {{ commits[0] }}assert!(prompt.contains("Single commit: solo commit"));assert!(!prompt.contains("Multiple commits detected"));}++ #[test]+ fn test_build_commit_prompt_with_template_file() {+ let temp_dir = std::env::temp_dir();+ let template_path = temp_dir.join("test_commit_template.txt");+ std::fs::write(+ &template_path,+ "Branch: {{ branch }}\nRepo: {{ repo }}\nDiff: {{ git_diff }}",+ )+ .unwrap();++ let config = CommitGenerationConfig {+ command: None,+ args: vec![],+ template: None,+ template_file: Some(template_path.to_string_lossy().to_string()),+ squash_template: None,+ squash_template_file: None,+ };+ let context = PromptContext {+ branch: "feature",+ repo_name: "myrepo",+ };+ let result = build_commit_prompt(&config, "my diff", None, &context);+ assert!(result.is_ok());+ assert_eq!(+ result.unwrap(),+ "Branch: feature\nRepo: myrepo\nDiff: my diff"+ );++ // Cleanup+ std::fs::remove_file(&template_path).ok();+ }++ #[test]+ fn test_build_commit_prompt_with_missing_template_file() {+ let config = CommitGenerationConfig {+ command: None,+ args: vec![],+ template: None,+ template_file: Some("/nonexistent/path/template.txt".to_string()),+ squash_template: None,+ squash_template_file: None,+ };+ let context = PromptContext {+ branch: "main",+ repo_name: "repo",+ };+ let result = build_commit_prompt(&config, "diff", None, &context);+ assert!(result.is_err());+ assert!(result.unwrap_err().to_string().contains("Failed to read"));+ }++ #[test]+ fn test_build_squash_prompt_with_template_file() {+ let temp_dir = std::env::temp_dir();+ let template_path = temp_dir.join("test_squash_template.txt");+ std::fs::write(+ &template_path,+ "Target: {{ target_branch }}\nBranch: {{ branch }}\n{% for c in commits %}{{ c }}\n{% endfor %}",+ )+ .unwrap();++ let config = CommitGenerationConfig {+ command: None,+ args: vec![],+ template: None,+ template_file: None,+ squash_template: None,+ squash_template_file: Some(template_path.to_string_lossy().to_string()),+ };+ let commits = vec!["A".to_string(), "B".to_string()];+ let context = PromptContext {+ branch: "feature",+ repo_name: "repo",+ };+ let result = build_squash_prompt(&config, "main", &commits, &context);+ assert!(result.is_ok());+ // Commits are reversed for chronological order+ assert_eq!(result.unwrap(), "Target: main\nBranch: feature\nB\nA\n");++ // Cleanup+ std::fs::remove_file(&template_path).ok();+ }++ #[test]+ fn test_build_commit_prompt_with_tilde_expansion() {+ // This test verifies tilde expansion works - it should attempt to read+ // from the expanded home directory path+ let config = CommitGenerationConfig {+ command: None,+ args: vec![],+ template: None,+ template_file: Some("~/nonexistent_template_for_test.txt".to_string()),+ squash_template: None,+ squash_template_file: None,+ };+ let context = PromptContext {+ branch: "main",+ repo_name: "repo",+ };+ let result = build_commit_prompt(&config, "diff", None, &context);+ // Should fail because file doesn't exist+ assert!(result.is_err());+ let err = result.unwrap_err().to_string();+ assert!(err.contains("Failed to read"));+ // Error message may display ~ for readability, but the actual file read+ // should have used the expanded path (verified by the error occurring)+ }}diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 33a1d03d1..42aad47ff 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -505,23 +505,17 @@ fn test_merge_squash_with_llm() {.output().expect("Failed to commit");- // Configure mock LLM command via environment variable+ // Configure mock LLM command via config file// Use sh to consume stdin and return a fixed message+ let worktrunk_config = r#"+[commit-generation]+command = "sh"+args = ["-c", "cat >/dev/null && echo 'feat: implement user authentication system'"]+"#;+ fs::write(repo.test_config_path(), worktrunk_config).expect("Failed to write worktrunk config");+// (squashing is now the default, no need for --squash flag)- snapshot_merge_with_env(- "merge_squash_with_llm",- &repo,- &["main"],- Some(&feature_wt),- &[- ("WORKTRUNK_COMMIT_GENERATION__COMMAND", "sh"),- ("WORKTRUNK_COMMIT_GENERATION__ARGS__0", "-c"),- (- "WORKTRUNK_COMMIT_GENERATION__ARGS__1",- "cat >/dev/null && echo 'feat: implement user authentication system'",- ),- ],- );+ snapshot_merge("merge_squash_with_llm", &repo, &["main"], Some(&feature_wt));}#[test]@@ -799,19 +793,20 @@ fn test_merge_auto_commit_with_llm() {std::fs::write(feature_wt.join("auth.txt"), "improved auth with validation").expect("Failed to write file");+ // Configure mock LLM command via config file+ let worktrunk_config = r#"+[commit-generation]+command = "echo"+args = ["fix: improve auth validation logic"]+"#;+ fs::write(repo.test_config_path(), worktrunk_config).expect("Failed to write worktrunk config");+// Merge with LLM configured - should auto-commit with LLM message- snapshot_merge_with_env(+ snapshot_merge("merge_auto_commit_with_llm",&repo,&["main"],Some(&feature_wt),- &[- ("WORKTRUNK_COMMIT_GENERATION__COMMAND", "echo"),- (- "WORKTRUNK_COMMIT_GENERATION__ARGS",- "fix: improve auth validation logic",- ),- ],);}@@ -823,20 +818,20 @@ fn test_merge_auto_commit_and_squash() {std::fs::write(feature_wt.join("file1.txt"), "updated content 1").expect("Failed to write file");+ // Configure mock LLM command via config file+ let worktrunk_config = r#"+[commit-generation]+command = "echo"+args = ["fix: update file 1 content"]+"#;+ fs::write(repo.test_config_path(), worktrunk_config).expect("Failed to write worktrunk config");+// Merge (squashing is default) - should stage uncommitted changes, then squash all commits including the staged changes- snapshot_merge_with_env(+ snapshot_merge("merge_auto_commit_and_squash",&repo,&["main"],Some(&feature_wt),- &[- ("WORKTRUNK_COMMIT_GENERATION__COMMAND", "echo"),- // Message is for the final squash commit- (- "WORKTRUNK_COMMIT_GENERATION__ARGS",- "fix: update file 1 content",- ),- ],);}diff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snapindex 80a3024ea..18d2b6032 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap@@ -16,8 +16,6 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: update file 1 content"- WORKTRUNK_COMMIT_GENERATION__COMMAND: echoWORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"---success: true@@ -26,15 +24,10 @@ exit_code: 0π [36mSquashing 2 commits & working tree changes into a single commit[0m (2 files, [32m+2[0m)...π‘ Backup created @ [SHA]π [36mGenerating squash commit message...[0m-π‘ [2mUsing fallback commit message. Run 'wt config --help' for LLM setup guide[0m-[107m [0m [1mSquash commits from main[0m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add file 1-[107m [0m - feat: add file 2+[107m [0m [1mfix: update file 1 content[0mβ [32mSquashed @ [2m[SHA][0mπ [36mMerging 1 commit to [1m[36mmain[0m[36m @ [2m[SHA][0m (no rebase needed)-[107m [0m * [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Squash commits from main+[107m [0m * [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m fix: update file 1 content[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_auto_commit_with_llm.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snapindex d483ba4ac..dda8a541d 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snap@@ -16,8 +16,6 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: improve auth validation logic"- WORKTRUNK_COMMIT_GENERATION__COMMAND: echoWORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"---success: true@@ -26,14 +24,10 @@ exit_code: 0π [36mSquashing 1 commit & working tree changes into a single commit[0m (1 file, [32m+1[0m)...π‘ Backup created @ [SHA]π [36mGenerating squash commit message...[0m-π‘ [2mUsing fallback commit message. Run 'wt config --help' for LLM setup guide[0m-[107m [0m [1mSquash commits from main[0m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add authentication+[107m [0m [1mfix: improve auth validation logic[0mβ [32mSquashed @ [2m[SHA][0mπ [36mMerging 1 commit to [1m[36mmain[0m[36m @ [2m[SHA][0m (no rebase needed)-[107m [0m * [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Squash commits from main+[107m [0m * [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m fix: improve auth validation logic[107m [0m auth.txt | 1 [32m+[m[107m [0m 1 file changed, 1 insertion(+)β [32mMerged to [1m[32mmain[0m[0m (1 commit, 1 file, [32m+1[0m)diff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snapindex 7c1a3a6c2..cbeba5529 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap@@ -16,9 +16,6 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_COMMIT_GENERATION__ARGS__0: "-c"- WORKTRUNK_COMMIT_GENERATION__ARGS__1: "cat >/dev/null && echo 'feat: implement user authentication system'"- WORKTRUNK_COMMIT_GENERATION__COMMAND: shWORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"---success: true@@ -26,15 +23,10 @@ exit_code: 0----- stdout -----π [36mSquashing 2 commits into a single commit[0m (1 file, [32m+1[0m)...π [36mGenerating squash commit message...[0m-π‘ [2mUsing fallback commit message. Run 'wt config --help' for LLM setup guide[0m-[107m [0m [1mSquash commits from main[0m-[107m [0m-[107m [0m Combined commits:-[107m [0m - feat: add authentication-[107m [0m - fix: handle edge case+[107m [0m [1mfeat: implement user authentication system[0mβ [32mSquashed @ [2m[SHA][0mπ [36mMerging 1 commit to [1m[36mmain[0m[36m @ [2m[SHA][0m (no rebase needed)-[107m [0m * [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Squash commits from main+[107m [0m * [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m feat: implement user authentication system[107m [0m auth.txt | 1 [32m+[m[107m [0m 1 file changed, 1 insertion(+)β [32mMerged to [1m[32mmain[0m[0m (1 commit, 1 file, [32m+1[0m)