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.rsindex 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.rsindex 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.snapnew file mode 100644index 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 -----+π [36mExecuting (post-create):[0m+[40m [0m if [ ! -f test.txt ]; then+[40m [0m echo 'File does not exist' > result.txt+[40m [0m else+[40m [0m echo 'File exists' > result.txt+[40m [0m fi++β [32mCreated new worktree for [1m[32mfeature[0m at [REPO].feature[0m++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.snapnew file mode 100644index 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 -----+π [36mStarting (background):[0m+[40m [0m echo 'first line' > multiline.txt+[40m [0m echo 'second line' >> multiline.txt+[40m [0m echo 'third line' >> multiline.txt++β [32mCreated new worktree for [1m[32mfeature[0m at [REPO].feature[0m++To enable automatic cd, run: wt configure-shell++----- stderr -----