Skip to content

fix(switch): refuse --path naming the bare repository; drop rm -rf hint for --path - #4532

Merged
max-sixty merged 2 commits into
mainfrom
fix/path-flag-guards
Oct 20, 2026
Merged

max-sixty merged 2 commits into
mainfrom
fix/path-flag-guards

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Two problems with the new wt switch --path flag from #4317, found in release review.

Bare repository moved aside. wt switch --create x --path proj.git --clobber in a bare clone with no worktrees printed Moved ~/proj.git to ~/proj.git.bak.<ts> (--clobber) and created a worktree in the repository's place. reject_path_overlapping_worktrees skipped a requested path equal to any entry it checks, which is right for a registered worktree (validate_worktree_creation reports it by branch) but nothing later checks the git common dir. The guard now refuses that case with --path <dir> is the repository's git directory. The template route has the same exposure and is left to #4461; this guard runs only for --path.

While there, the guard compares paths with canonicalize_with_parents rather than dunce::canonicalize, so a deep Windows path and the short root containing it get the same prefix spelling for starts_with (the #3898 mismatch).

rm -rf hint for a user-named directory. When the --path directory exists, the hint suggested rm -rf <path>, though --path can name any directory, such as a project. For --path it now reads To place the worktree elsewhere, choose a different --path; to overwrite (with backup), run wt switch --create --clobber <branch> --path=<path>. The template route's hint is unchanged.

Verification: reproduced both on a build of main and confirmed the fix on this branch. test_bare_repo_switch_path_rejects_the_repository fails with the old guard (its snapshot shows the Moved … (--clobber) line), and the switch_path_occupied snapshot shows the new hint. test_switch_path_naming_another_worktree pins that --path naming another branch's worktree, with --clobber, still reports that worktree's branch and leaves it in place. Ran the switch, bare-repository and error-display integration tests, clippy, and pre-commit.

This was written by Claude Code on behalf of max-sixty

…nt for --path

`wt switch --create x --path <bare-repo> --clobber` moved the bare
repository aside: the overlap guard skipped a requested path equal to the
git common dir, as it does for a registered worktree, but nothing later
checks the git dir. The guard now refuses it. It also compares paths with
canonicalize_with_parents, so a deep Windows path and a short root keep
one spelling for starts_with.

For a directory named by --path, the "Directory already exists" hint
suggested `rm -rf` on it, though --path can name any directory, such as a
project. That hint now suggests a different --path, keeping the --clobber
command, which moves the directory to a backup.

Both came in with --path (#4317).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 1fe07b3 into main Oct 20, 2026
38 checks passed
@max-sixty
max-sixty deleted the fix/path-flag-guards branch October 20, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants