Repository navigation
Conversation
There was a problem hiding this comment.
I will remove this before merging, but it helps me to run some tests with github.
commented
Apr 10, 2026
|
thanks for the effort are there ways we could break this up? it's not really viable to review 3.5K lines of code! what benefits could we see from more incremental changes, at first? |
Detects the branch dependency tree from git's commit graph using pairwise merge-base analysis, then rebases each branch onto its parent in topological order. Key behaviors: - No configuration needed — dependencies are inferred from git history - By default, only syncs the stack containing the current branch - `--all` syncs all worktree branches - `--dry-run` shows the dependency tree and planned rebases - Detects integrated (squash-merged) branches and reparents children using `rebase --onto` - Dirty worktree pre-check prevents partial sync failures
Flip the default: `wt sync` now syncs all worktree branches across all stacks. Use `--stack` to limit to the current stack only. Also improve the after_long_help documentation with a visual example showing the difference between default and --stack behavior.
Add optional phases to `wt sync`: - --fetch: run `git fetch --prune` before syncing - --push: force-push rebased branches with --force-with-lease - --prune: remove integrated worktrees (worktree + local branch + remote) All phases configurable via [sync] section in user/project config: [sync] fetch = false all = true push = false prune = false CLI flags override config: --fetch/--no-fetch, --push/--no-push, --prune/--no-prune, --stack/--all.
Add explicit stack definition via `.git/wt/stack` file (git-machete compatible format) to solve the mid-stack commit parent detection limitation. When this file exists, `wt sync` reads parents from it instead of inferring from the commit graph. - `wt sync --save` persists the inferred dependency tree - Stack file uses indentation-based tree format (tabs or spaces) - Supports comments (#) and annotations after branch names - Add [sync] config section documentation - Fix test_valid_user_config_keys_all_deserialize for sync section
When a stack file exists, `wt sync` now checks integration against each branch's explicit parent (not just main). This detects merges between stacked branches — e.g., PR2 squash-merged into PR1. Children of integrated branches are reparented by walking up the stack file's parent chain to find the first non-integrated ancestor, instead of always falling back to the default branch.
The stack file (.git/wt/stack) is now always persisted after building the dependency tree. This ensures parent-based integration detection (e.g., PR merged into non-default branch) works from the first sync without requiring the user to remember --save.
Add two integration tests covering scenarios previously only tested E2E: - test_sync_mid_stack_pr_merged_into_parent: PR3 squash-merged into its parent PR2 (mid-stack, not main). Verifies PR4 reparents onto PR2. - test_sync_cascading_merges: PR2 merged into PR1, then PR1 merged into main. Verifies PR3 correctly reparents through both merges.
Includes a self-contained E2E test that creates a real GitHub repo, builds a 5-PR stack, and validates sync scenarios (rebase, squash-merge reparenting, non-default branch integration detection, push with pruned remotes). Will be removed before merging.
Add remove_fixture_worktrees() and worktree prune to all sync tests. The standard TestRepo fixture includes feature-a/b/c worktrees that caused spurious "equidistant parents" warnings and "Failed to execute: git status --porcelain" errors in sync test snapshots.
Covers the gap where --push skips branches whose remote was deleted (e.g., after a PR merge on GitHub). Previously only tested in E2E.
- Fix stale --save references in module docstring and comments (removed in earlier commit, but references remained) - Fix module docstring claiming default is current-stack-only (actual default is all stacks) - Remove dead code: empty if-block and unused line_num suppression - Add visited set to reparenting loop to prevent infinite loops from cycles in hand-edited stack files - Update --prune help text to mention remote branch deletion Skipped: parse_stack_file indent handling (tabs vs spaces) — edge case with hand-edited files, format_stack_file always writes tabs. Conflict handling, push failure, prune failure test coverage — straightforward print-and-continue error paths, not worth the test complexity.
- Remove unused line_num from parse_stack_file enumerate loop - On cycle detection in reparenting, fall back to default branch instead of leaving new_parent pointing at an integrated branch
Squash-merges PR2 via GitHub, then syncs with --fetch --prune --push. Verifies worktree removal, stack file update, remote branch deletion, and correct reparenting of downstream branches.
The prune phase was deleting the local branch with `branch -D` before checking for an upstream tracking branch. This meant the upstream check always returned None, so remote branches were never cleaned up. Found by E2E Test 7 (--prune with real GitHub repo).
Resolve snapshot conflict from {{ owner }} template variable added in main.
a64bd9c to
cfee132
Compare
commented
Apr 10, 2026
|
I am primarily seeking feedback on this because it involves several design decisions. I've been experimenting with the new
Yes, if you exclude the tests, it is a "reasonable" size, except for I can address some of the cases in separate PRs, not sure how to split it, need to think. One thing I need to remove is the test-sync-e2e.sh, so please don't review it. I included temporarily so you could also run it and see how I'm testing this. Perhaps you have other ideas there too, besides the unit tests I addeed. |
1 similar comment
commented
Apr 10, 2026
|
I am primarily seeking feedback on this because it involves several design decisions. I've been experimenting with the new
Yes, if you exclude the tests, it is a "reasonable" size, except for I can address some of the cases in separate PRs, not sure how to split it, need to think. One thing I need to remove is the test-sync-e2e.sh, so please don't review it. I included temporarily so you could also run it and see how I'm testing this. Perhaps you have other ideas there too, besides the unit tests I addeed. |
left a comment
There was a problem hiding this comment.
A few notes from a fresh read.
CI is failing on three fixable items — all from this PR, none flaky:
- Three clippy errors in
src/commands/sync.rs(let_and_returnat L182, twocollapsible_ifat L318 and L422). Inline suggestions below. - Four shell-completion snapshots are missing the new
syncsubcommand (bash_completion_subcommands,zsh_completion_subcommands,fish_completion_subcommands,nushell_completion_subcommandsundertests/integration_tests/snapshots/). They each listswitch / list / remove / merge / step / hook / config;syncneeds to slot in betweenremoveandmergeto match theCommandsenum order insrc/cli/mod.rs:578.cargo insta test --acceptwill regenerate them.
Data-safety question on --prune. src/commands/sync.rs:826 calls git worktree remove --force, which silently deletes any untracked files in the integrated worktree (build artifacts, scratch notes, an unrelated .env, etc.). CLAUDE.md says: "Prefer failure over silent data loss" and "Operations that force-remove data (like --force on remove) require the user to explicitly request that behavior". --prune is opt-in, but it doesn't read like "also discard untracked files" — and wt remove itself requires an explicit --force for that. Worth either dropping --force here (let prune fail loudly when a worktree has untracked files, with a hint to clean it up) or wiring through a separate --prune-force so the destructive variant is named.
Same spot: L843 and L846 swallow errors with let _ = for branch -D and push --delete. If the local branch delete fails (e.g., still checked out somewhere unexpected), the user gets a misleading "Removed integrated worktree" success message. Worth at least logging the error.
Stack-file write happens before the dirty-worktree check (L582 vs L624). Aborting on a dirty worktree still leaves a freshly written .git/wt/stack behind. Probably benign (the file reflects the current tree either way), but moving the write below the safety checks would make it less surprising.
(Acknowledging test-sync-e2e.sh is already noted for removal.)
- Inline `let_and_return` in stack file parser - Collapse nested `if let` + `if` into `if let &&` (two instances)
Drop --force to avoid silently deleting untracked files in integrated worktrees. Without --force, git worktree remove fails loudly when the worktree has uncommitted changes, with a hint to clean up manually.
@max-sixty I am fixing some errors from the bot, that I will include here, but what about this split:
Note: I got inspired by |
commented
Apr 10, 2026
|
my main claims are:
I personally don't use stacked branches, though I would a bit if there were better approaches I think two possible approaches from here:
|
Replace `let _ =` on branch -D and push --delete with proper warning messages so users know when cleanup partially fails.
Move the stack file write to after dirty-worktree and rebase-in-progress checks so aborting on dirty worktrees doesn't leave a side-effect. Dry-run still writes the stack file since it reflects the current tree.
commented
Apr 10, 2026
Notice if I split this PR then I will need to use stacked PRs :) and possibly have a chance to test One question, what do you mean with "builds an add-on"? How would it work? |
Add sync to bash, fish, zsh, and nushell completion subcommand snapshots.
commented
Apr 10, 2026
|
Addressed all points:
|
commented
Apr 10, 2026
@worktrunk-bot please add this generic functionality in a new PR |
|
I split it into three PRs:
@max-sixty Let me know your preference. Do you think we can merge this, or would you prefer to keep it separate from the main repository? Should I close this and the other PRs? |
commented
Apr 10, 2026
|
I had meant: what is something incremental — but still useful — that we can merge on our way to something more complete? There's a huge space of possibilities with stacked branches, so we want to avoid choosing a single point in the space and releasing that immediately, because the first point we think of is unlikely to be the best one. Releasing things incrementally lets us experiment with it, get feedback from others, etc (Similarly: we wouldn't add a top-level command, it would be a If you think we need the full solution to get value, then releasing does that make sense? sorry if I was unclear above |
commented
Apr 10, 2026
|
Thanks for clarifying — that makes sense. I agree iterating is better than committing to a big design upfront. I want to finish addressing a few review fixes on the current PRs first, then I'll mark them as draft or close them (#2059, #2060, #2061). After that, I'll ship this as a separate A couple of questions:
|
commented
Apr 10, 2026
yes, it would be your project; I'd be happy to look if you wanted any help, but your repo etc!
it merged! |
commented
Apr 10, 2026
Ok, let me close the PRs then. Thanks! |
commented
Apr 10, 2026
|
A few questions on the
I'll finish addressing the review fixes on the current PRs (the CI |
commented
Apr 14, 2026
@max-sixty I was wondering how you would link it. In the documentation?
If you'd like to take a look, here's the link: Thanks for the help! |
commented
Apr 14, 2026
|
excellent! nice work! @worktrunk-bot where could this go in the docs? let's put a link in Extending Worktrunk as an example. Maybe also as a Q&A? |
commented
Apr 14, 2026
|
@pablospe I'm very sorry I just saw your questions from a few days ago. Which of those are still outstanding? Do you need anything adjusted in this repo? Happy to make items public etc. |
commented
Apr 14, 2026
|
Opened #2225 with both — an |
commented
Apr 14, 2026
Thanks! Here's where things stand:
Also, thanks for the docs link in #2225! |
commented
Apr 14, 2026
|
@worktrunk-bot please make the changes in #4 so those are public. list options we should consider in refactors. this doesn't have to be the final interface we ever choose, but it shouldn't be a mess; and we are fine to do some refactors now in order to make the interface nicer. if you feel confident, also open a PR in |

Summary
wt sync— auto-detects the branch dependency tree from git history and rebases each branch onto its parent in topological orderrebase --onto.git/wt/stack) is auto-created on every sync for reliable parent tracking and non-default branch integration detectionScenarios
1. Main advances — all stacked branches rebase in order
wt syncrebases PR1 onto main, PR2 onto PR1, PR3 onto PR2.2. Mid-stack change — downstream branches update
wt syncdetects PR2 is behind PR1, rebases PR2 and PR3.3. PR squash-merged to main — children reparent with
rebase --ontowt syncdetects PR1 is integrated (reuseswt list's⊂detection), reparents PR2 onto main viarebase --onto, then rebases PR3 onto PR2.4. PR squash-merged into non-default parent — stack file enables detection
Without a stack file,
integration_reason(PR2, main)returns nothing — PR2 isn't merged into main. With the stack file, sync knows PR2's parent is PR1 and checksintegration_reason(PR2, PR1), detecting the integration and reparenting PR3 onto PR1.5. Cascading merges — multiple levels collapse correctly
The reparenting logic walks up the parent chain to find the first non-integrated ancestor, handling arbitrary nesting depth.
The
--stackflagBy default,
wt syncsyncs all stacks. With--stack, only the current stack is synced.Say you have two independent stacks:
If main advances:
wt sync(default — syncs everything):wt sync --stack(from pr-auth's worktree — only stack A):Why not
git rebase --update-refs?Git 2.38 added
--update-refsfor stacked branches, but it explicitly skips branches checked out in worktrees:Git silently skips those refs without warning. Since worktrees are the core of worktrunk,
wt syncrebases each branch individually in its own worktree, in topological order.Stack file (
.git/wt/stack)The dependency tree is persisted to
.git/wt/stackon every sync. The format is git-machete compatible (indentation-based, one branch per line):The stack file is central to sync's reliability. It enables three things:
Stable parent tracking — after rebasing + mid-stack commits, the parent branch becomes "diverged" from the commit graph's perspective and merge-base inference may pick the wrong parent. The stack file preserves the correct parent relationships across syncs.
Non-default branch integration detection — when a stacked PR is squash-merged into another stacked branch (not main),
integration_reason(branch, main)returns nothing. The stack file tells sync to also checkintegration_reason(branch, parent), catching these merges.Reparenting with parent chain walking — when an integrated branch's parent is also integrated (cascading merges), sync walks up the stack file's parent chain to find the first non-integrated ancestor, rather than always falling back to main.
The file is human-editable — adjust indentation to change the tree. After a PR merges, sync automatically removes integrated branches from the file.
CLI
All flags have config equivalents in
[sync]:Conflict handling
On conflict, sync stops at the failing branch with a hint:
Algorithm
Parent detection (first sync or no stack file)
For each branch, find the closest parent via pairwise merge-base +
count_commits:merge_base(candidate, branch)for every other branchbranch_depth= commits from merge-base to branch tipcandidate_depth= commits from merge-base to candidate tipcandidate_depth == 0, meaning the candidate's tip is reachable from the branch): pick the one with smallestbranch_depthbranch_depth, then smallestcandidate_depthTrue ancestors are preferred over diverged candidates to prevent cycles in stacked branches.
Integration detection (two-phase)
Subsequent syncs (stack file exists)
Parents are read from the stack file instead of inferred. The stack file is updated after every sync to reflect the current tree (integrated branches removed, new worktrees added).
Test plan
--stackflag, default-syncs-all, up-to-date detection,--fetch,--push,--pushwith deleted remote,--prunetest-sync-e2e.sh)