worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Add support to pr_number in worktree-path

Open robsonpeixoto opened this issue 3 weeks ago • 3 comments

I'd like to use it worktree-path = "{{ repo_path }}.worktrees/{{ pr_number or branch | sanitize }}". So, if I'm checking out a PR to review, I prefer to use the PR number on the directory name.

robsonpeixoto avatar Nov 09 '26 22:11 robsonpeixoto

The obstacle here is that worktree-path sets where a branch's worktree is expected to be, and Worktrunk recomputes that from the branch name alone in places that never see a PR. expected_worktree_path calls format_path with only repo, branch, repo_path, owner and remote_repo. It feeds wt list's ⚐ branch-mismatch marker, plain wt switch <branch> resolution, and the expected_worktree_path_of_branch template function. pr_number exists only during a wt switch pr:N run (that's why the hook docs list it as "when switching via pr:N / mr:N"). So a template like {{ pr_number or branch | sanitize }} would put the worktree in .worktrees/123 at creation, and every later lookup would expect .worktrees/<branch>. wt list would then flag each PR worktree as mismatched. Doing this properly means deciding whether, and where, a branch's PR number gets recorded so later lookups can find it. That's a design choice for a maintainer, so I haven't opened a PR.

You can get a PR-numbered directory today by naming the local branch after the PR:

wt switch --create pr-123 --base=pr:123

With your template set to {{ repo_path }}.worktrees/{{ branch | sanitize }}, this creates the worktree at <repo>.worktrees/pr-123, branched from PR #123's head. For a same-repo PR, the new branch's upstream is set to the PR's source branch (switch.rs). I haven't run this end to end. One caveat from reading the code: the upstream setup applies only to same-repo PRs, so a fork PR checked out this way doesn't get the push-to-fork configuration that wt switch pr:N sets up. If you do this often, an alias can wrap it.

worktrunk-bot avatar Nov 09 '26 22:11 worktrunk-bot

This command wt switch --create pr-123 --base=pr:123 will create a new branch.

There is relation between branch name and worktreename, @worktrunk-bot!

Should be possible use pr_number because wt need to first resolve the pr:<number> to a branch first.

robsonpeixoto avatar Nov 09 '26 23:11 robsonpeixoto

You're right on both counts. The workaround creates a second local branch (pr-123) instead of checking out the PR's own branch, which isn't what you asked for. And pr_number is known when the worktree is created: wt switch pr:N resolves the PR first and already passes pr_number to the pre-start hooks (switch.rs). So supporting it in the template at creation time would be easy.

The harder part comes after creation. worktree-path is also re-evaluated for worktrees that already exist, and those lookups start from the branch name alone, with no PR in sight (compute_worktree_path):

  • wt list and the statusline compare each worktree's path against the template, so a worktree at .worktrees/123 whose branch is fix-foo would be shown as fix-foo @ <path> because it's "not where expected".
  • wt step relocate uses the same comparison (relocate.rs), so it would offer to move every PR worktree back to .worktrees/fix-foo.

To fix this properly, Worktrunk would need to remember which PR a branch came from, for example in the branch's git config at creation, so those later lookups can fill in pr_number too. Alternatively, pr_number could be allowed only at creation and the mismatch accepted. Either is a product decision about config semantics, so I'm leaving this open for @max-sixty rather than opening a PR.

worktrunk-bot avatar Nov 09 '26 23:11 worktrunk-bot