Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Sync config.example.toml templates with src/llm.rs
Maximilian Roosb8a5466928b3
diff --git a/Cargo.toml b/Cargo.tomlindex c99dbcb64..ef6a81341 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -84,8 +84,8 @@ rstest = "0.26"tempfile = "3.23"toml = "0.9"criterion = "0.7"-regex = "1.12"portable-pty = "0.9"+regex = "1.12"vt100 = "0.16"fs2 = "0.4"diff --git a/dev/config.example.toml b/dev/config.example.tomlindex 3b8818a00..0b5f145af 100644--- a/dev/config.example.toml+++ b/dev/config.example.toml@@ -69,14 +69,12 @@ command = "npm install"# squash-template-file = "~/.config/worktrunk/squash-template.txt"# Custom Prompt Templates (Advanced)-# NOTE: The commented templates below match src/llm.rs constants:-# - DEFAULT_TEMPLATE (lines 41-76)-# - DEFAULT_SQUASH_TEMPLATE (lines 109-118)-# When changing these constants, update the commented sections here to match.+# NOTE: Templates are synced from src/llm.rs by `cargo test readme_sync`# Optional: Custom prompt template (inline) - Uses minijinja syntax# Available variables: {{ git_diff }}, {{ branch }}, {{ recent_commits }}, {{ repo }}# If not specified, uses the default template shown below:+# <!-- DEFAULT_TEMPLATE_START --># template = """# Format# - First line: <50 chars, present tense, describes WHAT and WHY (not HOW).@@ -113,6 +111,7 @@ command = "npm install"# {% endif %}# </git-info># """+# <!-- DEFAULT_TEMPLATE_END -->## Example alternative template with different style:# template = """@@ -141,6 +140,7 @@ command = "npm install"# Optional: Custom squash commit message template (inline) - Uses minijinja syntax# Available variables: {{ commits }}, {{ target_branch }}, {{ branch }}, {{ repo }}# If not specified, uses the default template:+# <!-- DEFAULT_SQUASH_TEMPLATE_START --># squash-template = """# Generate a commit message that combines these changes into one cohesive message. Output only the commit message without any explanation.#@@ -157,6 +157,7 @@ command = "npm install"# - {{ commit }}# {% endfor %}# """+# <!-- DEFAULT_SQUASH_TEMPLATE_END -->## Example alternative template:# squash-template = """diff --git a/src/llm.rs b/src/llm.rsindex 9304808b7..04389d7b7 100644--- a/src/llm.rs+++ b/src/llm.rs@@ -15,8 +15,7 @@ struct PromptContext<'a> {/// Default template for 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 41-76).+/// Synced to dev/config.example.toml by `cargo test readme_sync`const DEFAULT_TEMPLATE: &str = r#"Format- First line: <50 chars, present tense, describes WHAT and WHY (not HOW).- Blank line after first line.@@ -55,8 +54,7 @@ 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 110-125).+/// Synced to dev/config.example.toml by `cargo test readme_sync`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.Formatdiff --git a/tests/integration_tests/readme_sync.rs b/tests/integration_tests/readme_sync.rsindex a1e51af44..afa3cfe7b 100644--- a/tests/integration_tests/readme_sync.rs+++ b/tests/integration_tests/readme_sync.rs@@ -1,7 +1,8 @@-//! README synchronization test+//! README and config synchronization tests//!//! Verifies that README.md examples stay in sync with their source snapshots and help output.-//! Automatically updates help sections when out of sync.+//! Also syncs default templates from src/llm.rs to dev/config.example.toml.+//! Automatically updates sections when out of sync.//!//! Run with: `cargo test --test integration readme_sync`@@ -49,6 +50,26 @@ static TMPDIR_REGEX: LazyLock<Regex> =static REPO_REGEX: LazyLock<Regex> =LazyLock::new(|| Regex::new(r"\[REPO\]").expect("Invalid REPO regex"));+/// Regex to find DEFAULT_TEMPLATE marker+static DEFAULT_TEMPLATE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {+ Regex::new(r"(?s)(# <!-- DEFAULT_TEMPLATE_START -->\n).*?(# <!-- DEFAULT_TEMPLATE_END -->)")+ .expect("Invalid DEFAULT_TEMPLATE marker regex")+});++/// Regex to find DEFAULT_SQUASH_TEMPLATE marker+static SQUASH_TEMPLATE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {+ Regex::new(+ r"(?s)(# <!-- DEFAULT_SQUASH_TEMPLATE_START -->\n).*?(# <!-- DEFAULT_SQUASH_TEMPLATE_END -->)",+ )+ .expect("Invalid DEFAULT_SQUASH_TEMPLATE marker regex")+});++/// Regex to extract Rust raw string constants (single pound)+static RUST_RAW_STRING_PATTERN: LazyLock<Regex> = LazyLock::new(|| {+ Regex::new(r##"(?s)const (DEFAULT_TEMPLATE|DEFAULT_SQUASH_TEMPLATE): &str = r#"(.*?)"#;"##)+ .expect("Invalid Rust raw string regex")+});+/// Strip ANSI escape codes from textfn strip_ansi(text: &str) -> String {let text = ANSI_ESCAPE_REGEX.replace_all(text, "");@@ -275,6 +296,99 @@ fn update_readme_section(}}+/// Convert a template to commented TOML format+fn comment_template(template: &str) -> String {+ template+ .lines()+ .map(|line| {+ if line.is_empty() {+ String::from("#")+ } else {+ format!("# {line}")+ }+ })+ .collect::<Vec<_>>()+ .join("\n")+}++/// Extract templates from llm.rs source+fn extract_templates(content: &str) -> std::collections::HashMap<String, String> {+ RUST_RAW_STRING_PATTERN+ .captures_iter(content)+ .map(|cap| {+ let name = cap.get(1).unwrap().as_str().to_string();+ let template = cap.get(2).unwrap().as_str().to_string();+ (name, template)+ })+ .collect()+}++#[test]+fn test_config_example_templates_are_in_sync() {+ let project_root = Path::new(env!("CARGO_MANIFEST_DIR"));+ let llm_rs_path = project_root.join("src/llm.rs");+ let config_path = project_root.join("dev/config.example.toml");++ let llm_content = fs::read_to_string(&llm_rs_path).expect("Failed to read src/llm.rs");+ let config_content =+ fs::read_to_string(&config_path).expect("Failed to read config.example.toml");++ // Extract templates from llm.rs+ let templates = extract_templates(&llm_content);+ assert!(+ templates.contains_key("DEFAULT_TEMPLATE"),+ "DEFAULT_TEMPLATE not found in src/llm.rs"+ );+ assert!(+ templates.contains_key("DEFAULT_SQUASH_TEMPLATE"),+ "DEFAULT_SQUASH_TEMPLATE not found in src/llm.rs"+ );++ let mut updated_content = config_content.clone();+ let mut updated_count = 0;++ // Helper to replace a template section+ let mut replace_template = |pattern: &Regex, name: &str, key: &str| {+ if let Some(cap) = pattern.captures(&updated_content.clone()) {+ let full_match = cap.get(0).unwrap();+ let prefix = cap.get(1).unwrap().as_str();+ let suffix = cap.get(2).unwrap().as_str();++ let template = templates+ .get(name)+ .unwrap_or_else(|| panic!("{name} not found in src/llm.rs"));+ let commented = comment_template(template);++ let replacement = format!(+ r#"{prefix}# {key} = """+{commented}+# """+{suffix}"#+ );++ if full_match.as_str() != replacement {+ updated_content = updated_content.replace(full_match.as_str(), &replacement);+ updated_count += 1;+ }+ }+ };++ replace_template(&DEFAULT_TEMPLATE_PATTERN, "DEFAULT_TEMPLATE", "template");+ replace_template(+ &SQUASH_TEMPLATE_PATTERN,+ "DEFAULT_SQUASH_TEMPLATE",+ "squash-template",+ );++ if updated_count > 0 {+ fs::write(&config_path, &updated_content).expect("Failed to write config.example.toml");+ println!(+ "✅ Updated {} template sections in config.example.toml",+ updated_count+ );+ }+}+#[test]fn test_readme_examples_are_in_sync() {let project_root = Path::new(env!("CARGO_MANIFEST_DIR"));diff --git a/tests/snapshots/integration__integration_tests__help__help_config_create.snap b/tests/snapshots/integration__integration_tests__help__help_config_create.snapindex c5c9141e3..4e10ca14a 100644--- a/tests/snapshots/integration__integration_tests__help__help_config_create.snap+++ b/tests/snapshots/integration__integration_tests__help__help_config_create.snap@@ -105,14 +105,12 @@ Creates [2m~/.config/worktrunk/config.toml[0m with the following content:[2m# squash-template-file = "~/.config/worktrunk/squash-template.txt"[0m[2m[0m[2m# Custom Prompt Templates (Advanced)[0m- [2m# NOTE: The commented templates below match src/llm.rs constants:[0m- [2m# - DEFAULT_TEMPLATE (lines 41-76)[0m- [2m# - DEFAULT_SQUASH_TEMPLATE (lines 109-118)[0m- [2m# When changing these constants, update the commented sections here to match.[0m+ [2m# NOTE: Templates are synced from src/llm.rs by `cargo test readme_sync`[0m[2m[0m[2m# Optional: Custom prompt template (inline) - Uses minijinja syntax[0m[2m# Available variables: {{ git_diff }}, {{ branch }}, {{ recent_commits }}, {{ repo }}[0m[2m# If not specified, uses the default template shown below:[0m+ [2m# <!-- DEFAULT_TEMPLATE_START -->[0m[2m# template = """[0m[2m# Format[0m[2m# - First line: <50 chars, present tense, describes WHAT and WHY (not HOW).[0m@@ -149,6 +147,7 @@ Creates [2m~/.config/worktrunk/config.toml[0m with the following content:[2m# {% endif %}[0m[2m# </git-info>[0m[2m# """[0m+ [2m# <!-- DEFAULT_TEMPLATE_END -->[0m[2m#[0m[2m# Example alternative template with different style:[0m[2m# template = """[0m@@ -177,6 +176,7 @@ Creates [2m~/.config/worktrunk/config.toml[0m with the following content:[2m# Optional: Custom squash commit message template (inline) - Uses minijinja syntax[0m[2m# Available variables: {{ commits }}, {{ target_branch }}, {{ branch }}, {{ repo }}[0m[2m# If not specified, uses the default template:[0m+ [2m# <!-- DEFAULT_SQUASH_TEMPLATE_START -->[0m[2m# squash-template = """[0m[2m# Generate a commit message that combines these changes into one cohesive message. Output only the commit message without any explanation.[0m[2m#[0m@@ -193,6 +193,7 @@ Creates [2m~/.config/worktrunk/config.toml[0m with the following content:[2m# - {{ commit }}[0m[2m# {% endfor %}[0m[2m# """[0m+ [2m# <!-- DEFAULT_SQUASH_TEMPLATE_END -->[0m[2m#[0m[2m# Example alternative template:[0m[2m# squash-template = """[0m