Skip to content

feat(wt sync): add core rebase engine (part 01/03) - #2059

Closed
pablospe wants to merge 6 commits into
max-sixty:mainfrom
pablospe:sync-core
Closed

pablospe wants to merge 6 commits into
max-sixty:mainfrom
pablospe:sync-core

Conversation

@pablospe

Copy link
Copy Markdown
Contributor

Summary

  • Add wt sync command that rebases stacked worktree branches in topological order
  • Builds a dependency tree via merge-base inference with true-ancestor/diverged tiers
  • Detects integrated branches (merged into default branch) and reparents their children
  • Includes --stack flag to sync only the current stack and --dry-run for previewing

Stack: 1/3 — next: sync-stack-file (stack file persistence), then sync-flags (--fetch/--push/--prune + config)

Test plan

  • 10 integration tests covering: linear stacks, fan-out, mid-stack changes, main advances, deep stacks, PR merged to main, dirty worktree abort, dry-run, --stack flag, up-to-date detection
  • 3 unit tests for topological ordering and stack containment
  • clippy clean, all existing tests pass

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice foundation for wt sync. The merge-base-based parent inference with two-tier (true-ancestor / diverged) selection is a reasonable approach, and the integration-reparent path is well-structured. A few findings:

1. print_tree_node produces flat output — the tree drawing is broken.

Both connector and child_prefix special-case prefix.is_empty(), returning "" in that case. But the recursion passes child_prefix as the next call's prefix, so every descendant also sees prefix == "" and never gets a connector. The "└── " / "├── " / "│ " / " " branches are unreachable, which is why codecov flags them as uncovered.

The sync_dry_run_linear_stack.snap snapshot confirms it — the output is:

Dependency tree:
main
pr1
pr2

instead of the intended:

Dependency tree:
main
└── pr1
    └── pr2

Easiest fix: handle the root once in print_sync_plan and call print_tree_node only for children with an unconditional connector. See the inline suggestion on src/commands/sync.rs.

2. --all is a no-op in this PR.

SyncArgs::all is declared in src/cli/mod.rs with conflicts_with = "stack", but handle_sync_command derives let all = !args.stack; and never reads args.all. So wt sync --all behaves identically to wt sync. The help text says "overrides config", but config isn't introduced until #2061.

I'd suggest dropping --all from this PR and adding it in #2061 where it actually overrides something — or at least wiring it up so args.all forces all = true even when (eventually) a config default of stack applies. Leaving a user-visible flag whose only effect is being rejected alongside --stack is confusing.

3. codecov/patch is failing at 81%.

Beyond the dead-code lines from the tree bug, the biggest untested surface is the rebase-conflict handling in both branches of handle_sync (lines 474–492 for rebase --onto and 503–521 for the normal rebase) — is_rebasing() / user-facing hints / early return Ok(()) are entirely untested. Given conflict recovery is a core behavior of this command, it's worth adding at least one integration test that forces a conflict and checks the error path. The bail! when the default branch has no worktree (line 153) and the "in-progress rebase" guard (lines 425–430) are also uncovered.

4. Stacked with #2053.

#2053 (same author, same feature) is still open with the monolithic version. If #2059/#2060/#2061 supersede it, closing #2053 would avoid confusion for other reviewers.

Minor:

  • handle_sync_command calls CommandEnv::for_action_branchless()? and binds to _env, then handle_sync immediately calls Repository::current()? again. The _env does nothing — removing it avoids a duplicate repo/config load.
  • build_dependency_tree unwraps repo.integration_target() to get target_ref, then passes it to repo.integration_reason(branch, target_ref), which internally calls effective_integration_target(target) again. Passing &default_branch directly achieves the same result.
  • error_message and hint_message are used with a fully-qualified worktrunk::styling:: path while the module-top imports already bring in eprintln, progress_message, success_message, warning_message. Adding them to the import keeps the rebase-error blocks readable.

Comment thread src/commands/sync.rs Resolved
Comment thread src/cli/mod.rs Resolved
@pablospe
pablospe force-pushed the sync-core branch 2 times, most recently from 1f4227c to 173ce6c Compare April 10, 2026 21:39
@pablospe pablospe changed the title feat: add wt sync core rebase engine feat(wt sync): add core rebase engine (part 01/03) Apr 10, 2026
Core implementation of `wt sync` that detects parent-child relationships
between worktree branches and rebases them in topological order.

Features:
- Automatic dependency tree detection via merge-base analysis
- Topological sort for correct rebase ordering
- Integration detection (squash-merged branches) with reparenting
- Dirty worktree detection to prevent data loss
- --dry-run mode to preview planned rebases
- --stack flag to sync only the current stack (default: all stacks)

Includes 10 integration tests covering linear stacks, deep stacks,
mid-stack changes, dirty worktree blocking, and PR merge scenarios.
The root call passed empty prefix, and children of root also received
empty prefix, making them all appear as root nodes. Fix by passing an
explicit is_root flag so only the actual root prints without connectors.
The --all field was never read (main.rs derives `all` from `!args.stack`),
and its help text referenced config that doesn't exist yet. Remove the
dead field and its conflicts_with constraint.
Covers the conflict handling branch that shows error and resolution
hint when a rebase conflict occurs during sync.
@pablospe
pablospe marked this pull request as draft April 11, 2026 07:43
@pablospe

Copy link
Copy Markdown
Contributor Author

Moved to a standalone binary: https://github.com/pablospe/worktrunk-sync — works via the external subcommand dispatch merged in #2054.

@pablospe

Copy link
Copy Markdown
Contributor Author

Superseded — sync moved to separate repo: https://github.com/pablospe/worktrunk-sync

@pablospe pablospe closed this Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants