Skip to content

feat(switch): add --path to create a worktree outside the worktree-path template - #4317

Merged
max-sixty merged 3 commits into
mainfrom
feat/switch-path
Sep 30, 2026
Merged

max-sixty merged 3 commits into
mainfrom
feat/switch-path

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Adds --path <PATH> to wt switch, so one worktree can live in a directory of the user's choosing while its branch keeps its own name. This is the opt-out from the worktree-path template that #1982 asked for:

$ wt switch --create feat/JIRA-1234/dark-mode --path ../dark-mode
✓ Created branch feat/JIRA-1234/dark-mode from main and worktree @ ~/code/dark-mode
$ wt switch ../dark-mode          # by path
$ wt switch feat/JIRA-1234/dark-mode   # or by branch

Semantics:

  • Relative paths resolve from the current directory (or -C), as git worktree add <path> does, through the same resolve_input_path that already resolves wt switch ../repo.feature. A leading ~ expands.
  • Works without --create when the branch exists but has no worktree yet.
  • A branch that already has a worktree elsewhere is an error, not silently ignored: ✗ feature-z already has a worktree @ …, not …; to switch to it, run without --path. Naming the directory it already has just switches.
  • Error hints keep --path. The --clobber suggestion for an occupied directory now reads wt switch --create --clobber feature-y --path=../occupied. Without --path it would have backed up the template's directory instead.
  • Occupancy by another worktree, --clobber, hooks, and {{ worktree_path }} all go through the existing plan, which now takes the requested path in place of compute_worktree_path.

Nothing is persisted: git already records where each worktree is, and resolution, list, merge, remove, and hooks read paths from git worktree list. The one command that notices is wt step relocate, which offers to move an off-template worktree back to the template path. The long help now says so. wt list still marks these rows ⚑ and shows the Path column.

Open question for review: I resolved relative paths from the cwd (git's rule) and not from the template's parent directory, which the closed #2275 did. The cwd rule is predictable and matches every other path argument wt takes. Its cost: running --path dark-mode from the main worktree nests the new worktree inside the repo, so users write ../dark-mode.

Tests: new integration snapshots cover create-with-path, an existing branch with no worktree, the existing-worktree-elsewhere error, and the --clobber hint. Help and doc mirrors are regenerated. Locally, the switch and picker integration tests (312), clippy, and pre-commit pass. The only unit-test failures are two tests that bind Unix sockets, which the CI sandbox forbids.

Closes #1982

`wt switch --create feature/JIRA-1234 --path ../dark-mode` creates the
branch with its worktree at ../dark-mode. Relative paths resolve from
the current directory (or -C), as with `git worktree add`. Also works
without --create for an existing branch that has no worktree yet.

Naming a different directory for a branch that already has a worktree
is an error rather than silently ignored. Error hints that suggest a
follow-up `wt switch` keep --path, so the --clobber suggestion backs up
the requested directory, not the template's.

Refs #1982

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--path lets --clobber move directories the template never reaches. A path that exists sends validate_worktree_creation to WorktreePathExists, which suggests rm -rf <path> or --clobber. From the main worktree, wt switch -c y --path .. (easy to type when you mean ../y) prints run rm -rf <parent of the repo>. With --clobber, it moves the directory that holds the repository to <parent>.bak.<ts> and creates the new worktree in its place. That new worktree's .git file then points at an admin dir that has moved away. --path src --clobber moves a tracked directory of the main worktree aside, so git status shows its files as deleted. I reproduced both against this head with a debug build.

I'll push a guard in plan_switch: an existing --path directory that contains, or sits inside, a registered worktree or the git common dir is refused. A directory that doesn't exist yet is left alone, so nesting a new worktree stays git's call, as the description says. A regression test covers both .. and src.

Under this repo's data-loss rule, this PR widens what --clobber can move, so it needs @max-sixty's review before merge.

An existing --path directory got the --clobber hint, and following it
moved the repository itself (--path ..) or a tracked directory
(--path src) aside.
@max-sixty

Copy link
Copy Markdown
Owner

are there any questions to think about re the API?

I think it's the only command which takes a --path? I guess it's not feasible to have the main arg take a branch name OR path and then have --branch, which would be more consistent with other APIs?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Yes, it's the only one. The other path-valued flags are global (-C, --config) or write files (config update --output), and nothing else takes a location for a worktree.

I don't think the flipped shape (path as the positional, --branch for the name) works here, for two reasons:

  • The positional can't tell a path from a branch at creation time. It already accepts either one, but only for a worktree that exists: branch lookup runs first, then path lookup against git worktree list. With --create neither lookup can hit, and feat/JIRA-1234/dark-mode is both a valid branch name and a valid relative path. We'd need a guessing rule like "starts with ., / or ~ means path", and wt switch --create dark-mode would still be ambiguous.
  • --branch already means something else. In step commit, step diff, and config state … --branch it means "act on that other branch's worktree", and in hook … --branch= it overrides {{ branch }}. Here it would mean "name of the branch to create", a third meaning.

git's own analog is git worktree add <path> -b <branch>, which is the flipped shape. But wt switch is organized around the branch, and -b is already --base, so matching git's shape would mean rethinking the whole command, not just adding this feature.

Two API questions are worth deciding before this lands:

  1. Which directory relative paths resolve from. This is the open question in the description. Resolving from the cwd matches git and wt switch ../repo.feature. Resolving from the template's parent (as feat(switch): add --path flag to specify custom worktree directory #2275 did) makes --path dark-mode land next to the other worktrees but surprises people used to git.
  2. The flag name. The alternatives I considered were --dir and --worktree-path, the latter mirroring the config key it overrides. --path reads most naturally in --create x --path ../y, but --worktree-path makes the relationship to the template explicit.

@max-sixty

Copy link
Copy Markdown
Owner

ok, label as experimental and then we'll merge

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

I've marked --path as experimental in 5b06fec, the same way --reap is labeled. Its flag summary now ends in [experimental], and the docs example sits under a new "Custom path [experimental]" heading, which renders the experimental badge on the docs site. I regenerated the docs mirrors and help snapshots. CI and codecov/patch are green at that commit.

@max-sixty
max-sixty merged commit 1a2015b into main Sep 30, 2026
37 checks passed
@max-sixty
max-sixty deleted the feat/switch-path branch September 30, 2026 09:49
max-sixty pushed a commit that referenced this pull request Oct 1, 2026
…te path (#4328)

The new `--path` help section (from #4317) says that "only `wt step
relocate`" treats a worktree created with `--path` differently. That
isn't true: `wt list` marks the row `⚑` (`worktree.branch_mismatch`,
"Worktree isn't at the path its branch implies"), because
`is_worktree_at_expected_path` compares every branch worktree against
the `worktree-path` template. #4317's own PR description says as much
("`wt list` still marks these rows `⚑`"), but the help text didn't carry
it over.

This rewords the sentence to name both: commands still reach the
worktree by branch or path, `wt list` flags it `⚑`, and `wt step
relocate` offers to move it back. The change is in `src/cli/mod.rs`,
plus the regenerated docs/skill mirrors and the `help_switch_long`
snapshot.

Verified against a local build: after `wt switch --create feature/x
--path ../custom`, `wt list` shows `+ feature/x ⚑_ … ../custom`, while a
template-path sibling shows no `⚑`. This changes docs only, so there's
no regression test. `test_docs_are_in_sync` and the help snapshot tests
pass.

Co-authored-by: worktrunk-bot <[email protected]>
max-sixty added a commit that referenced this pull request Oct 20, 2026
…nt for --path (#4532)

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_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

[Feature Request] specify custom worktree directory name independent of branch name

2 participants