Repository navigation
feat(templates): add expected_worktree_path_of_branch() template function - #4502
Conversation
`expected_worktree_path(branch)` renders the path `wt switch --create` would use for a branch, through the same computation `wt switch` uses (custom and per-project `worktree-path` templates, the default branch at the repo root). Unlike `worktree_path_of_branch`, it returns that path even when the worktree there has moved to another branch, so a wrapper can switch by path after the occupied-path error. The computation moves from `compute_worktree_path` in the binary to `UserConfig::expected_worktree_path` in the library so the template environment can call it. A `worktree-path` template that calls the function errors instead of recursing. Requested in #4480. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
codecov/patch is red at 76.92%. Merging needs a maintainer's explicit approval (root AGENTS.md, Coverage), or a test for these lines. All six misses are in UserConfig::expected_worktree_path in src/config/user/accessors.rs. They are the ok_or_else closures for "Repository path has no filename" and "Repository path contains invalid UTF-8", moved verbatim from compute_worktree_path, and the same closures are uncovered in src/commands/worktree/resolve.rs on main (5e5c65b2b). This PR adds no new coverage gap. Covering them would take a repository at / or at a path that isn't valid UTF-8, so I left them as is rather than add a test only for the percentage.
| | Function | Example | Description | | ||
| |----------|---------|-------------| | ||
| | `worktree_path_of_branch(branch)` | `{{ worktree_path_of_branch("main") }}` | Look up the path of a branch's worktree | | ||
| | `expected_worktree_path(branch)` | `{{ expected_worktree_path("feature") }}` | Where `wt switch` expects the branch's worktree | |
There was a problem hiding this comment.
should we have the _of_branch suffix? or remove from the other with a depreciation cycle? we should align
There was a problem hiding this comment.
I renamed the new function to expected_worktree_path_of_branch in 7bff225, so it matches worktree_path_of_branch. Dropping the suffix from the existing function instead would mean deprecating a name that users' hooks and aliases already reference, and keeping an alias for it. Adding the suffix to the new name costs nothing because the function hasn't shipped yet. The Rust method stays UserConfig::expected_worktree_path, since the _of_branch suffix only matters where the name sits next to the other template function.
…ee_path_of_branch Matches the existing worktree_path_of_branch function. Co-Authored-By: Claude <[email protected]>
|
Thank you for making an effort to support my workflow. :) In the future it would be nice if there was some kind of error reporting so that scripts could differentiate between expected and unexpected failures. As I understand it, currently all I can do is grep for a error message that might change in the future or assume the expected error in all cases (#4480 (comment)). Imo a script friendly errors would be useful in all cases, not just |
|
You're right. Today the only way to tell this failure apart from others is to match the stderr text. #3697 tracks this across commands: a stable, machine-readable error channel with codes under |
Adds an
expected_worktree_path_of_branch(branch)template function that returns the pathwt switch --createwould use for a branch, whether or not a worktree exists there. This is option (1) from the discussion on #4480, which @max-sixty approved. It gives @BenJurewicz's wrapper the missing piece: afterwt switch featurefails because the worktree atfeature's path now holds another branch, a script can get that path and switch to it, and that works with custom and per-projectworktree-pathtemplates:worktree_path_of_branchcan't do this, because it returns where the branch is checked out and comes back empty in exactly this case.What changed
compute_worktree_pathin the binary toUserConfig::expected_worktree_pathin the library, so the template environment can call it.compute_worktree_pathnow delegates to it, sowt switchand the template function share one implementation, including the default branch resolving to the repo root.worktree-pathtemplate goes through the same template environment, so aworktree-pathtemplate that callsexpected_worktree_path_of_branchwould recurse. A thread-local guard turns that into an error.Verification
wt step evalintegration tests check the expected path when another branch occupies it (whereworktree_path_of_branchis empty), the default-branch case, and the recursion error.repo.featureswitched toother:wt step eval "{{ expected_worktree_path_of_branch('feature') }}"printed…/repo.feature,wt switchon that path landed there, and--config-set 'worktree-path = "/tmp/wts/{{ branch }}"'produced/tmp/wts/f.The name follows the existing
worktree_path_of_branch, so both functions that take a branch end in_of_branch.codecov/patch is red because of moved lines. The six misses in
src/config/user/accessors.rsare the "Repository path has no filename" and "contains invalid UTF-8" error closures, moved fromcompute_worktree_pathwithout changes. They were already uncovered on main at5e5c65b(src/commands/worktree/resolve.rslines 47–56). Every new line inexpansion.rsandresolve.rsis covered. Reaching those closures would need a repository at/or at a path that isn't valid UTF-8, so I left them as they were. PerAGENTS.md, merging with this check red needs explicit approval.