Skip to content

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

feat: Use -d for safer branch deletion

Maximilian Roos7e37bbc18bf6
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index efcb1938a..75eaea5bd 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -271,6 +271,10 @@ pub fn handle_merge(
let worktree_root = repo.worktree_root()?;
repo.remove_worktree(&worktree_root)
.git_context("Failed to remove worktree")?;
+ // Use -d (safe delete) instead of -D to protect against race conditions:
+ // If someone commits to the branch between our push and this deletion,
+ // -d will refuse to delete, preventing data loss.
+ // See test: test_merge_race_condition_commit_after_push
primary_repo
.run_command(&["branch", "-d", &current_branch])
.git_context(&format!("Failed to delete branch '{}'", current_branch))?;
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 99de0d7e7..d1b374c66 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -2241,3 +2241,111 @@ fn test_merge_primary_on_different_branch_dirty() {
Some(&feature_wt),
);
}
+
+#[test]
+fn test_merge_race_condition_commit_after_push() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ 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");
+
+ // 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 to main (this pushes the branch to main)
+ snapshot_merge(
+ "merge_race_condition_before_new_commit",
+ &repo,
+ &["main", "--no-remove"],
+ Some(&feature_wt),
+ );
+
+ // RACE CONDITION: Simulate another developer adding a commit to the feature branch
+ // after the merge/push but before worktree removal and branch deletion.
+ // Since feature is already checked out in feature_wt, we'll add the commit directly there.
+ fs::write(feature_wt.join("extra.txt"), "race condition commit").expect("Failed to write file");
+
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "extra.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 extra file (race condition)"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Now simulate what wt merge would do: remove the worktree
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["worktree", "remove", feature_wt.to_str().unwrap()])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to remove worktree");
+
+ // Try to delete the branch with -d (safe delete)
+ // This should FAIL because the branch has the race condition commit not in main
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ let output = cmd
+ .args(["branch", "-d", "feature"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to run git branch -d");
+
+ // Verify the deletion failed (non-zero exit code)
+ assert!(
+ !output.status.success(),
+ "git branch -d should fail when branch has unmerged commits"
+ );
+
+ // Verify the error message mentions the branch is not fully merged
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains("not fully merged") || stderr.contains("not merged"),
+ "Error should mention branch is not fully merged, got: {}",
+ stderr
+ );
+
+ // Verify the branch still exists (wasn't deleted)
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ let output = cmd
+ .args(["branch", "--list", "feature"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to list branches");
+
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ stdout.contains("feature"),
+ "Branch should still exist after failed deletion"
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_race_condition_before_new_commit.snap b/tests/snapshots/integration__integration_tests__merge__merge_race_condition_before_new_commit.snap
new file mode 100644
index 000000000..8f7baa09a
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_race_condition_before_new_commit.snap
@@ -0,0 +1,35 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ - "--no-remove"
+ 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/.tmp1qAZ0Y/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+πŸ”„ Merging 1 commit to main @ [SHA] (no commit/squash/rebase needed)
+
+* [SHA] (HEAD -> feature) Add feature file
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Merged to main (1 commit, 1 file, +1)
+βœ… Worktree preserved (--no-remove)
+
+----- stderr -----