Repository navigation
feat(switch): add --path flag to specify custom worktree directory - #2275
XiaoPengYouCode wants to merge 1 commit into
Conversation
Adds `wt switch --create <branch> --path <custom>` so users can decouple the worktree directory name from the branch name. - Absolute paths are used as-is - Relative paths resolve under the configured worktree parent directory - Paths with `..` components are rejected (must use absolute to escape parent) - Custom path is persisted in git config so recreating the worktree reuses it - `--path` is only valid with `--create`; ignored for existing worktrees - Picker Alt-c path passes through the same resolution logic Closes #1982
worktrunk-bot
left a comment
There was a problem hiding this comment.
A couple of questions to flag for a human reviewer — the implementation looks coherent and well-tested, but two points seem worth resolving before merge.
1. The --path use case still triggers the "customize worktree locations" hint. The PR description's test plan lists test_switch_create_no_hint_with_custom_worktree_path, but no such test exists in the diff. The snapshot for test_switch_create_with_custom_relative_path confirms the hint still fires:
✓ Created branch feature/JIRA-1234 from main and worktree @ _PARENT_/navigation-animation-refactor
↳ To customize worktree locations, run wt config create
Either the suppression was intended and dropped (the hint is unhelpful here — the user just specified a one-off custom path), or the description is stale. Worth a maintainer call given #1982's discussion about how aggressively to surface the per-invocation override vs. the templated default.
2. The worktree_path_overrides cache is never invalidated. worktree_path_override populates a OnceCell<HashMap> on first read, and set_worktree_path_override / clear_worktree_path_override write to git config but don't refresh the cache. Today this is safe because the only writer (execute_switch) runs after the only reader (compute_worktree_path in plan_switch), so within a single command the read happens first. It's brittle, though — any future code that reads the override after set_* in the same process will silently get stale data. Worth either invalidating on write or documenting the invariant on RepoCache::worktree_path_overrides.
|
A couple of additional uncovered lines around the picker call site ( Happy to push a commit adding unit tests for the two helpers if that's useful — let me know. |
|
@worktrunk-bot can you link to past discussions on the sort of change please? |
|
@XiaoPengYouCode i appreciate the effort, but this isn't a feature I think we should add, and the code quality has some room to improve; so I'll close |
|
Three threads where you've discussed branch ↔ worktree decoupling:
So the consistent prior stance is: keep the 1:1 branch↔worktree mapping, push users to aliases/shell functions, and only promote to native commands once a pattern clearly catches on. This PR cuts straight to a native flag without that intermediate step — I should not have skipped to implementation given #1982's explicit "extremely high bar" framing. Happy to close it and instead post the alias recipe on #1982 if you'd prefer. |
|
Okay I will continue to maintain this branch and regularly sync it with the upstream. I want to share my use case. I currently need to perform operations across multiple repositories, and I hope the agent can have a clean workspace. But now if I have three repositories and create three worktrees, my workspace will have six directories. @worktrunk-bot Please could you introduce to me what approach I should use according to the current repository specifications? |
|
For a clean multi-repo workspace, the right lever is the A centralized layout in worktree-path = "~/worktrees/{{ repo }}/{{ branch | sanitize }}"For three repos so only You can also override per-project via |
…th template (#4317) 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: ```console $ 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 --------- Co-authored-by: worktrunk-bot <[email protected]> Co-authored-by: Claude <[email protected]>
Summary
--path <custom>flag towt switch --createso the worktree directory name can be specified independently of the branch name..components are rejected (use an absolute path to escape the parent)--pathis only valid with--create; passing it without--createis a no-op (existing worktrees keep their location)Closes #1982
Example
Test plan
test_switch_create_with_custom_relative_path— relative path resolves under parenttest_switch_create_rejects_parent_relative_custom_path—..paths are rejectedtest_switch_create_custom_path_persists_for_recreate— custom path is remembered on recreatetest_switch_create_custom_path_override_is_hidden_from_user_vars— path override doesn't leak into template varstest_switch_create_no_hint_with_custom_worktree_path— no spurious "customize worktree locations" hint when--pathis usedtest_command_pages_and_skill_files_are_in_sync)