Skip to content

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

Fix background force-delete for flag-like branches (#4253)

DDL96a00efbe13b
diff --git a/src/commands/process.rs b/src/commands/process.rs
index 8e0027165..58a944e4b 100644
--- a/src/commands/process.rs
+++ b/src/commands/process.rs
@@ -773,7 +773,7 @@ pub fn build_remove_command(
Some(branch_name) => {
let branch_escaped = escape(branch_name.into());
format!(
- "{}git worktree remove{} {} && git branch -D {}",
+ "{}git worktree remove{} {} && git branch -D -- {}",
prefix, force_flag, worktree_escaped, branch_escaped
)
}
@@ -879,18 +879,18 @@ mod tests {
// changed_directory=true: sleep before removal
assert_snapshot!(build_remove_command(&path, None, false, true), @"sleep 1 && git worktree remove /tmp/test-worktree");
- assert_snapshot!(build_remove_command(&path, Some("feature-branch"), false, true), @"sleep 1 && git worktree remove /tmp/test-worktree && git branch -D feature-branch");
+ assert_snapshot!(build_remove_command(&path, Some("feature-branch"), false, true), @"sleep 1 && git worktree remove /tmp/test-worktree && git branch -D -- feature-branch");
// changed_directory=false: no sleep
assert_snapshot!(build_remove_command(&path, None, false, false), @"git worktree remove /tmp/test-worktree");
- assert_snapshot!(build_remove_command(&path, Some("feature-branch"), false, false), @"git worktree remove /tmp/test-worktree && git branch -D feature-branch");
+ assert_snapshot!(build_remove_command(&path, Some("feature-branch"), false, false), @"git worktree remove /tmp/test-worktree && git branch -D -- feature-branch");
// With force flag
assert_snapshot!(build_remove_command(&path, None, true, true), @"sleep 1 && git worktree remove --force /tmp/test-worktree");
// Shell escaping for special characters
let special_path = PathBuf::from("/tmp/test worktree");
- assert_snapshot!(build_remove_command(&special_path, Some("feature/branch"), false, true), @"sleep 1 && git worktree remove '/tmp/test worktree' && git branch -D feature/branch");
+ assert_snapshot!(build_remove_command(&special_path, Some("feature/branch"), false, true), @"sleep 1 && git worktree remove '/tmp/test worktree' && git branch -D -- feature/branch");
}
#[test]
diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rs
index 51950c449..e6dee7bb7 100644
--- a/tests/integration_tests/remove.rs
+++ b/tests/integration_tests/remove.rs
@@ -3709,6 +3709,45 @@ fn test_remove_background_fallback_force_delete_branch(mut repo: TestRepo) {
let _ = std::fs::remove_file(&staged_path);
}
+/// The force-delete fallback must pass a flag-like branch after `--`. Git
+/// accepts such refs through plumbing, even though its branch parser cannot
+/// create or delete them without an option separator.
+#[rstest]
+fn test_remove_background_fallback_force_deletes_flag_like_branch(mut repo: TestRepo) {
+ repo.commit("initial");
+ let worktree_path = repo.add_worktree("flag-like-holder");
+ repo.run_git(&["update-ref", "refs/heads/-x", "HEAD"]);
+ repo.run_git_in(&worktree_path, &["symbolic-ref", "HEAD", "refs/heads/-x"]);
+ repo.run_git(&["branch", "-D", "flag-like-holder"]);
+ let staged_path = block_staged_rename(&repo, &worktree_path);
+
+ let output = repo
+ .wt_command()
+ .args(["remove", "--force", "-D", worktree_path.to_str().unwrap()])
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "wt remove --force -D should start the legacy fallback: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ crate::common::wait_for("flag-like worktree removed by legacy fallback", || {
+ !worktree_path.exists()
+ });
+ crate::common::wait_for("flag-like branch force-deleted by legacy fallback", || {
+ !repo
+ .git_command()
+ .args(["show-ref", "--verify", "--quiet", "refs/heads/-x"])
+ .run()
+ .unwrap()
+ .status
+ .success()
+ });
+
+ let _ = std::fs::remove_file(&staged_path);
+}
+
/// The rename-failure fallback removes a detached-HEAD worktree with no branch
/// to delete — the `_` arm of the fallback command builder. `wt remove` resolves
/// the detached worktree by path.