Skip to content

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

Support multi-line shell commands in command execution

Maximilian Roos37c51276ef2e
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index ccece6b4d..51b07f9b3 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -137,7 +137,9 @@ pub(crate) fn execute_streaming(
use std::process::Command;
let command_to_run = if redirect_stdout_to_stderr {
- format!("{{ {}; }} 1>&2", command)
+ // Use newline instead of semicolon before closing brace to support
+ // multi-line commands with control structures (if/fi, for/done, etc.)
+ format!("{{ {}\n}} 1>&2", command)
} else {
command.to_string()
};
diff --git a/tests/integration_tests/post_start_commands.rs b/tests/integration_tests/post_start_commands.rs
index 3dbc90a9d..99ecf46ad 100644
--- a/tests/integration_tests/post_start_commands.rs
+++ b/tests/integration_tests/post_start_commands.rs
@@ -774,3 +774,132 @@ command = "echo 'line1\nline2\nline3' | grep line2 > filtered.txt"
let contents = fs::read_to_string(&filtered_file).expect("Failed to read filtered.txt");
assert_snapshot!(contents, @"line2");
}
+
+#[test]
+fn test_post_start_multiline_commands_with_newlines() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create command with actual newlines (using TOML triple-quoted string)
+ 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-command = """
+echo 'first line' > multiline.txt
+echo 'second line' >> multiline.txt
+echo 'third line' >> multiline.txt
+"""
+"#,
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add multiline command with actual newlines");
+
+ // Pre-approve the command
+ let multiline_cmd = "echo 'first line' > multiline.txt
+echo 'second line' >> multiline.txt
+echo 'third line' >> multiline.txt
+";
+ fs::write(
+ repo.test_config_path(),
+ format!(
+ r#"worktree-path = "../{{main-worktree}}.{{branch}}"
+
+[[approved-commands]]
+project = "test-repo"
+command = """
+{}"""
+"#,
+ multiline_cmd
+ ),
+ )
+ .expect("Failed to write user config");
+
+ snapshot_switch(
+ "post_start_multiline_with_newlines",
+ &repo,
+ &["--create", "feature"],
+ );
+
+ // Wait for background command
+ thread::sleep(SLEEP_FAST_COMMAND);
+
+ // Verify the multiline command worked correctly
+ let worktree_path = repo.root_path().parent().unwrap().join("test-repo.feature");
+ let output_file = worktree_path.join("multiline.txt");
+ assert!(
+ output_file.exists(),
+ "Multiline command should create output file"
+ );
+
+ let contents = fs::read_to_string(&output_file).expect("Failed to read multiline.txt");
+ assert_snapshot!(contents, @r"
+ first line
+ second line
+ third line
+ ");
+}
+
+#[test]
+fn test_post_create_multiline_with_control_structures() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Test multiline command with if-else control structure
+ 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-create-command = """
+if [ ! -f test.txt ]; then
+ echo 'File does not exist' > result.txt
+else
+ echo 'File exists' > result.txt
+fi
+"""
+"#,
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add multiline control structure");
+
+ // Pre-approve the command
+ let multiline_cmd = "if [ ! -f test.txt ]; then
+ echo 'File does not exist' > result.txt
+else
+ echo 'File exists' > result.txt
+fi
+";
+ fs::write(
+ repo.test_config_path(),
+ format!(
+ r#"worktree-path = "../{{main-worktree}}.{{branch}}"
+
+[[approved-commands]]
+project = "test-repo"
+command = """
+{}"""
+"#,
+ multiline_cmd
+ ),
+ )
+ .expect("Failed to write user config");
+
+ snapshot_switch(
+ "post_create_multiline_control_structure",
+ &repo,
+ &["--create", "feature"],
+ );
+
+ // Verify the command executed correctly
+ let worktree_path = repo.root_path().parent().unwrap().join("test-repo.feature");
+ let result_file = worktree_path.join("result.txt");
+ assert!(
+ result_file.exists(),
+ "Control structure command should create result file"
+ );
+
+ let contents = fs::read_to_string(&result_file).expect("Failed to read result.txt");
+ assert_snapshot!(contents, @"File does not exist");
+}
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiline_control_structure.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiline_control_structure.snap
new file mode 100644
index 000000000..bdeb2e81e
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiline_control_structure.snap
@@ -0,0 +1,36 @@
+---
+source: tests/integration_tests/post_start_commands.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feature
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ 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: "1761609600"
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpTA5Adn/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+πŸ”„ Executing (post-create):
+  if [ ! -f test.txt ]; then
+  echo 'File does not exist' > result.txt
+  else
+  echo 'File exists' > result.txt
+  fi
+
+βœ… Created new worktree for feature at [REPO].feature
+
+To enable automatic cd, run: wt configure-shell
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap
new file mode 100644
index 000000000..b0f1dcbf3
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap
@@ -0,0 +1,34 @@
+---
+source: tests/integration_tests/post_start_commands.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feature
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ 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: "1761609600"
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpLo32Wc/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+πŸ”„ Starting (background):
+  echo 'first line' > multiline.txt
+  echo 'second line' >> multiline.txt
+  echo 'third line' >> multiline.txt
+
+βœ… Created new worktree for feature at [REPO].feature
+
+To enable automatic cd, run: wt configure-shell
+
+----- stderr -----