Skip to content

feat(templates): add expected_worktree_path_of_branch() template function - #4502

Merged
max-sixty merged 2 commits into
mainfrom
feat/expected-worktree-path
Oct 19, 2026
Merged

max-sixty merged 2 commits into
mainfrom
feat/expected-worktree-path

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Adds an expected_worktree_path_of_branch(branch) template function that returns the path wt switch --create would 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: after wt switch feature fails because the worktree at feature's path now holds another branch, a script can get that path and switch to it, and that works with custom and per-project worktree-path templates:

wts() {
  wt switch "$@" && return
  local rc=$? p
  p=$(wt step eval "{{ expected_worktree_path_of_branch('$1') }}") || return "$rc"
  [ -d "$p" ] && wt switch "$p" || return "$rc"
}

worktree_path_of_branch can't do this, because it returns where the branch is checked out and comes back empty in exactly this case.

What changed

  • The path computation moves from compute_worktree_path in the binary to UserConfig::expected_worktree_path in the library, so the template environment can call it. compute_worktree_path now delegates to it, so wt switch and the template function share one implementation, including the default branch resolving to the repo root.
  • Rendering the worktree-path template goes through the same template environment, so a worktree-path template that calls expected_worktree_path_of_branch would recurse. A thread-local guard turns that into an error.
  • The hook docs' functions table and a short paragraph cover the new function. Generated docs are regenerated.

Verification

  • New wt step eval integration tests check the expected path when another branch occupies it (where worktree_path_of_branch is empty), the default-branch case, and the recursion error.
  • Manually in a scratch repo, with repo.feature switched to other: wt step eval "{{ expected_worktree_path_of_branch('feature') }}" printed …/repo.feature, wt switch on 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.rs are the "Repository path has no filename" and "contains invalid UTF-8" error closures, moved from compute_worktree_path without changes. They were already uncovered on main at 5e5c65b (src/commands/worktree/resolve.rs lines 47–56). Every new line in expansion.rs and resolve.rs is 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. Per AGENTS.md, merging with this check red needs explicit approval.

`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 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.

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.

Comment thread docs/src/content/docs/hook.md Outdated
| 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 |

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

should we have the _of_branch suffix? or remove from the other with a depreciation cycle? we should align

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.

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]>
@worktrunk-bot worktrunk-bot changed the title feat(templates): add expected_worktree_path() template function feat(templates): add expected_worktree_path_of_branch() template function Oct 19, 2026
@max-sixty
max-sixty merged commit 4cb4267 into main Oct 19, 2026
37 of 38 checks passed
@max-sixty
max-sixty deleted the feat/expected-worktree-path branch October 19, 2026 05:33
This was referenced Oct 19, 2026
@BenJurewicz

Copy link
Copy Markdown

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 wt switch

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

You're right. Today the only way to tell this failure apart from others is to match the stderr text. wt switch --format=json structures only a successful result. On failure it prints the same prose to stderr and nothing to stdout, and the exit code is a generic 1.

#3697 tracks this across commands: a stable, machine-readable error channel with codes under --format=json. A switch whose target path holds another branch is a concrete case for it, and the code could carry the path and the branch checked out there. Until it lands, the wrapper in this PR's description keeps the caveat you noted. It falls back on any failure, but switches only when a directory exists at the expected path, and otherwise returns the original exit code. An unrelated failure, such as a failing hook, still triggers the fallback when that directory exists.

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.

3 participants