Skip to content

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

Feat: Add --force flag to `switch` command

Maximilian Roose24ba3c953f4
diff --git a/CLAUDE.md b/CLAUDE.md
index ce4d40901..d9e5a8f0a 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -179,3 +179,22 @@ println!("{}", line.render());
```
See `src/commands/list/render.rs` for advanced usage.
+
+## Testing Guidelines
+
+### Testing with --execute Commands
+
+When testing commands that require confirmation (e.g., `wt switch --execute "..."`), use the `--force` flag to skip the interactive prompt:
+
+```bash
+# Good - skips confirmation prompt for testing
+wt switch --create feature --execute "echo test" --force
+
+# Bad - DO NOT pipe 'yes' to stdin, this crashes Claude
+echo yes | wt switch --create feature --execute "echo test"
+```
+
+**Why `--force`?**
+- Non-interactive testing requires automated approval
+- Piping input to stdin interferes with Claude's I/O handling
+- `--force` provides explicit, testable behavior
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 8d4375fdf..9f9bc555f 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -238,6 +238,7 @@ pub fn handle_switch(
create: bool,
base: Option<&str>,
execute: Option<&str>,
+ force: bool,
config: &WorktrunkConfig,
) -> Result<SwitchResult, GitError> {
let repo = Repository::current();
@@ -315,7 +316,7 @@ pub fn handle_switch(
.unwrap_or_else(|_| worktree_path.clone());
// Execute post-start commands from project config (before -x command)
- execute_post_start_commands(&canonical_path, &repo, config, branch, repo_name)?;
+ execute_post_start_commands(&canonical_path, &repo, config, branch, repo_name, force)?;
// Execute -x command last (after setup is complete)
if let Some(cmd) = execute {
@@ -489,7 +490,7 @@ fn prompt_for_approval(command: &str, project_id: &str) -> io::Result<bool> {
use anstyle::Style;
// Extract project name from project_id (e.g., "worktrunk" from "github.com/max-sixty/worktrunk")
- let project_name = project_id.split('/').last().unwrap_or(project_id);
+ let project_name = project_id.split('/').next_back().unwrap_or(project_id);
let bold = Style::new().bold();
let dim = Style::new().dimmed();
@@ -517,6 +518,7 @@ fn execute_post_start_commands(
config: &WorktrunkConfig,
branch: &str,
repo_name: &str,
+ force: bool,
) -> Result<(), GitError> {
// Load project config
let repo_root = repo.repo_root()?;
@@ -546,8 +548,8 @@ fn execute_post_start_commands(
// Execute each command
for command in &project_config.post_start_commands {
- // Check if command is already approved
- let approved = if config.is_command_approved(&project_id, command) {
+ // Check if command is already approved or if force flag is set
+ let approved = if force || config.is_command_approved(&project_id, command) {
true
} else {
// Prompt for approval
@@ -775,10 +777,10 @@ fn parse_diff_shortstat(output: &str) -> DiffStats {
if let Some(num_str) = part.split_whitespace().next() {
stats.insertions = num_str.parse().ok();
}
- } else if part.contains("deletion") {
- if let Some(num_str) = part.split_whitespace().next() {
- stats.deletions = num_str.parse().ok();
- }
+ } else if part.contains("deletion")
+ && let Some(num_str) = part.split_whitespace().next()
+ {
+ stats.deletions = num_str.parse().ok();
}
}
diff --git a/src/main.rs b/src/main.rs
index 38579f49c..5d26c8dde 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -87,6 +87,10 @@ enum Commands {
#[arg(short = 'x', long)]
execute: Option<String>,
+ /// Skip confirmation prompt
+ #[arg(short = 'f', long)]
+ force: bool,
+
/// Use internal mode (outputs directives for shell wrapper)
#[arg(long, hide = true)]
internal: bool,
@@ -207,6 +211,7 @@ fn main() {
create,
base,
execute,
+ force,
internal,
} => WorktrunkConfig::load()
.map_err(|e| GitError::CommandFailed(format!("Failed to load config: {}", e)))
@@ -216,6 +221,7 @@ fn main() {
create,
base.as_deref(),
execute.as_deref(),
+ force,
&config,
)
.map(|result| {
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 5627e2864..40b703b48 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -268,7 +268,7 @@ _wt() {
return 0
;;
wt__switch)
- opts="-c -b -x -h --create --base --execute --help <BRANCH>"
+ opts="-c -b -x -f -h --create --base --execute --force --help <BRANCH>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap
index 5065187ad..601f95e4a 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap
@@ -270,7 +270,7 @@ _wt() {
return 0
;;
wt__switch)
- opts="-c -b -x -h --create --base --execute --help <BRANCH>"
+ opts="-c -b -x -f -h --create --base --execute --force --help <BRANCH>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; 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 e2d47b7f1..7eb0a0934 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -125,6 +125,7 @@ complete -c wt -n "__fish_wt_using_subcommand list" -s h -l help -d 'Print help
complete -c wt -n "__fish_wt_using_subcommand switch" -s b -l base -d 'Base branch to create from (only with --create)' -r
complete -c wt -n "__fish_wt_using_subcommand switch" -s x -l execute -d 'Execute command after switching' -r
complete -c wt -n "__fish_wt_using_subcommand switch" -s c -l create -d 'Create a new branch'
+complete -c wt -n "__fish_wt_using_subcommand switch" -s f -l force -d 'Skip confirmation prompt'
complete -c wt -n "__fish_wt_using_subcommand switch" -s h -l help -d 'Print help'
complete -c wt -n "__fish_wt_using_subcommand remove" -s h -l help -d 'Print help'
complete -c wt -n "__fish_wt_using_subcommand push" -l allow-merge-commits -d 'Allow pushing merge commits (non-linear history)'
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 36b69d726..f972cc615 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -139,6 +139,8 @@ _arguments "${_arguments_options[@]}" : \
'--execute=[Execute command after switching]:EXECUTE:_default' \
'-c[Create a new branch]' \
'--create[Create a new branch]' \
+'-f[Skip confirmation prompt]' \
+'--force[Skip confirmation prompt]' \
'-h[Print help]' \
'--help[Print help]' \
':branch -- Branch name or worktree path:_default' \