Stacked branches
I often work on a feature branch and dispatch jobs to multiple agents into worktrees created from with that feature branch as base. I'd like to be able to see the git info (diff, commits ahead\behind etc) for these worktrees against that branch instead of default one
wt list could take --base argument
wt switch does take --base, but not in the interactive mode
Thanks for the suggestion! I searched the codebase and confirmed that neither wt list nor wt switch (interactive mode) currently supports overriding the comparison base.
Here's what I found:
wt list: The "main…±" diff column and "main↕" ahead/behind column are both hardcoded to compare against the default branch (main/master). There's no--baseflag or config option to override this.wt switch --base: The--baseflag exists but is restricted to direct mode with--create(for specifying the starting point of a new branch). It's not passed through to the interactive picker at all.
So both parts of the request — wt list --base <branch> and --base in interactive wt switch — would be new functionality.
I'll leave it for a maintainer to evaluate and prioritize.
Stacked branches could be v interesting; it would be a fairly big change though, so we'd need to think a lot how broad to make it — whether to encode this in the worktree data / allow passing at CLI / etc...
Cross-posting from #1637 — a post-start hook can capture the base branch today using existing features:
post-start = [
"wt config state vars set base_branch='{{ base }}'",
]
After that, {{ vars.base_branch }} is available in all future hooks for that worktree. A merge alias could reference it:
[aliases]
merge-base = "wt merge {{ vars.base_branch }}"
Limitations:
- Only works for worktrees created after the hook is added (no backfill for existing ones)
- If
--baseisn't specified,{{ base }}resolves to the default branch, so it'd store that — which is already the merge default, making it redundant in that case. It's most useful when--baseis explicitly set to something else
also note https://github.com/pablospe/worktrunk-sync is related
Hey. I would also like to be able to not worry about which worktree got created from which base.
It would be nice if the base of a worktree would be (stored somewhere, is it stored somewhere?) and taken into account by wt step rebase so that my stacked branch is not merged into the default branch.
No, the base isn't stored natively today — --base is consumed at create time and not persisted on the worktree. The post-start hook recipe above is the workaround: it captures {{ base }} into vars.base_branch so later commands can read it back.
For wt step rebase specifically, the command already takes a positional target (wt step rebase <target>, defaults to the default branch), so the same recipe threads through:
[aliases]
rebase-base = "wt step rebase {{ vars.base_branch }}"
Same caveats as before — only worktrees created after the hook is added get the variable, and you'd need to set it explicitly when --base matches the default branch. Whether the base should be stored natively (and respected by wt merge / wt step rebase automatically) is the bigger design question @max-sixty flagged.
I just had an agent open up a 10 PR stack, lol
And the problem is that my wt switch is now pretty much useless
I've been doing more stacks recently - it lets me give the agent a big job, but I get an opportunity to give more granular feedback and review on the details of each stage. I'm happy to see GitHub's stack improvements, would be awesome of wt supported this workflow a bit better too.
The columns in that second screenshot are all merge-base-relative to the default branch, so in a stack every row reports everything below it too — that's the ↑68/↑67/↑66 ladder, and the picker's diff/committed preview tabs are anchored the same way (the tab shows the whole stack's diff, not what the branch adds). There's no per-invocation override: the base comes from Repository::default_branch(), whose only user-facing lever is git config worktrunk.default-branch — which is the wrong tool here, since it also retargets wt merge and wt step prune's merged-branch detection.
What does work today is a custom column that carries the stack structure itself. {{ git.branch.* }} reads the branch's own branch.<name>.* git config, so you can record each branch's parent there — including for the ten that already exist — and render it. Inferring the parent is just "nearest ancestor branch", which is one git merge-base --is-ancestor sweep:
#!/usr/bin/env bash
# Record each local branch's nearest ancestor branch, and how many commits it
# adds on top of it, in that branch's own git config.
set -euo pipefail
default=$(wt config state default-branch get)
branches=$(git for-each-ref --format='%(refname:short)' refs/heads/)
for b in $branches; do
if [ "$b" != "$default" ]; then
parent=$(for c in $branches; do
if [ "$c" != "$b" ] && git merge-base --is-ancestor "$c" "$b"; then
printf '%s %s\n' "$(git rev-list --count "$c..$b")" "$c"
fi
done | sort -n | head -1 | cut -d' ' -f2-)
[ -n "$parent" ] || parent=$default
git config "branch.$b.base" "$parent"
git config "branch.$b.baseahead" "$(git rev-list --count "$parent..$b")"
fi
done
Then in user config:
[list.custom-columns.Stack]
template = "{% if git.branch.base %}{{ git.branch.base }} ↑{{ git.branch.baseahead }}{% endif %}"
priority = 3
On a three-branch stack over main, the built-in column and the custom one side by side:
Branch Status HEAD± main↕ main…± Remote⇅ Path Stack Commit Age Message
@ main ^ . 9650895 2m main 4
/ stack-1 /↕ ↑2 ↓1 +2 main ↑2 14db231 2m stack1 2
/ stack-2 /↕ ↑5 ↓1 +5 stack-1 ↑3 e1b0a0a 2m stack2 3
/ stack-3 /↕ ↑6 ↓1 +6 stack-2 ↑1 1c43fcc 2m stack3 1
wt switch's picker shares the same column set, so the rows there pick it up too — that's where I checked it, not only in wt list. If main↕ and main…± are pure noise in a stack-heavy repo, [list] columns = ["branch", "status", "Stack", "ci", "age", "message"] drops them and keeps the custom one. To try it without touching your config at all:
wt --config-set '[list.custom-columns.Stack]
template = "{% if git.branch.base %}{{ git.branch.base }} ↑{{ git.branch.baseahead }}{% endif %}"' list --branches
Caveats worth stating plainly: baseahead is a snapshot, so it's stale until you re-run the script (the base half only changes when you restructure the stack, so it ages much more slowly); the inference picks the nearest ancestor branch and falls back to the default branch for anything nothing else sits under, so it reads a linear stack correctly but won't invent an order for unrelated branches; and custom columns are still marked experimental. None of this touches what wt merge or wt step rebase target — for that the post-start recipe above is still the lever.
@max-sixty happy to add this to the stacked-branches FAQ next to the worktrunk-sync pointer if it's worth documenting, though it's also exactly the kind of thing the native design question would replace.
have you tried worktrunk-sync? (I haven't been paying attention so no view but interested if it's working well....)
wt sync — rebase stacked worktree branches in dependency order
I haven't. I tend to run things stock - I did very few stacked PRs until GitHub shipped their PR stack feature. At least according the README, wt sync helps with rebasing stacked worktrees in dependency order. I have found that the agents do that 100% no problem, they don't seem to need help there. I'm the one who needs the help, just looking at the state.
I'm the one who needs the help, just looking at the state.
😄
yes; I do think this is a sufficiently big feature that it would require a real champion to own the design and translate it into an implementation, and likely need to pick between competing approaches; and given I don't use stacks that much, I'm skeptical I'm the champion you need
I would encourage someone who is keen to build the plugin! I'm happy to help in any way to let it work well with worktrunk / ensure all of the worktrunk primitives are available. I think it's mostly about design rather than complicated git interfaces; worktrunk hopefully does that part already...
I built this (https://gist.github.com/nixpulvis/b64e12b00df5f372502594a6e968a8a7), which I think is a pretty good start on the implementation.
Here's it running on a project of mine at the moment:
$ wt list-base
Branch Status HEAD± Base Base…± Remote⇅ CI Age
^ master ? ^| +101 | # 3w
+ faster-json ↑ ↑9 json-tokens +101 -40 2w
+ exact-number ↑| ↑2 pmap +70 -8 | #23 2w
+ json-tokens ↑| ↑3 skip-space +518 -245 | #22 2w
+ star-loop ↑| ↑1 pmap +9 -11 | #26 2w
+ where-condition-first ↑| ↑1 pmap +13 -13 | #24 2w
+ skip-space ↑| ↑2 pmap +21 -31 | #21 2w
+ pmap ↑| ↑3 master +382 -197 | #30 2w
+ variadic-seq ↑| ↑1 master +163 -115 | # 2w
+ lit-loop ↑| ↑1 master +4 -3 | #25 2w
+ opt-direct ↑| ↑1 master +5 -1 | # 2w
+ wip-todo ↑ ↑2 master +87 -18 2w
+ faster-json-wip ↕ ↑1 ↓1 master +139 -47 3w
One of the limitations in that README was a worktrunk bug, and #4775 fixes it. | int (and | float) on a git.branch.* or vars.* value failed only in the config-load trial render. That render answered every key with the string "PLACEHOLDER", so invalid float literal aborted wt list even though the template renders fine against the real values. With the fix, a template like {% if (git.branch.baseahead | default(0) | int) > 0 %} validates. I reproduced the failure on current main and checked the fix with a --config-set column, so once it lands the string-comparison workarounds in Base / Base…± can go.
I also reproduced two other limitations from the list on current main:
- No styling in custom columns. ANSI escapes in a template's output are stripped in both table and JSON output. There's no style option for a column.
list.columnscan't come from an env var.WORKTRUNK_LIST__COLUMNS='["branch","age"]'is ignored withexpected a sequence.--config-set, which the alias already uses, is the working route.
Those two, plus the Status arrows and the picker previews staying anchored to the default branch, are the primitive gaps this design runs into, as opposed to the base-inference logic itself.
@nixpulvis (or @worktrunk-bot if you can work it out from @nixpulvis 's code) — let me know any general changes that would make it easier to build this sort of thing; have tried to make worktrunk reasonable extensible, happy to do more in that direction!
Reading through @nixpulvis's gist, almost all of its ~300 lines of shell and template go to rebuilding numbers wt already computes, just against a different ref. Inferring the base, which is the hard and opinionated part, is a small share of it. From most leverage to least, these are the general changes:
- A per-branch comparison base that wt reads, for example from a
branch.<name>.*key a plugin writes. Themain↕andmain…±columns and the picker's diff/summary preview panes all get their base from one rule,select_comparison_base, and today it resolves once per repo. If it could resolve per branch, a stacking plugin would only infer the base and write one key. wt would then compute ahead/behind and the diff stat itself, cached as now, and the built-in columns, theStatusarrows and the picker previews would follow. That makesBase/Base…±, thebaseahead/basekeycaching, and thelist-basewrapper alias unnecessary. Merged-branch detection (wt step prune, the integrated markers) should still use the default branch, since a branch merged into its parent isn't done. This is also the narrow version of the native feature: wt stores and displays a base, and choosing it stays in the plugin. - Freshness, if (1) isn't done. Custom columns can only read stored values, so something has to refresh them before each render. That's why the gist needs a separate
wt list-basecommand, and why plainwt listand the picker show whatever the last run wrote. Under (1) this mostly goes away, because the base changes only when the stack is restructured. - Styling in custom columns. ANSI in a template's output is stripped, and there's no per-column style option, so
↑3 ↓1 stack-1can't be colored like the built-in columns. - List-valued env vars.
WORKTRUNK_LIST__COLUMNS='["branch","age"]'is rejected withexpected a sequence.--config-setworks, so this one is minor.
The | int validation limitation from the gist's README is already fixed in #4775, which is merged.