worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

feature request: Need branch prefix

Open mayureshwaykole opened this issue 8 months ago • 10 comments

My team follows the convention of creating branches with a prefix like u/username/feature-name With worktrunk, this naming convention becomes tedios.

Can we have a config setting to specify the branch prefix ? This way worktree naming and management using other wt commands remains simple.

mayureshwaykole avatar Mar 13 '26 22:03 mayureshwaykole

Thanks for the suggestion! I searched the codebase and didn't find an existing branch prefix configuration option. Currently, wt switch --create uses the branch name exactly as provided — there's no built-in transformation or prefix.

The closest related functionality is the worktree-path template in user config (~/.config/worktrunk/config.toml), which controls the directory path for worktrees using variables like {{ branch | sanitize }}, but it doesn't affect the branch name itself.

I'll leave it for a maintainer to evaluate and prioritize.

worktrunk-bot avatar Mar 13 '26 22:03 worktrunk-bot

@worktrunk-bot how about a pre-switch hook that checks if the branch has the prefix?

max-sixty avatar Mar 13 '26 22:03 max-sixty

That's a nice idea — though today pre-switch hooks can't quite do this. They run before the destination branch is resolved and only receive the current worktree's context ({{ branch }}, {{ worktree_path }}, etc.), not the target branch name. So a pre-switch hook couldn't inspect or enforce a prefix on the branch being switched to / created.

A couple of approaches that could work today as workarounds:

  1. Shell alias / function — wrap wt switch --create to prepend the prefix:

    wts() { wt switch --create "u/$(whoami)/$1" "${@:2}"; }
    # wts my-feature → wt switch --create u/alice/my-feature
    
  2. Git alias — similar idea via git config alias.

For a first-class solution, the simplest path might be exposing the destination branch to pre-switch hooks (or adding a dedicated branch-prefix config). Exposing the destination as a template variable (e.g. {{ destination }}) would let users write a validation hook, though it still wouldn't transform the name automatically — it could only reject branches missing the prefix.

A branch-prefix config in wt.toml or user config that auto-prepends on --create would be the most ergonomic solution for the use case described in this issue.

worktrunk-bot avatar Mar 13 '26 22:03 worktrunk-bot

@worktrunk-bot let me clarify When I run wt switch --create feat-a I want a worktree named feat-a but the checked out branch should have the branch prefix and be u/username/feat-a.

Ideally the branch prefix would be configurable in wt.toml

mayureshwaykole avatar Mar 13 '26 23:03 mayureshwaykole

Thanks for the clarification — that makes the desired behavior much clearer.

So the flow would be:

wt switch --create feat-a
  • Worktree directory: repo.feat-a (per the path template)
  • Git branch: u/username/feat-a (prefix prepended automatically)
  • wt list / wt switch feat-a: addresses it by the short name feat-a

This is a nice separation — the prefix is a team convention for the remote, but locally you'd interact with the short name.

A few design notes for whoever picks this up:

  1. Where the prefix applies: It would only affect git branch -b during --create. The worktree path template already uses {{ branch | sanitize }}, so it would need to use the unprefixed name for the directory but the prefixed name for the actual git branch.

  2. Config shape: Something like this in wt.toml:

    [switch]
    branch-prefix = "u/myuser/"
    

    Or in user config (~/.config/worktrunk/config.toml) if it should apply across all repos.

  3. Addressing worktrees: Since worktrunk addresses worktrees by branch name, commands like wt switch feat-a would need to resolve the short name to the full prefixed branch — or worktrees would be addressed by the full branch name u/username/feat-a. The former is more ergonomic but adds complexity; the latter is simpler but partly defeats the purpose.

  4. Interaction with wt list: The branch column would show the full prefixed name, which could be noisy. A display-name vs actual-branch-name distinction might be worth considering.

Point 3 is the main design question — happy to dig deeper once there's a direction.

worktrunk-bot avatar Mar 13 '26 23:03 worktrunk-bot

That's a nice idea — though today pre-switch hooks can't quite do this. They run before the destination branch is resolved and only receive the current worktree's context ({{ branch }}, {{ worktree_path }}, etc.), not the target branch name. So a pre-switch hook couldn't inspect or enforce a prefix on the branch being switched to / created.

@worktrunk-bot, separately from this issue, could you draft a change so that we can inspect the branch name, for example with {{ target }}? how about for wt merge or wt remove etc?

max-sixty avatar Mar 14 '26 01:03 max-sixty

@mayureshwaykole check out aliases, which allow this sort of specificity. if there's something missing from that which would also be generally helpful, then feel free to reopen this or open a new issue. thanks!

max-sixty avatar Mar 14 '26 01:03 max-sixty

Good news — {{ target }} already works for merge hooks. merge.rs passes it via extra_vars, so pre-merge and post-merge hooks can use it today.

Here's the current state across hook types:

Hook Extra vars available
pre-switch (none — runs before destination is resolved)
post-switch / post-create / post-start base, base_worktree_path (creates only)
pre-merge / post-merge target
pre-remove / post-remove removed worktree's branch, worktree_path, etc.

The gap is pre-switch: it runs before branch validation, so the destination branch isn't resolved yet. To expose {{ target }} there, we'd need to move branch resolution earlier (before hooks) or split it into two phases — resolve the target first, then run pre-switch with it.

For post-switch / post-create / post-start, the destination branch is known (it's the current {{ branch }}), but exposing the pre-switch source as {{ previous }} or similar could also be useful — though that's a different variable.

For remove: pre-remove and post-remove already get the removed worktree's full context (branch, worktree_path, commit, etc.) via prepare_post_remove_commands(), so there's no gap there.

Want me to draft a PR that moves branch resolution before pre-switch so {{ target }} is available there?

worktrunk-bot avatar Mar 14 '26 01:03 worktrunk-bot

Want me to draft a PR that moves branch resolution before pre-switch so {{ target }} is available there?

yes, I or just to {{ branch }} actually (let's continue the discussion on the PR)

max-sixty avatar Mar 14 '26 01:03 max-sixty

Drafted in #1497 — moves branch resolution before pre-switch hooks so {{ branch }} is the destination. CI is green across all platforms.

worktrunk-bot avatar Mar 14 '26 01:03 worktrunk-bot