Skip to content

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.toml
index 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.rs
index 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.rs
index 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.snap
index 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: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: update file 1 content"
- WORKTRUNK_COMMIT_GENERATION__COMMAND: echo
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: true
@@ -26,15 +24,10 @@ exit_code: 0
πŸ”„ Squashing 2 commits & working tree changes into a single commit (2 files, +2)...
πŸ’‘ Backup created @ [SHA]
πŸ”„ 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
+  fix: update file 1 content
βœ… Squashed @ [SHA]
πŸ”„ Merging 1 commit to main @ [SHA] (no rebase needed)
-  * [SHA] (HEAD -> feature) Squash commits from main
+  * [SHA] (HEAD -> feature) fix: update file 1 content
  file1.txt | 1 +
  file2.txt | 1 +
  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.snap
index 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: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: improve auth validation logic"
- WORKTRUNK_COMMIT_GENERATION__COMMAND: echo
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: true
@@ -26,14 +24,10 @@ exit_code: 0
πŸ”„ Squashing 1 commit & working tree changes into a single commit (1 file, +1)...
πŸ’‘ Backup created @ [SHA]
πŸ”„ Generating squash commit message...
-πŸ’‘ Using fallback commit message. Run 'wt config --help' for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add authentication
+  fix: improve auth validation logic
βœ… Squashed @ [SHA]
πŸ”„ Merging 1 commit to main @ [SHA] (no rebase needed)
-  * [SHA] (HEAD -> feature) Squash commits from main
+  * [SHA] (HEAD -> feature) fix: improve auth validation logic
  auth.txt | 1 +
  1 file changed, 1 insertion(+)
βœ… Merged to main (1 commit, 1 file, +1)
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.snap
index 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: C
LC_ALL: C
SOURCE_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: sh
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: true
@@ -26,15 +23,10 @@ exit_code: 0
----- stdout -----
πŸ”„ Squashing 2 commits into a single commit (1 file, +1)...
πŸ”„ Generating squash commit message...
-πŸ’‘ Using fallback commit message. Run 'wt config --help' for LLM setup guide
-  Squash commits from main
- 
-  Combined commits:
-  - feat: add authentication
-  - fix: handle edge case
+  feat: implement user authentication system
βœ… Squashed @ [SHA]
πŸ”„ Merging 1 commit to main @ [SHA] (no rebase needed)
-  * [SHA] (HEAD -> feature) Squash commits from main
+  * [SHA] (HEAD -> feature) feat: implement user authentication system
  auth.txt | 1 +
  1 file changed, 1 insertion(+)
βœ… Merged to main (1 commit, 1 file, +1)