Skip to content

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

fix(git): harden worktree add in main-worktree relocate against hyphen-prefixed branches (#2738)

Worktrunk Botb9862c9ad3a7
diff --git a/src/commands/relocate.rs b/src/commands/relocate.rs
index 1d64d1c88..9a06ce2f6 100644
--- a/src/commands/relocate.rs
+++ b/src/commands/relocate.rs
@@ -575,7 +575,7 @@ impl<'a> RelocationExecutor<'a> {
let dest = candidate.expected_path.to_string_lossy();
main_wt
- .run_command(&["worktree", "add", &dest, branch])
+ .run_command(&["worktree", "add", "--end-of-options", &dest, branch])
.context("Failed to create worktree for main relocation")?;
Ok(())
diff --git a/tests/integration_tests/step_relocate.rs b/tests/integration_tests/step_relocate.rs
index 071f2e2c3..d96274447 100644
--- a/tests/integration_tests/step_relocate.rs
+++ b/tests/integration_tests/step_relocate.rs
@@ -432,6 +432,41 @@ fn test_relocate_main_worktree(repo: TestRepo) {
);
}
+/// Regression: a branch literally named `-foo` (creatable via `git
+/// update-ref refs/heads/-foo HEAD`) must round-trip through main worktree
+/// relocation without `git worktree add` parsing the ref as a flag.
+/// Without `--end-of-options`, the `worktree add` call would fail with
+/// `unknown switch 'o'`.
+#[rstest]
+fn test_relocate_main_worktree_hyphen_prefixed_branch(repo: TestRepo) {
+ let parent = worktree_parent(&repo);
+
+ // `git checkout -b -- -foo` is rejected by modern git, but `update-ref`
+ // happily writes the ref, then `symbolic-ref` moves HEAD onto it.
+ repo.run_git(&["update-ref", "refs/heads/-foo", "HEAD"]);
+ repo.run_git(&["symbolic-ref", "HEAD", "refs/heads/-foo"]);
+
+ let output = repo
+ .wt_command()
+ .args(["step", "relocate"])
+ .output()
+ .unwrap();
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ output.status.success(),
+ "relocate must succeed with a hyphen-prefixed branch; \
+ stdout: {stdout}\nstderr: {stderr}"
+ );
+
+ let expected_path = parent.join("repo.-foo");
+ assert!(
+ expected_path.exists(),
+ "worktree for `-foo` should be created at: {}",
+ expected_path.display()
+ );
+}
+
/// Test swap scenario: two worktrees at each other's expected locations
///
/// When alpha is at repo.beta and beta is at repo.alpha, relocate