Skip to content

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

feat(commit-generation): Allow custom prompt templates

Maximilian Roosb746fed9a277
diff --git a/README.md b/README.md
index a8805f0d9..6011d5d57 100644
--- a/README.md
+++ b/README.md
@@ -112,7 +112,7 @@ Global config at `~/.config/worktrunk/config.toml`:
```toml
worktree-path = "../{main-worktree}.{branch}"
-[llm]
+[commit-generation]
command = "llm"
args = ["-m", "claude-3-7-sonnet-20250219"]
```
@@ -145,7 +145,7 @@ Set up LLM integration: `wt config help` shows the setup guide, `wt config init`
Edit `~/.config/worktrunk/config.toml`:
```toml
-[llm]
+[commit-generation]
command = "llm" # or "claude", "gpt", etc.
args = ["-m", "claude-haiku-4-5-20251001"]
```
@@ -154,6 +154,90 @@ If the LLM is unavailable or fails, worktrunk falls back to a deterministic mess
</details>
+<details>
+<summary><b>Advanced: Custom Prompt Templates</b></summary>
+
+Worktrunk uses [minijinja templates](https://docs.rs/minijinja/latest/minijinja/syntax/index.html) for commit message prompts, giving you full control over what the LLM sees.
+
+**Inline template for normal commits:**
+
+```toml
+[commit-generation]
+command = "llm"
+args = ["-s"]
+template = """
+Generate a commit message for {{ repo | upper }}.
+
+Branch: {{ branch }}
+{%- if recent_commits %}
+
+Recent commit style ({{ recent_commits | length }} commits):
+{%- for commit in recent_commits %}
+ {{ loop.index }}. {{ commit }}
+{%- endfor %}
+{%- endif %}
+
+Changes to commit:
+```
+{{ git_diff }}
+```
+
+Requirements:
+- Follow the style of recent commits above
+- First line under 50 chars
+- Focus on WHY, not HOW
+"""
+```
+
+**Inline template for squash commits:**
+
+```toml
+[commit-generation]
+command = "llm"
+squash-template = """
+Squashing {{ commits | length }} commit(s) from {{ branch }} to {{ target_branch }}.
+
+{% if commits | length > 1 -%}
+Commits being combined:
+{%- for c in commits %}
+ {{ loop.index }}/{{ loop.length }}: {{ c }}
+{%- endfor %}
+{%- else -%}
+Single commit: {{ commits[0] }}
+{%- endif %}
+
+Generate one cohesive commit message that captures the overall change.
+Use conventional commit format (feat/fix/docs/refactor).
+"""
+```
+
+**External template files:**
+
+```toml
+[commit-generation]
+command = "claude"
+template-file = "~/.config/worktrunk/commit-template.jinja"
+squash-template-file = "~/.config/worktrunk/squash-template.jinja"
+```
+
+**Available template variables:**
+
+Normal commits:
+- `{{ git_diff }}` - Staged changes
+- `{{ branch }}` - Current branch name
+- `{{ recent_commits }}` - Array of recent commit messages (for style matching)
+- `{{ repo }}` - Repository name
+
+Squash commits:
+- `{{ commits }}` - Array of commit messages being squashed
+- `{{ target_branch }}` - Branch being merged into (e.g., "main")
+- `{{ branch }}` - Current branch name
+- `{{ repo }}` - Repository name
+
+See the [minijinja template documentation](https://docs.rs/minijinja/latest/minijinja/syntax/index.html) for complete syntax reference (filters, conditionals, loops, whitespace control, etc.).
+
+</details>
+
### Project Automation
Automate common tasks by creating `.config/wt.toml` in your repository root. Run tests before merging, install dependencies when creating worktrees, start dev servers automatically.
diff --git a/src/llm.rs b/src/llm.rs
index c8a536a8d..52290d662 100644
--- a/src/llm.rs
+++ b/src/llm.rs
@@ -496,4 +496,142 @@ mod tests {
"Repo: myrepo\nBranch: feature\nTarget: main\nB\nA\n"
);
}
+
+ #[test]
+ fn test_build_commit_prompt_with_sophisticated_jinja() {
+ // Test advanced jinja features: filters, length, conditionals, whitespace control
+ let config = CommitGenerationConfig {
+ command: None,
+ args: vec![],
+ template: Some(
+ r#"=== {{ repo | upper }} ===
+Branch: {{ branch }}
+{%- if recent_commits %}
+Commits: {{ recent_commits | length }}
+{%- for c in recent_commits %}
+ - {{ loop.index }}. {{ c }}
+{%- endfor %}
+{%- else %}
+No recent commits
+{%- endif %}
+
+Diff follows:
+{{ git_diff }}"#
+ .to_string(),
+ ),
+ template_file: None,
+ squash_template: None,
+ squash_template_file: None,
+ };
+ let commits = vec![
+ "feat: add auth".to_string(),
+ "fix: bug".to_string(),
+ "docs: update".to_string(),
+ ];
+ let result = build_commit_prompt(
+ &config,
+ "my diff content",
+ "feature-x",
+ Some(&commits),
+ "myapp",
+ );
+ assert!(result.is_ok());
+ let prompt = result.unwrap();
+
+ // Verify filters work (upper)
+ assert!(prompt.contains("=== MYAPP ==="));
+
+ // Verify length filter
+ assert!(prompt.contains("Commits: 3"));
+
+ // Verify loop.index
+ assert!(prompt.contains(" - 1. feat: add auth"));
+ assert!(prompt.contains(" - 2. fix: bug"));
+ assert!(prompt.contains(" - 3. docs: update"));
+
+ // Verify whitespace control (no blank lines after "Branch:")
+ assert!(prompt.contains("Branch: feature-x\nCommits: 3"));
+
+ // Verify diff is included
+ assert!(prompt.contains("Diff follows:\nmy diff content"));
+ }
+
+ #[test]
+ fn test_build_commit_prompt_with_sophisticated_jinja_no_commits() {
+ // Test the else branch of conditionals
+ let config = CommitGenerationConfig {
+ command: None,
+ args: vec![],
+ template: Some(
+ r#"Repo: {{ repo | upper }}
+{%- if recent_commits %}
+Has commits: {{ recent_commits | length }}
+{%- else %}
+No recent commits
+{%- endif %}"#
+ .to_string(),
+ ),
+ template_file: None,
+ squash_template: None,
+ squash_template_file: None,
+ };
+ let result = build_commit_prompt(&config, "diff", "main", None, "test");
+ assert!(result.is_ok());
+ let prompt = result.unwrap();
+
+ assert!(prompt.contains("Repo: TEST"));
+ assert!(prompt.contains("No recent commits"));
+ assert!(!prompt.contains("Has commits"));
+ }
+
+ #[test]
+ fn test_build_squash_prompt_with_sophisticated_jinja() {
+ // Test sophisticated jinja in squash templates
+ let config = CommitGenerationConfig {
+ command: None,
+ args: vec![],
+ template: None,
+ template_file: None,
+ squash_template: Some(
+ r#"Squashing {{ commits | length }} commit(s) from {{ branch }} to {{ target_branch }}
+{% if commits | length > 1 -%}
+Multiple commits detected:
+{%- for c in commits %}
+ {{ loop.index }}/{{ loop.length }}: {{ c }}
+{%- endfor %}
+{%- else -%}
+Single commit: {{ commits[0] }}
+{%- endif %}"#
+ .to_string(),
+ ),
+ squash_template_file: None,
+ };
+
+ // Test with multiple commits
+ let commits = vec![
+ "commit A".to_string(),
+ "commit B".to_string(),
+ "commit C".to_string(),
+ ];
+ let result = build_squash_prompt(&config, "main", &commits, "feature", "repo");
+ assert!(result.is_ok());
+ let prompt = result.unwrap();
+
+ // Commits are reversed for chronological order, so we expect C, B, A
+ assert!(prompt.contains("Squashing 3 commit(s) from feature to main"));
+ assert!(prompt.contains("Multiple commits detected:"));
+ assert!(prompt.contains("1/3: commit C")); // First in chronological order
+ assert!(prompt.contains("2/3: commit B"));
+ assert!(prompt.contains("3/3: commit A")); // Last in chronological order
+
+ // Test with single commit
+ let single_commit = vec!["solo commit".to_string()];
+ let result = build_squash_prompt(&config, "main", &single_commit, "feature", "repo");
+ assert!(result.is_ok());
+ let prompt = result.unwrap();
+
+ assert!(prompt.contains("Squashing 1 commit(s)"));
+ assert!(prompt.contains("Single commit: solo commit"));
+ assert!(!prompt.contains("Multiple commits detected"));
+ }
}