Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Add default_branch template variable for hook expansion
Maximilian Roos457c3aa19002
diff --git a/docs/content/hooks.md b/docs/content/hooks.mdindex dc484d582..1223c38c5 100644--- a/docs/content/hooks.md+++ b/docs/content/hooks.md@@ -73,6 +73,7 @@ Hooks can use template variables that expand at runtime:- `{{ branch }}` — Branch name (slashes replaced with dashes)- `{{ worktree }}` — Absolute path to the worktree- `{{ repo_root }}` — Absolute path to the repository root+- `{{ default_branch }}` — Default branch name (e.g., "main")### Merge Variables (pre-commit, pre-merge, post-merge)diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rsindex 7a0c21dbf..08af88283 100644--- a/src/commands/command_executor.rs+++ b/src/commands/command_executor.rs@@ -69,6 +69,9 @@ fn expand_commands("repo_root".to_string(),repo_root.to_string_lossy().to_string(),);+ if let Ok(default_branch) = ctx.repo.default_branch() {+ extras_owned.insert("default_branch".to_string(), default_branch);+ }for &(key, value) in extra_vars {extras_owned.insert(key.to_string(), value.to_string());}diff --git a/src/config/expansion.rs b/src/config/expansion.rsindex 70a8eee39..4bf70658e 100644--- a/src/config/expansion.rs+++ b/src/config/expansion.rs@@ -100,6 +100,7 @@ pub fn expand_template(/// - `{{ branch }}` - Branch name (sanitized: slashes → dashes)/// - `{{ worktree }}` - Path to the worktree/// - `{{ repo_root }}` - Path to the main repository root+/// - `{{ default_branch }}` - Default branch name (e.g., "main")/// - `{{ target }}` - Target branch (for merge commands, optional)////// # Examplesdiff --git a/src/config/project.rs b/src/config/project.rsindex 35f663178..4123662c5 100644--- a/src/config/project.rs+++ b/src/config/project.rs@@ -20,6 +20,7 @@ use super::commands::CommandConfig;/// - `{{ branch }}` - Branch name (e.g., "feature-foo")/// - `{{ worktree }}` - Absolute path to the worktree/// - `{{ repo_root }}` - Absolute path to the repository root+/// - `{{ default_branch }}` - Default branch name (e.g., "main")////// Merge-related hooks (`pre-commit`, `pre-merge`, `post-merge`) also support:/// - `{{ target }}` - Target branch for the merge (e.g., "main")@@ -28,14 +29,14 @@ pub struct ProjectConfig {/// Commands to execute sequentially before worktree is ready (blocking)/// Supports string (single command), array (sequential), or table (named, sequential)///- /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`+ /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ default_branch }}`#[serde(default, rename = "post-create")]pub post_create: Option<CommandConfig>,/// Commands to execute in parallel as background processes (non-blocking)/// Supports string (single), array (parallel), or table (named, parallel)///- /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`+ /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ default_branch }}`#[serde(default, rename = "post-start")]pub post_start: Option<CommandConfig>,@@ -44,7 +45,7 @@ pub struct ProjectConfig {/// All commands must exit with code 0 for commit to proceed/// Runs before any commit operation during `wt merge` (both squash and no-squash modes)///- /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ target }}`+ /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ default_branch }}`, `{{ target }}`#[serde(default, rename = "pre-commit")]pub pre_commit: Option<CommandConfig>,@@ -52,7 +53,7 @@ pub struct ProjectConfig {/// Supports string (single command), array (sequential), or table (named, sequential)/// All commands must exit with code 0 for merge to proceed///- /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ target }}`+ /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ default_branch }}`, `{{ target }}`#[serde(default, rename = "pre-merge")]pub pre_merge: Option<CommandConfig>,@@ -60,7 +61,7 @@ pub struct ProjectConfig {/// Supports string (single command), array (sequential), or table (named, sequential)/// Runs after push succeeds but before cleanup///- /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ target }}`+ /// Available template variables: `{{ repo }}`, `{{ branch }}`, `{{ worktree }}`, `{{ repo_root }}`, `{{ default_branch }}`, `{{ target }}`#[serde(default, rename = "post-merge")]pub post_merge: Option<CommandConfig>,diff --git a/tests/integration_tests/post_start_commands.rs b/tests/integration_tests/post_start_commands.rsindex 4e3c64ef8..dc489f4db 100644--- a/tests/integration_tests/post_start_commands.rs+++ b/tests/integration_tests/post_start_commands.rs@@ -229,6 +229,52 @@ approved-commands = [);}+#[test]+fn test_post_create_default_branch_template() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ // Create project config with default_branch template variable+ repo.write_project_config(+ r#"post-create = "echo 'Default: {{ default_branch }}' > default.txt""#,+ );++ repo.commit("Add config with default_branch template");++ // Pre-approve the command+ repo.write_test_config(+ r#"worktree-path = "../{{ main_worktree }}.{{ branch }}"++[projects."repo"]+approved-commands = ["echo 'Default: {{ default_branch }}' > default.txt"]+"#,+ );++ // Create a feature branch worktree (--force skips approval prompt)+ snapshot_switch(+ "post_create_default_branch_template",+ &repo,+ &["--create", "feature", "--force"],+ );++ // Verify template expansion actually worked+ let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");+ let default_file = worktree_path.join("default.txt");++ assert!(+ default_file.exists(),+ "default.txt should have been created in the worktree"+ );++ let contents = fs::read_to_string(&default_file).unwrap();+ assert!(+ contents.contains("Default: main"),+ "Should contain expanded default_branch, got: {}",+ contents+ );+}+// ============================================================================// Post-Start Command Tests (parallel, background)// ============================================================================diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_default_branch_template.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_default_branch_template.snapnew file mode 100644index 000000000..f2bd79597--- /dev/null+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_default_branch_template.snap@@ -0,0 +1,32 @@+---+source: tests/integration_tests/post_start_commands.rs+info:+ program: wt+ args:+ - switch+ - "--create"+ - feature+ - "--force"+ 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: ""+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1735776000"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+🔄 [36mRunning post-create:[39m+[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'Default: main'[0m[2m [0m[2m[36m>[0m[2m default.txt[0m+[0m✅ [32m[32mCreated new worktree for [1mfeature[22m from [1mmain[22m at [1m[REPO].feature[39m[22m[39m+💡 [2mRun [90mwt config shell install[39m to enable automatic cd[22m