worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

[Feature Request] specify custom worktree directory name independent of branch name

Open christowiz opened this issue 7 months ago • 24 comments

Problem

When creating a new worktree with wt switch --create, the worktree directory name is always derived from the branch name. For branches using / separators (e.g. feature/JIRA-1234), the slash is sanitized to a dash, giving a directory like feature-JIRA-1234. There is no way to specify a different directory name on the command line.

Example:

  • wt switch --create feature/JIRA-1234 → directory feature-JIRA-1234, branch feature/JIRA-1234

If I want the directory to be named something descriptive like navigation-animation-refactor while the branch is feature/JIRA-1234, there's currently no way to do this.

Desired behavior

An option (e.g. --path or --name) that lets you specify the worktree directory path independently of the branch name:

wt switch --create feature/JIRA-1234 --path navigation-animation-refactor

This would create a branch called feature/JIRA-1234 with the worktree at a directory called navigation-animation-refactor — exactly what git worktree add already supports:

git worktree add -b feature/JIRA-1234 ../navigation-animation-refactor
# or
git worktree add ../navigation-animation-refactor feature/JIRA-1234

Workaround

Today the only workaround is to drop down to git worktree add directly, which bypasses all of worktrunk's hooks, path templating, and shell integration.

Notes

  • The worktree-path config template controls the base path but not the per-invocation leaf name
  • Related: #1494 (branch prefix), #1451 (rename after creation)

christowiz avatar Apr 07 '26 18:04 christowiz

Thanks for the suggestion! I searched the codebase and confirmed there's no existing flag to override the worktree directory name per-invocation on wt switch --create.

The closest related functionality is the worktree-path template in your config, which supports a replace filter for common patterns. For example, if your branches are always feature/JIRA-1234, you could strip the prefix:

[project]
worktree-path = "{{ repo_path }}/../{{ repo }}.{{ branch | replace('feature/', '') | sanitize }}"

That would give you repo.JIRA-1234 instead of repo.feature-JIRA-1234 — but it's a global rule, not a per-invocation override, so it doesn't address the core request (choosing an arbitrary descriptive name like navigation-animation-refactor at creation time).

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

worktrunk-bot avatar Apr 07 '26 18:04 worktrunk-bot

A core part of Worktrunk is making a reliable mapping between branches and worktrees. So the bar would be extremely high for us to offer a way to deviate from that.

(wt step promote is one (the only?) example of us doing that)

probably the best way would be to make an alias which ran git worktree move with a vars. @worktrunk-bot could you give an example / options of what that would look like?

and then, as ever — if something catches on, then we can consider how to update worktrunk towards it

max-sixty avatar Apr 07 '26 20:04 max-sixty

Here are a couple of options:

Option 1: wt step alias (simplest)

# ~/.config/worktrunk/config.toml
[aliases]
rename = "git worktree move {{ worktree_path }} {{ repo_path }}/../{{ repo }}.{{ name }}"

Usage:

wt switch --create feature/JIRA-1234
wt step rename --var name=navigation-animation-refactor
cd ../myrepo.navigation-animation-refactor   # shell needs to follow the move

The {{ repo_path }}/../{{ repo }}.{{ name }} pattern mirrors the default worktree-path template, just with a custom leaf name instead of the sanitized branch. If you've customized worktree-path, the alias template should match your pattern.

Downside: After git worktree move, the current shell is still in the old (now-gone) directory, so you need a manual cd. The shell integration can't intercept this since the move happens inside the alias command.

Option 2: Shell function (handles the cd)

# In .zshrc / .bashrc
wt-rename() {
  local new_name="$1"
  local old_path="$(pwd)"
  local new_path="$(dirname "$old_path")/$new_name"
  git worktree move "$old_path" "$new_path" && cd "$new_path"
}

Usage:

wt switch --create feature/JIRA-1234
wt-rename navigation-animation-refactor

This one handles the cd automatically since it's a shell function. Doesn't use wt template variables, but it doesn't need to — it just moves wherever you currently are.


I'd lean toward Option 2 for ergonomics — the cd issue with Option 1 adds friction. Option 1 is nice if you want the rename path to follow your worktree-path template pattern though.

worktrunk-bot avatar Apr 07 '26 20:04 worktrunk-bot

ok. I think we could combine these two into an alias:

wt switch --create feature/JIRA-1234
wt step rename --var name=navigation-animation-refactor

...but fair point on the cd; that does somewhat require a shell function

max-sixty avatar Apr 07 '26 21:04 max-sixty

Yeah — the alias is cleaner for discoverability but the shell function is the only way to get the cd for free. If a pattern emerges from users doing this, a native wt move or wt rename command could handle the shell integration the same way wt switch does (via the shell wrapper).

worktrunk-bot avatar Apr 07 '26 21:04 worktrunk-bot

I guess this is a "me" problem ;)

The main argument for me is that I can organize my context as I see fit and Git natively already supports this behavior. To use only branch prefixes and issue numbers would result in a list of worktrees and directories like the following:

feature-JIRA-1234
feature-JIRA-2242
feature-JIRA-2664
...

There is a simple workaround which is to adjust my workflow start adding my custom context to the branch names:

feature-JIRA-1234-custom-context
feature-JIRA-2242-add-cool-system-fix
feature-JIRA-2664-change-some-behavior
...

It is additional noise unfortunately

christowiz avatar Apr 08 '26 18:04 christowiz

I guess this is a "me" problem ;)

ha, totally understandable, and I empathize. I do think that part of the role of Worktrunk is to set some norms so coordination becomes easier, even if that makes some existing workflows a bit harder

I'm genuine in suggesting trying to build something outside the norms, and then seeing how that goes. Or use vars to set vars on the worktrees and then use the structured data about them; like a jira ticket no.

max-sixty avatar Apr 08 '26 19:04 max-sixty

I would also like it if worktrunk could name the folders something completely irrelevant, like Conductor or Zed's built in worktrees. I personally don't need the branch name in the folder, it's included in my shell's statusline.

Something like {{ word }} and {{ city }} for use within the directory template using datasets similar to Zed/Conductor would be great.

endigma avatar Apr 29 '26 10:04 endigma

I implemented by own suggestion in https://github.com/max-sixty/worktrunk/pull/2641, you can now have random human readable directories instead of branch name.

endigma avatar May 09 '26 17:05 endigma

i share the exact same pain as @christowiz and i also understand this functionality may be too specific to be implemented in worktrunk.

With multi AI agents working on separate worktrees, it can become difficult to keep track of what each branch is for. I wonder if there are alternative ways to help users identify the purpose of a branch more easily.

for example, perhaps wt list could display an optional description column, allowing users to attach a short label or note to each worktree.

+-----------------+--------------------------------+
|     Branch      |          Description           |
+-----------------+--------------------------------+
| fix/JIRA-1234   | Bug fix for production fire    |
| feat/JIRA-1235  | New feature being implemented  |
| chore/JIRA-1236 | Updating Node version v20->v22 |
+-----------------+--------------------------------+

denisdifazio avatar May 27 '26 16:05 denisdifazio

yes @denisdifazio !

Two things:

  • Have you seen the summaries feature? It's not on by default because it fires off lots of Haiku calls, but it generates a summary of what's on the branch for wt switch or wt list --full. I think it's a bit hidden away atm, lmk any ideas for making it more available
  • I would be quite up for allowing custom vars in wt list. I worry a bit that it's a cooler feature to build than use; i.e. how helpful is it really? I could build something experimental if someone was confident they would use it. It would be something like:
    • in a list config, specify a templated string (allowing vars), the priority and the width (currently we want to paint the table skeleton really quickly, and we don't want shifting columns, so we need to know the width at create-time)
    • then wt list would get the vars for each branch and show that in the table
    • could also possibly be an templated expression / command

max-sixty avatar May 28 '26 03:05 max-sixty

Have you seen the summaries feature? It's not on by default because it fires off lots of Haiku calls, but it generates a summary of what's on the branch for wt switch or wt list --full. I think it's a bit hidden away atm, lmk any ideas for making it more available

i didn't know about that! This is exactly what i was looking for!

thanks @max-sixty

I would be quite up for allowing custom vars in wt list. I worry a bit that it's a cooler feature to build than use; i.e. how helpful is it really?

i liked that idea. Maybe it doesn't need to be as flexible in the first version. A middle-ground solution you be to drop the --full flag and let users decide which columns they want to display in wt list:

[list]
summary = boolen
status = boolean
branch-name = boolean
path = boolean
ci = boolean
...

denisdifazio avatar May 28 '26 20:05 denisdifazio

I would be quite up for allowing custom vars in wt list.

to round this out, this is now released!

max-sixty avatar Jun 29 '26 02:06 max-sixty

Just for the record because I'm not sure if I understand it correctly: The initial feature request is not possible? The worktree directory must match the branch name via the worktree-path template? I get that you could add additional info in the list but most other tools display the folder of the worktree as the name and I'm not always in charge of the branch names. Why do they need to be consistent if they can be mapped via git (git worktree list [--porcelain])?

krsilas avatar Aug 11 '26 19:08 krsilas

Not quite — the directory name doesn't have to encode the branch. Two things have changed since the earlier discussion here.

1. Branch-free directory names are supported. The codename(n) filter (added in 0.49.0 via #2641) generates deterministic friendly names from the branch, so you can drop the branch out of the path entirely:

worktree-path = "{{ repo_path }}/../{{ repo }}.{{ branch | codename(2) }}"

That gives myrepo.tenable-hookworm for feature/JIRA-9999 — stable across invocations, so it's the same directory every time.

2. For a one-off descriptive name, --config-set overrides the template for a single invocation — this is close to the --path flag the issue originally asked for:

$ wt --config-set 'worktree-path="{{ repo_path }}/../navigation-animation-refactor"' switch -c feature/JIRA-1234
✓ Created branch feature/JIRA-1234 from main and worktree @ /tmp/scratch/navigation-animation-refactor

Afterwards plain wt switch feature/JIRA-1234 finds it, and wt list shows it, with no override needed — because addressing is branch-first and the path is only ever a location. The one caveat is wt step relocate, which reconciles worktrees against the template and would offer to move it back to myrepo.feature-JIRA-1234; the off-template name isn't recorded anywhere.

That caveat is also the answer to "why do they need to be consistent": they don't need to match, but the path does need to be derivable. Worktrunk computes the expected path from the template rather than reading it back from git, which is what lets it create a worktree at a known location, detect drift, and relocate. A per-invocation name that lives only in git's worktree list would be invisible to that. Whether it's worth storing such a name is a maintainer call — @max-sixty already set the bar for deviating from the branch↔worktree mapping above.

Verification

Tested against wt v0.72.0-22-g8bf8ae44a in a scratch repo:

  • wt --config-set 'worktree-path="…/navigation-animation-refactor"' switch -c feature/JIRA-1234 → directory navigation-animation-refactor, git branch --show-current in it → feature/JIRA-1234.
  • Plain wt switch feature/JIRA-1234 (no override) → ○ Switched to worktree for feature/JIRA-1234 @ /tmp/scratch/navigation-animation-refactor.
  • wt step relocate --dry-run → feature/JIRA-1234: /tmp/scratch/navigation-animation-refactor → /tmp/scratch/myrepo.feature-JIRA-1234.
  • codename(2) on feature/JIRA-9999 → myrepo.tenable-hookworm, identical on a second run.

Source: template expansion and the filter registry in src/config/expansion.rs; creation-time validation checks only path occupancy, never a branch/path relationship (switch.rs); branch-first resolution in worktrees.rs.

worktrunk-bot avatar Aug 11 '26 19:08 worktrunk-bot

I'm fine with keeping the default behaviour. The cli could provide an additional option (eg: wt switch --path <folder-name> [--create <branchname>]) that allows creation and retrieval of worktrees with custom path names. Just git for-each-ref --format='%(worktreepath)' "refs/heads/<branch-name>" instead of worktree-path.

krsilas avatar Aug 11 '26 20:08 krsilas

Worth splitting your two halves, because retrieval is already there and creation nearly is.

Retrieval by path already works today. Every argument that names a worktree also accepts that worktree's own path — resolved after the branch, so a directory can never shadow a branch sharing its name (docs). Against the custom-named worktree from my last comment:

$ wt switch ../navigation-animation-refactor
○ Switched to worktree for feature/JIRA-1234 @ /tmp/scratch/navigation-animation-refactor

Absolute paths and ~-relative paths work the same way; relative ones resolve against -C. The one shape that doesn't is a bare leaf name — wt switch navigation-animation-refactor is read as a branch and fails with No branch named …, since branch-first ordering is what stops a stray directory from hijacking a branch name.

Creation is covered too, just verbosely — the --config-set recipe above. So a --path flag wouldn't unlock anything new; it would be shorthand for that plus, presumably, persistence.

That persistence is the real content of the request, and it's why this isn't a small flag. Worktrunk computes the expected path from the template rather than reading it back from git — that's what lets it create at a known location, detect drift, and offer wt step relocate. An off-template name isn't recorded anywhere, so relocate still offers to move it back:

$ wt step relocate --dry-run
○ 1 worktree would be relocated:
  feature/JIRA-1234: /tmp/scratch/navigation-animation-refactor → /tmp/scratch/myrepo.feature-JIRA-1234

A native --path would have to store the chosen name in per-worktree state and teach relocate to honour it — a new piece of persisted state per worktree, not just a flag. That's a maintainer call, and @max-sixty has set the bar for deviating from the branch↔worktree mapping above.

Verification

Tested against a local build at 8bf8ae44a in a scratch repo (/tmp/scratch/myrepo, worktree created via wt --config-set 'worktree-path="{{ repo_path }}/../navigation-animation-refactor"' switch -c feature/JIRA-1234):

Selector Result
wt switch /tmp/scratch/navigation-animation-refactor resolves → feature/JIRA-1234
wt switch ../navigation-animation-refactor resolves → feature/JIRA-1234
wt switch navigation-animation-refactor ✗ No branch named navigation-animation-refactor
wt switch feature/JIRA-1234 resolves → same worktree
wt list Path column shows ../navigation-animation-refactor
wt step relocate --dry-run offers to move it back to myrepo.feature-JIRA-1234

Resolution ladder (branch → path → verdict) in Repository::resolve_worktree / resolve_selector; the same wording is the primary source for the docs page at src/cli/mod.rs.

worktrunk-bot avatar Aug 11 '26 20:08 worktrunk-bot

You're right, and I overstated it last time. I wrote that worktrunk "computes the expected path from the template rather than reading it back from git" — that holds only for creation, wt step relocate, and display naming. Resolution and listing already read paths back from git, which is exactly what you're proposing.

The mechanism is git worktree list --porcelain rather than for-each-ref %(worktreepath), but it's the same information, and a superset: the ref-keyed atom can't express a detached worktree (no branch ref to hang the path on) or two checkouts of one branch, both of which the porcelain list carries.

So the retrieval half is already implemented — wt switch short-circuits on a resolved worktree before it ever expands the template. The code comment calls the template the slow path outright: "Resolving before the path template is also the fast path: an existing worktree answers without the ~7 git commands compute_worktree_path runs."

And the display shape you're after largely exists too. A worktree whose path is off-template gets named by its directory, not its branch:

$ wt step for-each -- git rev-parse --abbrev-ref HEAD
◎ Running in main...
◎ Running in feature/JIRA-5678...                              # on-template → branch name
◎ Running in navigation-animation-refactor (on feature/JIRA-1234)...   # off-template → dir name

What's genuinely left is narrower than a --path flag would suggest, and it splits the same way your comment does:

  • Creation has to compute a path — there's nothing to read back yet, since the worktree doesn't exist. --config-set already covers this per-invocation, so --path would be shorthand rather than new capability.
  • wt step relocate reconciles against the template, so it still offers to move an off-template worktree back. This is the one place a custom name would have to be persisted rather than read from git — git records where the worktree is, but not that you chose that name, so relocate can't tell an intentional name from drift.

So the open design question isn't retrieval at all; it's whether a per-worktree chosen path gets recorded so relocate leaves it alone. That's a maintainer call, and @max-sixty set the bar for it earlier in the thread.

Verification

Local build at 8bf8ae44a, scratch repo at /tmp/scratch/myrepo, worktree created with wt --config-set 'worktree-path="{{ repo_path }}/../navigation-animation-refactor"' switch -c feature/JIRA-1234.

Retrieval with no override in effect — both selectors land on the off-template path:

Command Result
wt switch feature/JIRA-1234 Worktree for feature/JIRA-1234 @ /tmp/scratch/navigation-animation-refactor
wt switch ../navigation-animation-refactor same worktree
wt list Path column → ../navigation-animation-refactor
wt step relocate --dry-run feature/JIRA-1234: /tmp/scratch/navigation-animation-refactor → /tmp/scratch/myrepo.feature-JIRA-1234

%(worktreepath) vs the porcelain list (git 2.54.0), showing the two shapes the ref-keyed atom drops:

$ git for-each-ref --format='%(refname:short) -> [%(worktreepath)]' refs/heads/
feature/JIRA-1234 -> [/tmp/feref/navigation-animation-refactor]
main -> [/tmp/feref/repo]
no-worktree -> []

$ git worktree list --porcelain          # the detached worktree has no refs/heads/ row above
worktree /tmp/feref/detached-wt
HEAD 3d4b709f7fca0aba7a8ef1e53f8754b1247151a0
detached

Source: worktree enumeration reads the porcelain list in Repository::list_worktrees; wt switch resolves first and only reaches the template for creation (switch.rs Phase 2 → Phase 3, where line 887 is commented "Compute expected path (only needed for create)"); the directory-name display form is worktree_display_name. Those three call sites plus relocate.rs are the only consumers of compute_worktree_path.

worktrunk-bot avatar Aug 11 '26 20:08 worktrunk-bot

So to resolve this issue, you would advise the author to create an alias or shell script with wt --config-set ... for creation and wt switch <relative-path> for switching? Give an example. Is there a feature that would be broken with this workflow? What else is the path mapping used for?

krsilas avatar Aug 11 '26 20:08 krsilas

Yes, and it's one alias rather than two — --create can ride in {{ args }}, so the same alias covers creation and materialising an existing branch:

# ~/.config/worktrunk/config.toml
[aliases]
new = '''wt --config-set 'worktree-path="{{ repo_path }}/../{{ name }}"' switch {{ args }}'''
$ wt new -c feature/JIRA-1234 --name=navigation-animation-refactor
✓ Created branch feature/JIRA-1234 from main and worktree @ /tmp/scratch/navigation-animation-refactor

$ wt new existing-branch --name=some-custom-dir      # no -c: existing branch, custom dir
✓ Created worktree for existing-branch @ /tmp/scratch/some-custom-dir

--name=… binds to {{ name }} and everything else forwards to {{ args }} via smart routing, so flags like -c and --yes pass straight through. {{ repo_path }}/../{{ name }} mirrors the shape of the default worktree-path, minus the {{ repo }}. prefix and branch leaf — match your own template if you've customised it.

And it cds. I got this wrong earlier in the thread when I said a shell function was needed — that was about git worktree move, not wt switch. wt switch, wt merge, and wt remove propagate the directory change to the parent shell even when invoked from an alias; I verified wt new … lands you in the new worktree. Afterwards plain wt switch feature/JIRA-1234 and wt switch ../navigation-animation-refactor both work with no override.

What breaks

One thing: wt step relocate (experimental) reconciles worktrees against the template, so it offers to move every custom-named worktree back to myrepo.feature-JIRA-1234. Nothing runs it automatically — it's an explicit wt step invocation — so the workflow is safe as long as you don't run it. That's the persistence gap from my last comment: git records where the worktree is, not that you chose the name.

Everything else I tried works unchanged: hooks fire with the custom {{ worktree_path }}, wt merge --yes merges and cleans up the custom directory and cds back to main, wt remove accepts either the branch or the path.

Cosmetically, wt list marks each off-template worktree with a dim-yellow ⚑ — and, as a side effect, turns the Path column on, since a path that doesn't follow the template carries information the branch column doesn't. For this use case that's arguably the feature you wanted:

  Branch             Status        HEAD±    main↕     main…±  Remote⇅  Path                              Commit   Age   Message
@ main                   ^                                             .                                 aa500db  now   init
+ feature/JIRA-5678     ⚑_                                             ../dark-mode-toggle               aa500db  now   init
+ feature/JIRA-1234     ⚑_                                             ../navigation-animation-refactor  aa500db  now   init

○ Showing 3 worktrees

What the path template is used for

compute_worktree_path — the only thing that expands worktree-path — has exactly four call sites, which is the whole surface a custom name touches:

Consumer Effect on a custom-named worktree
Creation (wt switch -c) The only place it's load-bearing — there's no worktree to read back yet, so a path must be computed. This is what the alias overrides.
wt step relocate Sees the mismatch as drift; offers to move it back.
Display naming (worktree_display_name) Off-template worktrees are named by directory: navigation-animation-refactor (on feature/JIRA-1234) in wt step for-each.
wt list state flag Sets ⚑ and worktree.state: "branch_worktree_mismatch" in --format=json; makes the Path column informative.

Resolution, listing, merge, remove, and hooks never consult it — they read paths back from git worktree list --porcelain, exactly as you proposed.

Verification

Local build at 8bf8ae44a, scratch repo /tmp/scratch/myrepo (default branch main), bash with shell integration installed, the alias above as the only user config.

Alias expansion:

$ wt config alias dry-run new -- -c feature/JIRA-7777 --name=api-rate-limits
○ Alias new (user) would run:
  # bound: name=api-rate-limits
  # args: -c feature/JIRA-7777
  wt --config-set 'worktree-path="/tmp/scratch/myrepo/../api-rate-limits"' switch -c feature/JIRA-7777
Scenario Result
wt new -c feature/JIRA-5678 --name=dark-mode-toggle in bash worktree at /tmp/scratch/dark-mode-toggle; $PWD after → same path (cd propagated through the alias)
wt switch feature/JIRA-5678 (no override) resolves → /tmp/scratch/dark-mode-toggle
wt switch ../navigation-animation-refactor resolves → feature/JIRA-1234
wt new existing-branch --name=some-custom-dir ✓ Created worktree for existing-branch @ /tmp/scratch/some-custom-dir
post-start hook in project config ◎ Running post-start: greet (project) @ /tmp/scratch/search-indexing
wt merge --yes from /tmp/scratch/payments-retry merged, removed the custom dir, $PWD after → /tmp/scratch/myrepo
wt remove existing-branch --yes / wt remove ../dark-mode-toggle --yes both removed
wt step relocate --dry-run offers to move both back to myrepo.feature-JIRA-*
wt list --format=json "worktree": {"state": "branch_worktree_mismatch", "detached": false} on both custom rows

Failure modes, all safe:

$ wt new feature/JIRA-9999                                  # --name omitted
✗ Failed to expand new: undefined value @ line 1
  wt --config-set 'worktree-path="{{ repo_path }}/../{{ name }}"' switch {{ args }}
↳ Available variables: args, branch, cwd, main_worktree, repo, repo_path, repo_root, worktree, worktree_name, worktree_path

$ wt new -c feature/JIRA-8888 --name=dark-mode-toggle       # name already taken
✗ Cannot switch to feature/JIRA-8888 — there's a worktree at the expected path /tmp/scratch/dark-mode-toggle on branch feature/JIRA-5678

$ wt new -c existing-branch --name=some-custom-dir          # branch already exists
✗ Branch existing-branch already exists
↳ To switch to the existing branch, run without --create: wt switch existing-branch

Source: the four compute_worktree_path call sites are switch.rs:888 (guarded by the create path), relocate.rs:203, and — via is_worktree_at_expected_path — worktree_display_name and list/collect/mod.rs:1150. The Path-column trigger is layout.rs:1216; the state-symbol priority is item.rs:31-43.

worktrunk-bot avatar Aug 11 '26 21:08 worktrunk-bot