Skip to content

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

Flush output after pre-merge commands

Maximilian Roosbd23af821d7b
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index d488bbcd5..2a7ca1a34 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -368,6 +368,10 @@ fn run_pre_merge_checks(
error: e.to_string(),
});
}
+
+ // Flush both streams to ensure command output appears before next progress message
+ let _ = std::io::stdout().flush();
+ let _ = std::io::stderr().flush();
}
Ok(())
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 559c8adba..802cd4ce8 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -450,6 +450,44 @@ pub fn make_snapshot_cmd(
make_snapshot_cmd_with_global_flags(repo, subcommand, args, cwd, &[])
}
+/// Run a command and capture combined stdout+stderr output for snapshot testing
+///
+/// This mimics real terminal usage where stdout and stderr are interleaved (like `2>&1`).
+/// Returns the combined output as a String with exit code information.
+pub fn run_with_combined_output(
+ repo: &TestRepo,
+ subcommand: &str,
+ args: &[&str],
+ cwd: Option<&Path>,
+) -> String {
+ use std::process::Stdio;
+
+ // Build the command with all arguments
+ let wt_bin = insta_cmd::get_cargo_bin("wt");
+ let mut cmd_parts = vec![wt_bin.to_str().unwrap(), subcommand];
+ cmd_parts.extend(args.iter());
+
+ // Run through shell with 2>&1 to get true interleaved output
+ let cmd_str = cmd_parts.join(" ");
+ let shell_cmd = format!("{} 2>&1", cmd_str);
+
+ let mut cmd = Command::new("sh");
+ repo.clean_cli_env(&mut cmd);
+ cmd.arg("-c")
+ .arg(&shell_cmd)
+ .current_dir(cwd.unwrap_or(repo.root_path()))
+ .stdout(Stdio::piped());
+
+ let output = cmd.output().expect("Failed to execute command");
+ let combined = String::from_utf8_lossy(&output.stdout).to_string();
+
+ format!(
+ "Exit code: {}\n{}",
+ output.status.code().unwrap_or(-1),
+ combined
+ )
+}
+
/// Resolve the actual git directory path from a worktree path
///
/// In worktrees, `.git` is a file containing `gitdir: /path/to/git/dir`,
diff --git a/tests/fixtures/mixed-output.sh b/tests/fixtures/mixed-output.sh
new file mode 100755
index 000000000..adb3de467
--- /dev/null
+++ b/tests/fixtures/mixed-output.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+# Test script that outputs to both stdout and stderr
+# Usage: mixed-output.sh <name> <count>
+
+NAME="${1:-test}"
+COUNT="${2:-5}"
+
+for i in $(seq 1 "$COUNT"); do
+ echo "[$NAME] stdout line $i"
+ echo "[$NAME] stderr line $i" >&2
+done
+
+echo "[$NAME] stdout complete"
+echo "[$NAME] stderr complete" >&2
diff --git a/tests/fixtures/ordered-output.sh b/tests/fixtures/ordered-output.sh
new file mode 100755
index 000000000..f04c7bf28
--- /dev/null
+++ b/tests/fixtures/ordered-output.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+# Test script that outputs markers in a specific order to verify stream ordering
+# Usage: ordered-output.sh <prefix>
+
+PREFIX="${1:-test}"
+
+echo "STDOUT-START:$PREFIX"
+echo "STDOUT-1:$PREFIX" >&2
+echo "STDOUT-2:$PREFIX"
+echo "STDERR-1:$PREFIX" >&2
+echo "STDOUT-3:$PREFIX"
+echo "STDERR-2:$PREFIX" >&2
+echo "STDOUT-END:$PREFIX"
+echo "STDERR-END:$PREFIX" >&2
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 5cacb41d2..e2afba597 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -1,6 +1,7 @@
use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
use insta_cmd::assert_cmd_snapshot;
use std::fs;
+use std::path::PathBuf;
use std::process::Command;
/// Helper to create snapshot with normalized paths
@@ -953,3 +954,134 @@ test = "exit 0"
Some(&feature_wt),
);
}
+
+#[test]
+fn test_merge_pre_merge_check_stdout_stderr_ordering() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Get path to the test fixture script
+ let fixtures_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
+ let script_path = fixtures_dir.join("mixed-output.sh");
+
+ // Create project config with two named pre-merge checks that both output to stdout and stderr
+ 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"),
+ format!(
+ r#"
+[pre-merge-check]
+check1 = "{} check1 3"
+check2 = "{} check2 3"
+"#,
+ script_path.display(),
+ script_path.display()
+ ),
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add config");
+
+ // Create a worktree for main
+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add worktree");
+
+ // Create a feature worktree and make a commit
+ let feature_wt = repo.add_worktree("feature", "feature");
+ fs::write(feature_wt.join("feature.txt"), "feature content").expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "feature.txt"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to add file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Add feature file"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Merge with --force - verify output ordering
+ // Expected behavior in real usage (with 2>&1):
+ // check1 header → all check1 output → check2 header → all check2 output
+ //
+ // Note: The snapshot separates stdout/stderr for display, but in practice
+ // they're properly interleaved when both go to the terminal.
+ // The important verification is that within each stream, the ordering is correct.
+ snapshot_merge(
+ "merge_pre_merge_check_stdout_stderr_ordering",
+ &repo,
+ &["main", "--force"],
+ Some(&feature_wt),
+ );
+}
+
+#[test]
+fn test_merge_pre_merge_check_combined_output() {
+ use crate::common::run_with_combined_output;
+
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Create project config with two named pre-merge checks that output to BOTH stdout and stderr
+ // This verifies the ordering is: header1 → command1 → stdout1 → stderr1 → header2 → command2 → stdout2 → stderr2
+ 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#"
+[pre-merge-check]
+first = "echo 'STDOUT-FROM-FIRST' && echo 'STDERR-FROM-FIRST' >&2"
+second = "echo 'STDOUT-FROM-SECOND' && echo 'STDERR-FROM-SECOND' >&2"
+"#,
+ )
+ .expect("Failed to write config");
+
+ repo.commit("Add config");
+
+ // Create a worktree for main
+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add worktree");
+
+ // Create a feature worktree and make a commit
+ let feature_wt = repo.add_worktree("feature", "feature");
+ fs::write(feature_wt.join("feature.txt"), "feature content").expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "feature.txt"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to add file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Add feature file"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Run merge and capture combined output
+ let output = run_with_combined_output(&repo, "merge", &["main", "--force"], Some(&feature_wt));
+
+ let settings = crate::common::setup_snapshot_settings(&repo);
+ settings.bind(|| {
+ insta::assert_snapshot!("merge_pre_merge_check_combined_output", output);
+ });
+}
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_combined_output.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_combined_output.snap
new file mode 100644
index 000000000..6e4d0dfde
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_combined_output.snap
@@ -0,0 +1,29 @@
+---
+source: tests/integration_tests/merge.rs
+expression: output
+---
+Exit code: 0
+🔄 Running pre-merge check 'first':
+  echo 'STDOUT-FROM-FIRST' && echo 'STDERR-FROM-FIRST' >&2
+STDOUT-FROM-FIRST
+STDERR-FROM-FIRST
+🔄 Running pre-merge check 'second':
+  echo 'STDOUT-FROM-SECOND' && echo 'STDERR-FROM-SECOND' >&2
+STDOUT-FROM-SECOND
+STDERR-FROM-SECOND
+🔄 Rebasing onto main...
+🔄 Pushing 1 commit to main @ [SHA]
+
+* d8375ea (HEAD -> feature) Add feature file
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+✅ Pushed to main (1 commit, 1 file, +1) 
+🔄 Cleaning up worktree...
+✅ Removed worktree, returned to primary at [REPO]
+
+✅ Merge complete
+
+Merged: feature → main
+Worktree: Removed
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snap
new file mode 100644
index 000000000..573fb3d1d
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snap
@@ -0,0 +1,63 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ - "--force"
+ 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: "1761609600"
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpzP1ywc/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+[check1] stdout line 1
+[check1] stdout line 2
+[check1] stdout line 3
+[check1] stdout complete
+[check2] stdout line 1
+[check2] stdout line 2
+[check2] stdout line 3
+[check2] stdout complete
+🔄 Rebasing onto main...
+🔄 Pushing 1 commit to main @ [SHA]
+
+* 268abe9 (HEAD -> feature) Add feature file
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+✅ Pushed to main (1 commit, 1 file, +1) 
+🔄 Cleaning up worktree...
+✅ Removed worktree, returned to primary at [REPO]
+
+✅ Merge complete
+
+Merged: feature → main
+Worktree: Removed
+
+----- stderr -----
+🔄 Running pre-merge check 'check1':
+  /Users/maximilian/workspace/worktrunk.stderr-out/tests/fixtures/mixed-output.sh
+  check1 3
+[check1] stderr line 1
+[check1] stderr line 2
+[check1] stderr line 3
+[check1] stderr complete
+🔄 Running pre-merge check 'check2':
+  /Users/maximilian/workspace/worktrunk.stderr-out/tests/fixtures/mixed-output.sh
+  check2 3
+[check2] stderr line 1
+[check2] stderr line 2
+[check2] stderr line 3
+[check2] stderr complete