Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(approvals): don't read approvals.toml under --yes (#4467)
Worktrunk Bote217429a54b0
diff --git a/src/commands/command_approval.rs b/src/commands/command_approval.rsindex c5fecf50a..a6ab39bfa 100644--- a/src/commands/command_approval.rs+++ b/src/commands/command_approval.rs@@ -43,61 +43,50 @@ use worktrunk::config::HookSource;/// `yes` grants consent for this invocation alone: nothing is recorded, so the/// next run asks again. Recording without a prompt is `wt config approvals/// add --yes`'s job ([`crate::commands::add_approvals`]), which doesn't pass-/// through this gate.-///-/// # Parameters-/// - `commands_already_filtered`: If true, commands list is pre-filtered; skip filtering by approval status+/// through this gate. Because `yes` neither reads nor records approvals,+/// `approvals.toml` is loaded only on the interactive path, so a malformed+/// file never aborts a `--yes` run.pub fn approve_command_batch(commands: &[ApprovableCommand],project_id: &str,- approvals: &Approvals,yes: bool,- commands_already_filtered: bool,) -> anyhow::Result<bool> {+ if yes {+ return Ok(true);+ }++ let mut approvals = Approvals::load().context("Failed to load approvals")?;let needs_approval: Vec<&ApprovableCommand> = commands.iter()- .filter(|cmd| {- commands_already_filtered- || !approvals.is_command_approved(project_id, &cmd.command.template)- })+ .filter(|cmd| !approvals.is_command_approved(project_id, &cmd.command.template)).collect();if needs_approval.is_empty() {return Ok(true);}- let approved = if yes {- true- } else {- prompt_for_batch_approval(&needs_approval, project_id)?- };-- if !approved {+ if !prompt_for_batch_approval(&needs_approval, project_id)? {return Ok(false);}- // Only save approvals when interactively approved, not when using --yes.// A failed save costs one more prompt next run, not the command the user- // actually ran, so it warns rather than errors.- if !yes {- let mut fresh_approvals = Approvals::load().context("Failed to load approvals")?;- let commands: Vec<String> = needs_approval- .iter()- .map(|cmd| cmd.command.template.clone())- .collect();- let save_result = require_approvals_path().and_then(|path| {- fresh_approvals.approve_commands(project_id.to_string(), commands, &path)- });- if let Err(e) = save_result {- eprintln!(- "{}",- warning_message(format!("Failed to save command approval: {e}"))- );- eprintln!(- "{}",- hint_message("Approval will be requested again next time")- );- }+ // actually ran, so it warns rather than errors. `approve_commands` reloads+ // under the lock, so approvals saved since the load above are kept.+ let commands: Vec<String> = needs_approval+ .iter()+ .map(|cmd| cmd.command.template.clone())+ .collect();+ let save_result = require_approvals_path()+ .and_then(|path| approvals.approve_commands(project_id.to_string(), commands, &path));+ if let Err(e) = save_result {+ eprintln!(+ "{}",+ warning_message(format!("Failed to save command approval: {e}"))+ );+ eprintln!(+ "{}",+ hint_message("Approval will be requested again next time")+ );}Ok(true)@@ -211,8 +200,6 @@ pub fn approve_alias_commands(project_id: &str,yes: bool,) -> anyhow::Result<bool> {- let approvals = Approvals::load().context("Failed to load approvals")?;-let cmds: Vec<_> = commands.commands().map(|cmd| ApprovableCommand {@@ -224,7 +211,7 @@ pub fn approve_alias_commands(}).collect();- approve_command_batch(&cmds, project_id, &approvals, yes, false)+ approve_command_batch(&cmds, project_id, yes)}/// Collect project commands for hooks and request batch approval.@@ -305,8 +292,7 @@ pub fn approve_hooks_filtered(}let project_id = ctx.repo.project_identifier()?;- let approvals = Approvals::load().context("Failed to load approvals")?;- approve_command_batch(&commands, &project_id, &approvals, ctx.yes, false)+ approve_command_batch(&commands, &project_id, ctx.yes)}/// Approve `hook_types` and centralize the "decline → continue without hooks" message.@@ -371,14 +357,9 @@ pub fn approve_commit_template_append(};let project_id = ctx.repo.project_identifier()?;- let approvals = Approvals::load().context("Failed to load approvals")?;let owned = fragment.to_string();- if approvals.is_command_approved(&project_id, &owned) {- return Ok(Some(owned));- }-let batch = vec![ApprovableCommand::commit_template_append(owned.clone())];- let approved = approve_command_batch(&batch, &project_id, &approvals, ctx.yes, true)?;+ let approved = approve_command_batch(&batch, &project_id, ctx.yes)?;if !approved {worktrunk::styling::eprintln!("{}",diff --git a/src/commands/hook_plan.rs b/src/commands/hook_plan.rsindex 33f25a464..d176febbb 100644--- a/src/commands/hook_plan.rs+++ b/src/commands/hook_plan.rs@@ -214,14 +214,7 @@ impl HookPlan {}let project_id =project_id.context("project identifier is required to approve project commands")?;- // `approve_command_batch` with `yes = true` is an unconditional- // `Ok(true)`, so loading `Approvals` here would be dead work.- let approved = if yes {- true- } else {- let approvals = Approvals::load().context("Failed to load approvals")?;- approve_command_batch(&approvable, project_id, &approvals, false, false)?- };+ let approved = approve_command_batch(&approvable, project_id, yes)?;if !approved {return Ok(None);}diff --git a/tests/integration_tests/approvals.rs b/tests/integration_tests/approvals.rsindex 171eb88c5..ab04cc772 100644--- a/tests/integration_tests/approvals.rs+++ b/tests/integration_tests/approvals.rs@@ -817,3 +817,35 @@ fn test_approvals_malformed_file(repo: TestRepo, #[case] args: &[&str]) {contents);}++/// `--yes` approves without reading `approvals.toml`, so a malformed file must+/// not abort a command run with it — the same rule `wt switch` and `wt remove`+/// follow through their hook plans. The file stays untouched.+#[rstest]+#[case::hook(&["hook", "pre-merge", "--yes"])]+#[case::alias(&["--yes", "mark"])]+fn test_yes_ignores_malformed_approvals_file(repo: TestRepo, #[case] args: &[&str]) {+ let contents = "[projects\n";+ fs::write(repo.test_approvals_path(), contents).unwrap();+ repo.write_project_config(+ r#"pre-merge = "echo ran > marker.txt"++[aliases]+mark = "echo ran > marker.txt"+"#,+ );+ repo.commit("Add project commands");++ let output = repo.wt_command().args(args).output().unwrap();+ assert!(+ output.status.success(),+ "wt {} failed: {}",+ args.join(" "),+ String::from_utf8_lossy(&output.stderr)+ );+ assert!(repo.root_path().join("marker.txt").exists());+ assert_eq!(+ fs::read_to_string(repo.test_approvals_path()).unwrap(),+ contents+ );+}