Skip to content

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

feat(dev): Enhance `ask-approvals` to handle project commands and remove unused imports

Maximilian Roos90dff7296d7b
diff --git a/src/commands/command_approval.rs b/src/commands/command_approval.rs
index 88f8b0168..7ae902043 100644
--- a/src/commands/command_approval.rs
+++ b/src/commands/command_approval.rs
@@ -14,16 +14,26 @@ use worktrunk::styling::{
/// declined, and `Err` if config reload/save fails.
///
/// Shows expanded commands to the user. Templates are saved to config for future approval checks.
+///
+/// # Parameters
+/// - `commands_already_filtered`: If true, commands list is pre-filtered; skip filtering by approval status
pub fn approve_command_batch(
commands: &[Command],
project_id: &str,
config: &WorktrunkConfig,
force: bool,
+ commands_already_filtered: bool,
) -> Result<bool, GitError> {
- let needs_approval: Vec<&Command> = commands
- .iter()
- .filter(|cmd| !config.is_command_approved(project_id, &cmd.template))
- .collect();
+ let needs_approval: Vec<&Command> = if commands_already_filtered {
+ // Commands already filtered by caller, use as-is
+ commands.iter().collect()
+ } else {
+ // Filter to only unapproved commands
+ commands
+ .iter()
+ .filter(|cmd| !config.is_command_approved(project_id, &cmd.template))
+ .collect()
+ };
if needs_approval.is_empty() {
return Ok(true);
diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rs
index 9ba2ec5ab..375433aa6 100644
--- a/src/commands/command_executor.rs
+++ b/src/commands/command_executor.rs
@@ -120,7 +120,13 @@ pub fn prepare_project_commands(
// Approve using expanded commands (which have both template and expanded forms)
if !auto_trust
- && !approve_command_batch(&expanded_commands, &project_id, ctx.config, ctx.force)?
+ && !approve_command_batch(
+ &expanded_commands,
+ &project_id,
+ ctx.config,
+ ctx.force,
+ false,
+ )?
{
return Err(GitError::CommandNotApproved);
}
diff --git a/src/commands/dev.rs b/src/commands/dev.rs
index 73801d0be..f05d2f2fa 100644
--- a/src/commands/dev.rs
+++ b/src/commands/dev.rs
@@ -391,39 +391,80 @@ pub fn handle_dev_rebase(target: Option<&str>) -> Result<bool, GitError> {
Ok(true)
}
-/// Handle `wt dev ask-approval` command - test the approval prompt UI
-pub fn handle_dev_ask_approval(force: bool) -> Result<(), GitError> {
+/// Handle `wt dev ask-approvals` command - approve all commands in the project
+pub fn handle_dev_ask_approvals(force: bool, show_all: bool) -> Result<(), GitError> {
use super::command_approval::approve_command_batch;
- use worktrunk::config::{Command, CommandPhase, WorktrunkConfig};
-
- // Create some test commands to show in the approval prompt
- let test_commands = vec![
- Command::with_expansion(
- Some("insta".to_string()),
- "NEXTEST_STATUS_LEVEL=fail cargo insta test".to_string(),
- "NEXTEST_STATUS_LEVEL=fail cargo insta test --test-runner nextest".to_string(),
- CommandPhase::PreMerge,
- ),
- Command::with_expansion(
- Some("doc".to_string()),
- "RUSTDOCFLAGS='-Dwarnings' cargo doc --no-deps".to_string(),
- "RUSTDOCFLAGS='-Dwarnings' cargo doc --no-deps".to_string(),
- CommandPhase::PreMerge,
- ),
- Command::with_expansion(
- None,
- "cargo install --path .".to_string(),
- "cargo install --path .".to_string(),
- CommandPhase::PostMerge,
- ),
- ];
+ use worktrunk::config::{CommandPhase, WorktrunkConfig};
+ use worktrunk::styling::INFO_EMOJI;
let repo = Repository::current();
let project_id = repo.project_identifier()?;
let config = WorktrunkConfig::load().git_context("Failed to load config")?;
+ // Load project config (show helpful error if missing)
+ let project_config = load_project_config(&repo)?;
+
+ // Collect all commands from the project config
+ let mut commands = Vec::new();
+
+ // post-create-command
+ if let Some(cmd_config) = &project_config.post_create_command {
+ commands.extend(cmd_config.commands_with_phase(CommandPhase::PostCreate));
+ }
+
+ // post-start-command
+ if let Some(cmd_config) = &project_config.post_start_command {
+ commands.extend(cmd_config.commands_with_phase(CommandPhase::PostStart));
+ }
+
+ // pre-commit-command
+ if let Some(cmd_config) = &project_config.pre_commit_command {
+ commands.extend(cmd_config.commands_with_phase(CommandPhase::PreCommit));
+ }
+
+ // pre-merge-command
+ if let Some(cmd_config) = &project_config.pre_merge_command {
+ commands.extend(cmd_config.commands_with_phase(CommandPhase::PreMerge));
+ }
+
+ // post-merge-command
+ if let Some(cmd_config) = &project_config.post_merge_command {
+ commands.extend(cmd_config.commands_with_phase(CommandPhase::PostMerge));
+ }
+
+ if commands.is_empty() {
+ let dim = worktrunk::styling::AnstyleStyle::new().dimmed();
+ crate::output::success(format!(
+ "{INFO_EMOJI} {dim}No commands configured in project{dim:#}"
+ ))?;
+ return Ok(());
+ }
+
+ // Filter to only unapproved commands (unless --all is specified)
+ let commands_to_approve = if !show_all {
+ let unapproved: Vec<_> = commands
+ .into_iter()
+ .filter(|cmd| !config.is_command_approved(&project_id, &cmd.template))
+ .collect();
+
+ if unapproved.is_empty() {
+ let dim = worktrunk::styling::AnstyleStyle::new().dimmed();
+ crate::output::success(format!(
+ "{INFO_EMOJI} {dim}All commands already approved{dim:#}"
+ ))?;
+ return Ok(());
+ }
+
+ unapproved
+ } else {
+ commands
+ };
+
// Call the approval prompt
- let approved = approve_command_batch(&test_commands, &project_id, &config, force)?;
+ // When show_all=true, we've already included all commands in commands_to_approve
+ // When show_all=false, we've already filtered to unapproved commands
+ // So we pass skip_approval_filter=true to prevent double-filtering
+ let approved = approve_command_batch(&commands_to_approve, &project_id, &config, force, true)?;
// Show result
if approved {
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index bbdb3a90f..a9b09946c 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -157,7 +157,7 @@ pub fn handle_merge(
// Approve all commands in a single batch
// Commands collected here are not yet expanded - expansion happens later in prepare_project_commands
- approve_command_batch(&all_commands, &project_id, &config, force)?;
+ approve_command_batch(&all_commands, &project_id, &config, force, false)?;
// Handle uncommitted changes (skip if --no-commit) - track whether commit occurred
let committed = if !no_commit && repo.is_dirty()? {
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 44521a9ca..d7055ebc8 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -16,7 +16,7 @@ pub use config::{
};
pub use configure_shell::{ConfigAction, handle_configure_shell};
pub use dev::{
- handle_dev_ask_approval, handle_dev_commit, handle_dev_push, handle_dev_rebase,
+ handle_dev_ask_approvals, handle_dev_commit, handle_dev_push, handle_dev_rebase,
handle_dev_run_hook, handle_dev_squash,
};
pub use init::handle_init;
diff --git a/src/main.rs b/src/main.rs
index b24666d0f..a0a78faeb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -32,7 +32,7 @@ use commands::worktree::SwitchResult;
use commands::{
ConfigAction, Shell, handle_complete, handle_completion, handle_config_help,
handle_config_init, handle_config_list, handle_config_refresh_cache, handle_configure_shell,
- handle_dev_ask_approval, handle_dev_commit, handle_dev_push, handle_dev_rebase,
+ handle_dev_ask_approvals, handle_dev_commit, handle_dev_push, handle_dev_rebase,
handle_dev_run_hook, handle_dev_squash, handle_init, handle_list, handle_merge, handle_remove,
handle_switch,
};
@@ -139,11 +139,15 @@ enum DevCommand {
target: Option<String>,
},
- /// Test approval prompt UI (for development/debugging)
- AskApproval {
+ /// Approve commands in the project config (shows unapproved by default)
+ AskApprovals {
/// Skip command approval prompts
#[arg(short, long)]
force: bool,
+
+ /// Show all commands including already-approved ones
+ #[arg(long)]
+ all: bool,
},
}
@@ -597,7 +601,7 @@ fn main() {
allow_merge_commits,
} => handle_dev_push(target.as_deref(), allow_merge_commits),
DevCommand::Rebase { target } => handle_dev_rebase(target.as_deref()).map(|_| ()),
- DevCommand::AskApproval { force } => handle_dev_ask_approval(force),
+ DevCommand::AskApprovals { force, all } => handle_dev_ask_approvals(force, all),
},
Commands::List {
format,
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index f037ba110..ee30c0696 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpBXcTuz/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp4TfyPE/test-config.toml
---
success: true
exit_code: 0
@@ -199,8 +199,8 @@ _wt() {
wt__config,shell)
cmd="wt__config__shell"
;;
- wt__dev,ask-approval)
- cmd="wt__dev__ask__approval"
+ wt__dev,ask-approvals)
+ cmd="wt__dev__ask__approvals"
;;
wt__dev,commit)
cmd="wt__dev__commit"
@@ -354,7 +354,7 @@ _wt() {
return 0
;;
wt__dev)
- opts="-v -h --verbose --help run-hook commit squash push rebase ask-approval"
+ opts="-v -h --verbose --help run-hook commit squash push rebase ask-approvals"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
@@ -367,8 +367,8 @@ _wt() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
- wt__dev__ask__approval)
- opts="-f -v -h --force --verbose --help"
+ wt__dev__ask__approvals)
+ opts="-f -v -h --force --all --verbose --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 4108672f2..8ba30eabf 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpPGWZC8/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp6NXzVa/test-config.toml
---
success: true
exit_code: 0
@@ -244,14 +244,14 @@ zsh\t''"
complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from shell" -s f -l force -d 'Skip confirmation prompt'
complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from shell" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from shell" -s h -l help -d 'Print help'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -s h -l help -d 'Print help'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -f -a "run-hook" -d 'Run a project hook for testing'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -f -a "commit" -d 'Commit changes with LLM-generated message'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -f -a "squash" -d 'Squash commits with LLM-generated message'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -f -a "push" -d 'Push changes to target branch'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -f -a "rebase" -d 'Rebase current branch onto target branch'
-complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approval" -f -a "ask-approval" -d 'Test approval prompt UI (for development/debugging)'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -s h -l help -d 'Print help'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -f -a "run-hook" -d 'Run a project hook for testing'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -f -a "commit" -d 'Commit changes with LLM-generated message'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -f -a "squash" -d 'Squash commits with LLM-generated message'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -f -a "push" -d 'Push changes to target branch'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -f -a "rebase" -d 'Rebase current branch onto target branch'
+complete -c wt -n "__fish_wt_using_subcommand dev; and not __fish_seen_subcommand_from run-hook commit squash push rebase ask-approvals" -f -a "ask-approvals" -d 'Approve commands in the project config (shows unapproved by default)'
complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from run-hook" -s f -l force -d 'Skip command approval prompts'
complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from run-hook" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from run-hook" -s h -l help -d 'Print help'
@@ -268,9 +268,10 @@ complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_fr
complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from push" -s h -l help -d 'Print help'
complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from rebase" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from rebase" -s h -l help -d 'Print help'
-complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approval" -s f -l force -d 'Skip command approval prompts'
-complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approval" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
-complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approval" -s h -l help -d 'Print help'
+complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approvals" -s f -l force -d 'Skip command approval prompts'
+complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approvals" -l all -d 'Show all commands including already-approved ones'
+complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approvals" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
+complete -c wt -n "__fish_wt_using_subcommand dev; and __fish_seen_subcommand_from ask-approvals" -s h -l help -d 'Print help'
complete -c wt -n "__fish_wt_using_subcommand list" -l format -d 'Output format' -r -f -a "table\t'Human-readable table format'
json\t'Machine-readable JSON with display fields (includes styled unicode for rendering)'"
complete -c wt -n "__fish_wt_using_subcommand list" -l branches -d 'Include branches without worktrees'
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 1192d65a4..ca9235ac8 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpT0vvwA/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptILqJ8/test-config.toml
---
success: true
exit_code: 0
@@ -293,10 +293,11 @@ _arguments "${_arguments_options[@]}" : \
'::target -- Target branch to rebase onto (defaults to default branch):_default' \
&& ret=0
;;
-(ask-approval)
+(ask-approvals)
_arguments "${_arguments_options[@]}" : \
'-f[Skip command approval prompts]' \
'--force[Skip command approval prompts]' \
+'--all[Show all commands including already-approved ones]' \
'-v[Enable verbose output (show git commands and debug info)]' \
'--verbose[Enable verbose output (show git commands and debug info)]' \
'-h[Print help]' \
@@ -455,14 +456,14 @@ _wt__dev_commands() {
'squash:Squash commits with LLM-generated message' \
'push:Push changes to target branch' \
'rebase:Rebase current branch onto target branch' \
-'ask-approval:Test approval prompt UI (for development/debugging)' \
+'ask-approvals:Approve commands in the project config (shows unapproved by default)' \
)
_describe -t commands 'wt dev commands' commands "$@"
}
-(( $+functions[_wt__dev__ask-approval_commands] )) ||
-_wt__dev__ask-approval_commands() {
+(( $+functions[_wt__dev__ask-approvals_commands] )) ||
+_wt__dev__ask-approvals_commands() {
local commands; commands=()
- _describe -t commands 'wt dev ask-approval commands' commands "$@"
+ _describe -t commands 'wt dev ask-approvals commands' commands "$@"
}
(( $+functions[_wt__dev__commit_commands] )) ||
_wt__dev__commit_commands() {