Skip to content

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

feat: detect and report rebase conflicts during merge

Maximilian Roos83aea642b86c
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 515e5b036..d9d9797e8 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -81,9 +81,32 @@ pub fn handle_merge(
))
.map_err(|e| GitError::CommandFailed(e.to_string()))?;
- repo.run_command(&["rebase", &target_branch]).map_err(|e| {
- GitError::CommandFailed(format!("Failed to rebase onto '{}': {}", target_branch, e))
- })?;
+ let rebase_result = repo.run_command(&["rebase", &target_branch]);
+
+ // If rebase failed, check if it's due to conflicts
+ if let Err(e) = rebase_result {
+ if let Some(state) = repo.worktree_state()?
+ && state.starts_with("REBASING")
+ {
+ return Err(GitError::RebaseConflict {
+ state,
+ target_branch: target_branch.to_string(),
+ });
+ }
+ // Not a rebase conflict, return original error
+ return Err(GitError::CommandFailed(format!(
+ "Failed to rebase onto '{}': {}",
+ target_branch, e
+ )));
+ }
+
+ // Verify rebase completed successfully (safety check for edge cases)
+ if let Some(state) = repo.worktree_state()? {
+ return Err(GitError::RebaseConflict {
+ state,
+ target_branch: target_branch.to_string(),
+ });
+ }
// Fast-forward push to target branch (reuse handle_push logic)
handle_push(Some(&target_branch), false)?;
diff --git a/src/git.rs b/src/git.rs
index 661e488a9..aeff4c060 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -47,6 +47,11 @@ pub enum GitError {
CommandNotApproved,
/// Push operation failed
PushFailed { error: String },
+ /// Rebase resulted in a conflict or incomplete state
+ RebaseConflict {
+ state: String,
+ target_branch: String,
+ },
}
impl std::fmt::Display for GitError {
@@ -178,6 +183,18 @@ impl std::fmt::Display for GitError {
GitError::PushFailed { error } => {
write!(f, "{ERROR_EMOJI} {ERROR}Push failed: {error}{ERROR:#}")
}
+
+ // Rebase conflict
+ GitError::RebaseConflict {
+ state,
+ target_branch,
+ } => {
+ let error_bold = ERROR.bold();
+ write!(
+ f,
+ "{ERROR_EMOJI} {ERROR}Rebase onto {error_bold}{target_branch}{error_bold:#} incomplete: {state}{ERROR:#}\n\n{HINT_EMOJI} {HINT}Resolve conflicts and run 'git rebase --continue'{HINT:#}\n{HINT_EMOJI} {HINT}Or abort with 'git rebase --abort'{HINT:#}"
+ )
+ }
}
}
}
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 000de3053..00ef71be0 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -195,6 +195,99 @@ fn test_merge_not_fast_forward() {
);
}
+#[test]
+fn test_merge_rebase_conflict() {
+ let mut repo = TestRepo::new();
+
+ // Create a shared file
+ std::fs::write(repo.root_path().join("shared.txt"), "initial content\n")
+ .expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "shared.txt"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add file");
+
+ repo.commit("Add shared file");
+ repo.setup_remote("main");
+
+ // 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");
+
+ // Modify shared.txt in main branch (from the base commit)
+ std::fs::write(repo.root_path().join("shared.txt"), "main version\n")
+ .expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "shared.txt"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Update shared.txt in main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to commit");
+
+ // Create a feature worktree branching from before the main commit
+ // We need to create it from the original commit, not current main
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ let output = cmd
+ .args(["rev-parse", "HEAD~1"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to get previous commit");
+ let base_commit = String::from_utf8_lossy(&output.stdout).trim().to_string();
+
+ let feature_wt = repo.root_path().parent().unwrap().join("test-repo.feature");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args([
+ "worktree",
+ "add",
+ feature_wt.to_str().unwrap(),
+ "-b",
+ "feature",
+ &base_commit,
+ ])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add feature worktree");
+
+ // Modify the same file with conflicting content
+ std::fs::write(feature_wt.join("shared.txt"), "feature version\n")
+ .expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "shared.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", "Update shared.txt in feature"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Try to merge - should fail with rebase conflict
+ snapshot_merge("merge_rebase_conflict", &repo, &["main"], Some(&feature_wt));
+}
+
#[test]
fn test_merge_to_default_branch() {
let mut repo = TestRepo::new();
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 17f3aca88..08a614bd6 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/.tmp9QAkUl/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnHxfp0/test-config.toml
---
success: true
exit_code: 0
@@ -33,7 +33,7 @@ exit_code: 0
πŸ”„ Rebasing onto main...
πŸ”„ Pushing 1 commit to main @ [SHA]
-* a334407 (HEAD -> feature) Add feature file
+* 08f205e (HEAD -> feature) Add feature file
feature.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snap b/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snap
new file mode 100644
index 000000000..ddead2f0e
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snap
@@ -0,0 +1,30 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ 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/.tmpA0Yk8D/test-config.toml
+---
+success: false
+exit_code: 1
+----- stdout -----
+πŸ”„ Validating merge...
+πŸ”„ Rebasing onto main...
+
+----- stderr -----
+❌ Rebase onto main incomplete: REBASING 1/1
+
+πŸ’‘ Resolve conflicts and run 'git rebase --continue'
+πŸ’‘ Or abort with 'git rebase --abort'