Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Fix relocate temp path collisions (#4256)
DDL3a45a60c6202
diff --git a/src/commands/relocate.rs b/src/commands/relocate.rsindex d7c4ad3aa..b84c253e7 100644--- a/src/commands/relocate.rs+++ b/src/commands/relocate.rs@@ -710,9 +710,11 @@ impl<'a> RelocationExecutor<'a> {// Create temp directory if neededstd::fs::create_dir_all(&self.temp_dir)?;- // Sanitize branch name for temp path (feature/foo -> feature-foo)+ // Prefix with the candidate index because sanitize_for_filename's+ // short hash can collide for distinct branch names. Multiple cycles+ // keep their temporary worktrees alive until finalization.let safe_branch = worktrunk::path::sanitize_for_filename(branch);- let temp_path = self.temp_dir.join(&safe_branch);+ let temp_path = self.temp_dir.join(format!("{i}-{safe_branch}"));let msg = cformat!("Moving <bold>{branch}</> to temporary location...");eprintln!("{}", progress_message(msg));diff --git a/tests/integration_tests/step_relocate.rs b/tests/integration_tests/step_relocate.rsindex 47d39e7fc..63fe27b24 100644--- a/tests/integration_tests/step_relocate.rs+++ b/tests/integration_tests/step_relocate.rs@@ -783,6 +783,87 @@ fn test_relocate_swap(repo: TestRepo) {assert!(path_for_beta.exists(), "beta should be at repo.beta");}+/// Two independent relocation cycles must not share a temporary worktree path.+///+/// These branch names produce the same filename under the current 3-character+/// hash. The first cycle keeps its worktree in staging until all cycles have+/// been broken, so the second cycle used to fail when it tried to reuse that+/// live path.+#[rstest]+fn test_relocate_disjoint_swaps_with_colliding_temp_names(repo: TestRepo) {+ const FIRST: &str = "a/a-a-a/a-a/a/a-a/a";+ const SECOND: &str = "a-a-a-a/a/a-a/a/a-a";+ assert_eq!(+ worktrunk::path::sanitize_for_filename(FIRST),+ worktrunk::path::sanitize_for_filename(SECOND),+ "test premise: both branches must sanitize to the same filename"+ );++ let parent = worktree_parent(&repo);+ let first_source = parent.join("aaa-first");+ let first_target = parent.join("bbb-first-partner");+ let second_source = parent.join("ccc-second");+ let second_target = parent.join("ddd-second-partner");++ for (branch, path) in [+ (FIRST, &first_source),+ ("first-partner", &first_target),+ (SECOND, &second_source),+ ("second-partner", &second_target),+ ] {+ repo.run_git(&["worktree", "add", "-b", branch, path.to_str().unwrap()]);+ }++ let worktrunk_config = format!(+ r#"+worktree-path = "{{% if branch == '{FIRST}' %}}{}{{% elif branch == 'first-partner' %}}{}{{% elif branch == '{SECOND}' %}}{}{{% elif branch == 'second-partner' %}}{}{{% else %}}../{{{{ repo }}}}.{{{{ branch | sanitize }}}}{{% endif %}}"+"#,+ first_target.to_slash_lossy(),+ first_source.to_slash_lossy(),+ second_target.to_slash_lossy(),+ second_source.to_slash_lossy(),+ );+ fs::write(repo.test_config_path(), worktrunk_config).unwrap();++ let output = repo+ .wt_command()+ .args([+ "step",+ "relocate",+ FIRST,+ "first-partner",+ SECOND,+ "second-partner",+ ])+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "both swaps should relocate; stderr:\n{}",+ String::from_utf8_lossy(&output.stderr)+ );++ for (path, branch) in [+ (&first_target, FIRST),+ (&first_source, "first-partner"),+ (&second_target, SECOND),+ (&second_source, "second-partner"),+ ] {+ let output = repo+ .git_command()+ .current_dir(path)+ .args(["branch", "--show-current"])+ .run()+ .unwrap();+ assert_eq!(+ String::from_utf8_lossy(&output.stdout).trim(),+ branch,+ "{} should contain {branch}",+ path.display()+ );+ }+}+/// A worktree whose target is occupied by a *blocked* worktree must itself be/// skipped, not temp-moved.///