Skip to content

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

Auto-commit dirty worktrees before merge

Maximilian Roosa22b05de176d
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 191711e73..515e5b036 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -218,16 +218,10 @@ fn handle_commit_changes(
))
.map_err(|e| GitError::CommandFailed(e.to_string()))?;
- // Stage all tracked changes (excludes untracked files)
- repo.run_command(&["add", "-u"])
+ // Stage all changes including untracked files
+ repo.run_command(&["add", "-A"])
.map_err(|e| GitError::CommandFailed(format!("Failed to stage changes: {}", e)))?;
- // Check if there are staged changes after staging
- if !repo.has_staged_changes()? {
- // No staged changes means only untracked files exist
- return Err(GitError::UntrackedFiles);
- }
-
// Generate commit message
crate::output::progress(format!("πŸ”„ {CYAN}Generating commit message...{CYAN:#}"))
.map_err(|e| GitError::CommandFailed(e.to_string()))?;
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 802cd4ce8..d4d468631 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -380,6 +380,15 @@ pub fn setup_snapshot_settings(repo: &TestRepo) -> insta::Settings {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_path("../snapshots");
+ // Normalize project root path (for test fixtures)
+ // This must come before repo path filter to avoid partial matches
+ let project_root = std::env::var("CARGO_MANIFEST_DIR")
+ .ok()
+ .and_then(|p| std::path::PathBuf::from(p).canonicalize().ok());
+ if let Some(root) = project_root {
+ settings.add_filter(root.to_str().unwrap(), "[PROJECT_ROOT]");
+ }
+
// Normalize paths
settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
for (name, path) in &repo.worktrees {
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index e2afba597..000de3053 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -142,46 +142,6 @@ fn test_merge_dirty_working_tree() {
);
}
-#[test]
-fn test_merge_dirty_working_tree_error_message() {
- let mut repo = TestRepo::new();
- repo.commit("Initial commit");
- repo.setup_remote("main");
-
- // Create a feature worktree with uncommitted changes (untracked file)
- let feature_wt = repo.add_worktree("feature", "feature");
- std::fs::write(feature_wt.join("untracked.txt"), "uncommitted content")
- .expect("Failed to write file");
-
- // Run merge command and capture output
- let mut cmd = make_snapshot_cmd(&repo, "merge", &["main"], Some(&feature_wt));
- let output = cmd.output().expect("Failed to run command");
-
- // Should fail with exit code 1
- assert!(!output.status.success(), "Command should fail");
-
- // Convert stderr to string
- let stderr = String::from_utf8_lossy(&output.stderr);
-
- // CRITICAL: Error message must appear (styled with emoji/color)
- // Before the fix, this would fail silently with no error message
- // After the fix, structured errors are formatted in ONE canonical location (GitError::Display)
- // and printed by main.rs (the only place that prints errors)
- assert!(
- stderr.contains("Working tree has untracked files"),
- "Error output must contain the error message. Got stderr:\n{}",
- stderr
- );
-
- // Verify we see the error exactly once (not duplicated)
- let error_count = stderr.matches("Working tree has untracked files").count();
- assert_eq!(
- error_count, 1,
- "Should see error message exactly once from main.rs via GitError::Display. Got {} occurrences",
- error_count
- );
-}
-
#[test]
fn test_merge_not_fast_forward() {
let mut repo = TestRepo::new();
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
index bf72b98b5..ac6987708 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
@@ -15,14 +15,30 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDd1Xdj/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp41DHCB/test-config.toml
---
-success: false
-exit_code: 1
+success: true
+exit_code: 0
----- stdout -----
πŸ”„ Committing uncommitted changes...
+πŸ”„ Generating commit message...
+  WIP: Auto-commit before merge
+βœ… Committed changes
+πŸ”„ Rebasing onto main...
+πŸ”„ Pushing 1 commit to main @ [SHA]
------ stderr -----
-❌ Working tree has untracked files
+* 25858bd (HEAD -> feature) WIP: Auto-commit before merge
+
+ dirty.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]
-πŸ’‘ Add them with 'git add' and try again
+βœ… Merge complete
+
+Merged: feature β†’ main
+Worktree: Removed
+
+----- stderr -----
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
index 573fb3d1d..15bdd3e44 100644
--- 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
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpzP1ywc/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpgf2x1t/test-config.toml
---
success: true
exit_code: 0
@@ -32,7 +32,7 @@ exit_code: 0
πŸ”„ Rebasing onto main...
πŸ”„ Pushing 1 commit to main @ [SHA]
-* 268abe9 (HEAD -> feature) Add feature file
+* 061819f (HEAD -> feature) Add feature file
feature.txt | 1 +
1 file changed, 1 insertion(+)
@@ -48,14 +48,14 @@ exit_code: 0
----- stderr -----
πŸ”„ Running pre-merge check 'check1':
-  /Users/maximilian/workspace/worktrunk.stderr-out/tests/fixtures/mixed-output.sh
+  [PROJECT_ROOT]/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
+  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
  check2 3
[check2] stderr line 1
[check2] stderr line 2