Repository navigation
feat(switch): add --path to create a worktree outside the worktree-path template - #4317
Conversation
`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
left a comment
There was a problem hiding this comment.
--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.
|
are there any questions to think about re the API? I think it's the only command which takes a |
|
Yes, it's the only one. The other path-valued flags are global ( I don't think the flipped shape (path as the positional,
git's own analog is Two API questions are worth deciding before this lands:
|
|
ok, label as experimental and then we'll merge |
|
I've marked |
…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]>
…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]>
Adds
--path <PATH>towt 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 theworktree-pathtemplate that #1982 asked for:Semantics:
-C), asgit worktree add <path>does, through the sameresolve_input_paththat already resolveswt switch ../repo.feature. A leading~expands.--createwhen the branch exists but has no worktree yet.✗ feature-z already has a worktree @ …, not …; to switch to it, run without --path. Naming the directory it already has just switches.--path. The--clobbersuggestion for an occupied directory now readswt switch --create --clobber feature-y --path=../occupied. Without--pathit would have backed up the template's directory instead.--clobber, hooks, and{{ worktree_path }}all go through the existing plan, which now takes the requested path in place ofcompute_worktree_path.Nothing is persisted: git already records where each worktree is, and resolution,
list,merge,remove, and hooks read paths fromgit worktree list. The one command that notices iswt step relocate, which offers to move an off-template worktree back to the template path. The long help now says so.wt liststill marks these rows⚑and shows thePathcolumn.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-modefrom 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
--clobberhint. 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