Skip to content

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

feat: Add --no-config-commands to `wt switch`

Maximilian Roos3d5746de4dd0
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 0bd4fab1e..213323283 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -145,6 +145,7 @@ pub fn handle_switch(
create: bool,
base: Option<&str>,
force: bool,
+ no_config_commands: bool,
config: &WorktrunkConfig,
) -> Result<SwitchResult, GitError> {
let repo = Repository::current();
@@ -219,10 +220,14 @@ pub fn handle_switch(
.unwrap_or_else(|_| worktree_path.clone());
// Execute post-create commands (sequential, blocking)
- execute_post_create_commands(&canonical_path, &repo, config, branch, repo_name, force)?;
+ if !no_config_commands {
+ execute_post_create_commands(&canonical_path, &repo, config, branch, repo_name, force)?;
+ }
// Spawn post-start commands (parallel, background)
- spawn_post_start_commands(&canonical_path, &repo, config, branch, repo_name, force)?;
+ if !no_config_commands {
+ spawn_post_start_commands(&canonical_path, &repo, config, branch, repo_name, force)?;
+ }
Ok(SwitchResult::CreatedWorktree {
path: canonical_path,
diff --git a/src/main.rs b/src/main.rs
index 4be816c61..dd2885dda 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -93,6 +93,10 @@ enum Commands {
#[arg(short = 'f', long)]
force: bool,
+ /// Skip executing post-start commands from project config
+ #[arg(long)]
+ no_config_commands: bool,
+
/// Use internal mode (outputs directives for shell wrapper)
#[arg(long, hide = true)]
internal: bool,
@@ -214,11 +218,20 @@ fn main() {
base,
execute,
force,
+ no_config_commands,
internal,
} => WorktrunkConfig::load()
.map_err(|e| GitError::CommandFailed(format!("Failed to load config: {}", e)))
.and_then(|config| {
- handle_switch(&branch, create, base.as_deref(), force, &config).and_then(|result| {
+ handle_switch(
+ &branch,
+ create,
+ base.as_deref(),
+ force,
+ no_config_commands,
+ &config,
+ )
+ .and_then(|result| {
handle_switch_output(&result, &branch, execute.as_deref(), internal)
})
}),
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 88863dae3..f9e76eab9 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -1,11 +1,25 @@
use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
use insta_cmd::assert_cmd_snapshot;
+use std::path::Path;
/// Helper to create snapshot with normalized paths and SHAs
fn snapshot_switch(test_name: &str, repo: &TestRepo, args: &[&str]) {
+ snapshot_switch_with_home(test_name, repo, args, None);
+}
+
+/// Helper that also allows setting a custom HOME directory
+fn snapshot_switch_with_home(
+ test_name: &str,
+ repo: &TestRepo,
+ args: &[&str],
+ temp_home: Option<&Path>,
+) {
let settings = setup_snapshot_settings(repo);
settings.bind(|| {
let mut cmd = make_snapshot_cmd(repo, "switch", args, None);
+ if let Some(home) = temp_home {
+ cmd.env("HOME", home);
+ }
assert_cmd_snapshot!(test_name, cmd);
});
}
@@ -214,3 +228,130 @@ fn test_switch_execute_multiline() {
&["--create", "multiline-test", "--execute", multiline_cmd],
);
}
+
+#[test]
+fn test_switch_no_config_commands_execute_still_runs() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ snapshot_switch(
+ "switch_no_config_commands_execute_still_runs",
+ &repo,
+ &[
+ "--create",
+ "no-config-commands-test",
+ "--execute",
+ "echo 'execute command runs'",
+ "--no-config-commands",
+ ],
+ );
+}
+
+#[test]
+fn test_switch_no_config_commands_skips_post_start_commands() {
+ use std::fs;
+ use tempfile::TempDir;
+
+ let temp_home = TempDir::new().unwrap();
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create project config with a command that would create a file
+ let config_dir = repo.root_path().join(".config");
+ fs::create_dir_all(&config_dir).expect("Failed to create .config dir");
+
+ #[cfg(target_os = "windows")]
+ let create_file_cmd = "echo marker > marker.txt";
+ #[cfg(not(target_os = "windows"))]
+ let create_file_cmd = "echo 'marker' > marker.txt";
+
+ fs::write(
+ config_dir.join("wt.toml"),
+ format!(r#"post-start-commands = ["{}"]"#, create_file_cmd),
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add config");
+
+ // Pre-approve the command
+ let user_config_dir = temp_home
+ .path()
+ .join("Library/Application Support/worktrunk");
+ fs::create_dir_all(&user_config_dir).expect("Failed to create user config dir");
+ fs::write(
+ user_config_dir.join("config.toml"),
+ format!(
+ r#"worktree-path = "../{{repo}}.{{branch}}"
+
+[[approved-commands]]
+project = "main"
+command = "{}"
+"#,
+ create_file_cmd
+ ),
+ )
+ .expect("Failed to write user config");
+
+ // With --no-config-commands, the post-start command should be skipped
+ snapshot_switch_with_home(
+ "switch_no_config_commands_skips_post_start",
+ &repo,
+ &["--create", "no-post-start", "--no-config-commands"],
+ Some(temp_home.path()),
+ );
+}
+
+#[test]
+fn test_switch_no_config_commands_with_existing_worktree() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create a worktree first
+ repo.add_worktree("existing-no-config-commands", "existing-no-config-commands");
+
+ // With --no-config-commands, the --execute command should still run
+ snapshot_switch(
+ "switch_no_config_commands_existing",
+ &repo,
+ &[
+ "existing-no-config-commands",
+ "--execute",
+ "echo 'execute still runs'",
+ "--no-config-commands",
+ ],
+ );
+}
+
+#[test]
+fn test_switch_no_config_commands_with_force() {
+ use std::fs;
+ use tempfile::TempDir;
+
+ let temp_home = TempDir::new().unwrap();
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create project config with a command
+ let config_dir = repo.root_path().join(".config");
+ fs::create_dir_all(&config_dir).expect("Failed to create .config dir");
+ fs::write(
+ config_dir.join("wt.toml"),
+ r#"post-start-commands = ["echo 'test'"]"#,
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add config");
+
+ // With --no-config-commands, even --force shouldn't execute config commands
+ snapshot_switch_with_home(
+ "switch_no_config_commands_with_force",
+ &repo,
+ &[
+ "--create",
+ "force-no-config-commands",
+ "--force",
+ "--no-config-commands",
+ ],
+ Some(temp_home.path()),
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 14df1811e..9fd260576 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -298,7 +298,7 @@ _wt() {
return 0
;;
wt__switch)
- opts="-c -b -x -f -h --create --base --execute --force --help <BRANCH>"
+ opts="-c -b -x -f -h --create --base --execute --force --no-config-commands --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 c66ef9334..977110e65 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
@@ -300,7 +300,7 @@ _wt() {
return 0
;;
wt__switch)
- opts="-c -b -x -f -h --create --base --execute --force --help <BRANCH>"
+ opts="-c -b -x -f -h --create --base --execute --force --no-config-commands --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 03d358b8f..63cabc1ae 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -166,6 +166,7 @@ complete -c wt -n "__fish_wt_using_subcommand switch" -s b -l base -d 'Base bran
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" -l no-config-commands -d 'Skip executing post-start commands from project config'
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_oil.snap b/tests/snapshots/integration__integration_tests__init__init_oil.snap
index a1ed52fc7..b8ecb6dfe 100644
--- a/tests/snapshots/integration__integration_tests__init__init_oil.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_oil.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 --no-hooks --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_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 845605964..8e5c92f8e 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -171,6 +171,7 @@ _arguments "${_arguments_options[@]}" : \
'--create[Create a new branch]' \
'-f[Skip confirmation prompt]' \
'--force[Skip confirmation prompt]' \
+'--no-config-commands[Skip executing post-start commands from project config]' \
'-h[Print help]' \
'--help[Print help]' \
':branch -- Branch name or worktree path:_default' \
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_execute_still_runs.snap b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_execute_still_runs.snap
new file mode 100644
index 000000000..6880b2f10
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_execute_still_runs.snap
@@ -0,0 +1,31 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - no-config-commands-test
+ - "--execute"
+ - "echo 'execute command runs'"
+ - "--no-config-commands"
+ env:
+ CLICOLOR_FORCE: "1"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+βœ… Created new worktree for no-config-commands-test
+ Path: [REPO].no-config-commands-test
+
+execute command runs
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_existing.snap b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_existing.snap
new file mode 100644
index 000000000..2433d9f82
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_existing.snap
@@ -0,0 +1,29 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - existing-no-config-commands
+ - "--execute"
+ - "echo 'execute still runs'"
+ - "--no-config-commands"
+ env:
+ CLICOLOR_FORCE: "1"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+βœ… Switched to worktree for existing-no-config-commands
+
+execute still runs
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_skips_post_start.snap b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_skips_post_start.snap
new file mode 100644
index 000000000..672cd0ec3
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_skips_post_start.snap
@@ -0,0 +1,30 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - no-post-start
+ - "--no-config-commands"
+ env:
+ CLICOLOR_FORCE: "1"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpBQEtm6
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+βœ… Created new worktree for no-post-start
+ Path: [REPO].no-post-start
+
+To enable automatic cd, run: wt configure-shell
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_with_force.snap b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_with_force.snap
new file mode 100644
index 000000000..891252fdf
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_no_config_commands_with_force.snap
@@ -0,0 +1,31 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - force-no-config-commands
+ - "--force"
+ - "--no-config-commands"
+ env:
+ CLICOLOR_FORCE: "1"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpZHsoXp
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+βœ… Created new worktree for force-no-config-commands
+ Path: [REPO].force-no-config-commands
+
+To enable automatic cd, run: wt configure-shell
+
+----- stderr -----