Skip to content

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

Show command templates in approval prompts

Maximilian Roos2315d268fe29
diff --git a/src/commands/command_approval.rs b/src/commands/command_approval.rs
index 2f6a1be68..3d83beb4a 100644
--- a/src/commands/command_approval.rs
+++ b/src/commands/command_approval.rs
@@ -33,7 +33,8 @@ use worktrunk::styling::{
/// Returns `Ok(true)` when execution may continue, `Ok(false)` when the user
/// declined, and `Err` if config reload/save fails.
///
-/// Shows expanded commands to the user. Templates are saved to config for future approval checks.
+/// Shows command templates to the user (what gets saved to config), not expanded values.
+/// This ensures users see exactly what they're approving.
///
/// # Parameters
/// - `commands_already_filtered`: If true, commands list is pre-filtered; skip filtering by approval status
@@ -123,7 +124,7 @@ fn prompt_for_batch_approval(commands: &[&Command], project_id: &str) -> anyhow:
None => format!("{INFO_EMOJI} {phase}:"),
};
eprintln!("{label}");
- eprint!("{}", format_bash_with_gutter(&cmd.expanded, ""));
+ eprint!("{}", format_bash_with_gutter(&cmd.template, ""));
}
// Check if stdin is a TTY before attempting to prompt
@@ -150,35 +151,28 @@ fn prompt_for_batch_approval(commands: &[&Command], project_id: &str) -> anyhow:
Ok(response.trim().eq_ignore_ascii_case("y"))
}
-/// Collect project commands for hooks and request batch approval with template expansion.
+/// Collect project commands for hooks and request batch approval.
///
/// This is the "gate" function that should be called at command entry points
/// (like `wt remove`, `wt switch --create`, `wt merge`) before any hooks execute.
-/// It expands template variables before showing the approval prompt, so users see
-/// the actual commands that will run (e.g., with actual branch names) instead of
-/// template placeholders.
+/// Shows command templates (not expanded values) so users see exactly what
+/// patterns they're approving.
///
/// # Parameters
-/// - `ctx`: Command context providing template variables (branch, worktree, etc.)
+/// - `ctx`: Command context (provides project identifier and config)
/// - `hook_types`: Which hook types to collect commands for
-/// - `extra_vars`: Additional template variables
///
/// # Example
///
/// ```ignore
/// let ctx = CommandContext::new(&repo, &config, &branch, &worktree_path, &repo_root, force);
-/// let approved = approve_hooks(
-/// &ctx,
-/// &[HookType::PostCreate, HookType::PostStart],
-/// &[],
-/// )?;
+/// let approved = approve_hooks(&ctx, &[HookType::PostCreate, HookType::PostStart])?;
/// ```
pub fn approve_hooks(
ctx: &super::command_executor::CommandContext<'_>,
hook_types: &[HookType],
- extra_vars: &[(&str, &str)],
) -> anyhow::Result<bool> {
- approve_hooks_filtered(ctx, hook_types, extra_vars, None)
+ approve_hooks_filtered(ctx, hook_types, None)
}
/// Like `approve_hooks` but with optional name filter for targeted hook approval.
@@ -189,7 +183,6 @@ pub fn approve_hooks(
pub fn approve_hooks_filtered(
ctx: &super::command_executor::CommandContext<'_>,
hook_types: &[HookType],
- extra_vars: &[(&str, &str)],
name_filter: Option<&str>,
) -> anyhow::Result<bool> {
let project_config = match ctx.repo.load_project_config()? {
@@ -208,10 +201,6 @@ pub fn approve_hooks_filtered(
return Ok(true);
}
- // Expand templates with the provided context
- let expanded =
- super::command_executor::expand_commands_for_approval(&commands, ctx, extra_vars)?;
-
let project_id = ctx.repo.project_identifier()?;
- approve_command_batch(&expanded, &project_id, ctx.config, ctx.force, false)
+ approve_command_batch(&commands, &project_id, ctx.config, ctx.force, false)
}
diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rs
index 938502aab..bc1716aa9 100644
--- a/src/commands/command_executor.rs
+++ b/src/commands/command_executor.rs
@@ -164,19 +164,6 @@ fn expand_commands(
Ok(result)
}
-/// Expand commands for approval display (public version for gate approval)
-///
-/// Returns commands with their `expanded` field filled in, showing actual values
-/// instead of template placeholders.
-pub fn expand_commands_for_approval(
- commands: &[Command],
- ctx: &CommandContext<'_>,
- extra_vars: &[(&str, &str)],
-) -> anyhow::Result<Vec<Command>> {
- let expanded = expand_commands(commands, ctx, extra_vars)?;
- Ok(expanded.into_iter().map(|(cmd, _json)| cmd).collect())
-}
-
/// Prepare project commands for execution
///
/// This function:
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 353b677f6..d654b5ecf 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -4,7 +4,7 @@ use worktrunk::git::Repository;
use worktrunk::styling::info_message;
use super::command_approval::approve_command_batch;
-use super::command_executor::{CommandContext, expand_commands_for_approval};
+use super::command_executor::CommandContext;
use super::commit::CommitOptions;
use super::context::CommandEnv;
use super::hooks::{HookFailureStrategy, HookPipeline, HookSource};
@@ -118,13 +118,8 @@ pub fn handle_merge(
}
.collect()?;
- // Expand commands for approval display (shows actual values instead of templates)
- let ctx = env.context(force);
- let expanded =
- expand_commands_for_approval(&all_commands, &ctx, &[("target", &target_branch)])?;
-
- // Approve all expanded commands in a single batch
- let approved = approve_command_batch(&expanded, &project_id, config, force, false)?;
+ // Approve all commands in a single batch (shows templates, not expanded values)
+ let approved = approve_command_batch(&all_commands, &project_id, config, force, false)?;
// If commands were declined, skip hooks but continue with merge
// Shadow verify to gate all subsequent hook execution on approval
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index 7ffb2e05b..842fee260 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -36,27 +36,8 @@ pub fn run_hook(hook_type: HookType, force: bool, name_filter: Option<&str>) ->
let project_config = repo.load_project_config()?;
// "Approve at the Gate": approve project hooks upfront
- // Get extra vars for template expansion based on hook type
- // For wt hook pre-merge/post-merge, use current branch as target (not default branch)
- // because we're testing hooks in the context of the current worktree
- let default_branch = repo.default_branch().ok();
- let extra_vars: Vec<(&str, &str)> = match hook_type {
- HookType::PreCommit => {
- // Pre-commit uses default branch as target (for comparison context)
- default_branch
- .as_deref()
- .into_iter()
- .map(|t| ("target", t))
- .collect()
- }
- HookType::PreMerge | HookType::PostMerge => {
- // Pre-merge and post-merge use current branch as target for testing
- vec![("target", env.branch.as_str())]
- }
- _ => Vec::new(),
- };
// Pass name_filter to only approve the targeted hook, not all hooks of this type
- let approved = approve_hooks_filtered(&ctx, &[hook_type], &extra_vars, name_filter)?;
+ let approved = approve_hooks_filtered(&ctx, &[hook_type], name_filter)?;
// If declined, return early - the whole point of `wt hook` is to run hooks
if !approved {
crate::output::print(worktrunk::styling::info_message("Commands declined"))?;
@@ -227,13 +208,7 @@ pub fn step_commit(
// "Approve at the Gate": approve pre-commit hooks upfront (unless --no-verify)
// Shadow no_verify: if user declines approval, skip hooks but continue commit
let no_verify = if !no_verify {
- let target_branch = env.repo.default_branch().ok();
- let extra_vars: Vec<(&str, &str)> = target_branch
- .as_deref()
- .into_iter()
- .map(|t| ("target", t))
- .collect();
- let approved = approve_hooks(&ctx, &[HookType::PreCommit], &extra_vars)?;
+ let approved = approve_hooks(&ctx, &[HookType::PreCommit])?;
if !approved {
crate::output::print(worktrunk::styling::info_message(
"Commands declined, committing without hooks",
diff --git a/src/main.rs b/src/main.rs
index 9c72cf884..805eb2961 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -924,13 +924,7 @@ fn main() {
use commands::context::CommandEnv;
let env = CommandEnv::for_action("squash")?;
let ctx = env.context(force);
- let target_branch = env.repo.default_branch().ok();
- let extra_vars: Vec<(&str, &str)> = target_branch
- .as_deref()
- .into_iter()
- .map(|t| ("target", t))
- .collect();
- let approved = approve_hooks(&ctx, &[HookType::PreCommit], &extra_vars)?;
+ let approved = approve_hooks(&ctx, &[HookType::PreCommit])?;
if !approved {
crate::output::print(info_message(
"Commands declined, squashing without hooks",
@@ -1083,7 +1077,7 @@ fn main() {
&repo_root,
force,
);
- approve_hooks(&ctx, &[HookType::PostCreate, HookType::PostStart], &[])?
+ approve_hooks(&ctx, &[HookType::PostCreate, HookType::PostStart])?
} else {
true // No hooks to approve = considered approved
};
@@ -1169,7 +1163,7 @@ fn main() {
&repo_root,
force,
);
- let approved = approve_hooks(&ctx, &[HookType::PreRemove], &[])?;
+ let approved = approve_hooks(&ctx, &[HookType::PreRemove])?;
// If declined, skip hooks but continue with removal
if !approved {
crate::output::print(info_message(
diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rs
index 79b09b14b..aafe6b77d 100644
--- a/tests/integration_tests/remove.rs
+++ b/tests/integration_tests/remove.rs
@@ -808,7 +808,7 @@ worktree_name = "echo 'Name: {{ worktree_name }}'"
);
repo.commit("Add config with templates");
- // Pre-approve the commands (use expanded forms for approval)
+ // Pre-approve the commands (templates match what's shown in prompts)
repo.write_test_config(
r#"worktree-path = "../{{ main_worktree }}.{{ branch }}"
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap
index c3ed37c79..8c6e3cb69 100644
--- a/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap
+++ b/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap
@@ -9,13 +9,13 @@ exit_code: 1
🟑 repo needs approval to execute 4 commands:
βšͺ post-create branch:
-  echo 'Branch: test-nested-branch'
+  echo 'Branch: {{ branch }}'
βšͺ post-create worktree:
-  echo 'Worktree: [REPO].test-nested-branch'
+  echo 'Worktree: {{ worktree }}'
βšͺ post-create repo:
-  echo 'Repo: repo'
+  echo 'Repo: {{ main_worktree }}'
βšͺ post-create pwd:
-  cd [REPO].test-nested-branch && pwd
+  cd {{ worktree }} && pwd
❌ Cannot prompt for approval in non-interactive environment
πŸ’‘ In CI/CD, use --force to skip prompts. To pre-approve commands, use wt hook approvals add
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap
index 3fdbd8f03..d26f179da 100644
--- a/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap
+++ b/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap
@@ -9,7 +9,7 @@ exit_code: 1
🟑 repo needs approval to execute 1 command:
βšͺ post-create:
-  echo 'Worktree path: [REPO].feature-test-approval'
+  echo 'Worktree path: {{ worktree }}'
❌ Cannot prompt for approval in non-interactive environment
πŸ’‘ In CI/CD, use --force to skip prompts. To pre-approve commands, use wt hook approvals add
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap
index f73906acb..196c7dd2f 100644
--- a/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap
+++ b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap
@@ -9,7 +9,7 @@ exit_code: 1
🟑 repo needs approval to execute 1 command:
βšͺ post-merge:
-  echo 'Post-merge cleanup for main'
+  echo 'Post-merge cleanup for {{ branch }}'
❌ Cannot prompt for approval in non-interactive environment
πŸ’‘ In CI/CD, use --force to skip prompts. To pre-approve commands, use wt hook approvals add
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap
index d9688745e..256c5f6ce 100644
--- a/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap
+++ b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap
@@ -9,7 +9,7 @@ exit_code: 1
🟑 repo needs approval to execute 1 command:
βšͺ pre-merge:
-  echo 'Running pre-merge checks on main'
+  echo 'Running pre-merge checks on {{ branch }}'
❌ Cannot prompt for approval in non-interactive environment
πŸ’‘ In CI/CD, use --force to skip prompts. To pre-approve commands, use wt hook approvals add