Skip to content

Commit cbe1e94

Browse files
committed
pull in remote updates during sync
1 parent 8e7863a commit cbe1e94

4 files changed

Lines changed: 818 additions & 26 deletions

File tree

‎cmd/checkout.go‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -491,21 +491,9 @@ func importRemoteStack(
491491
// Skip merged PRs whose branches were deleted from the remote —
492492
// these no longer exist upstream and can't be created locally.
493493
for _, pr := range prs {
494-
branch := pr.HeadRefName
495-
if git.BranchExists(branch) {
496-
continue
497-
}
498-
remoteRef := remote + "/" + branch
499-
if err := git.CreateBranch(branch, remoteRef); err != nil {
500-
if pr.Merged {
501-
cfg.Infof("Skipping merged branch %s", branch)
502-
continue
503-
}
504-
cfg.Errorf("failed to pull branch %s from %s: %v", branch, remoteRef, err)
505-
return nil, ErrSilent
494+
if _, err := ensureLocalBranchFromRemote(cfg, remote, pr); err != nil {
495+
return nil, err
506496
}
507-
_ = git.SetUpstreamTracking(branch, remote)
508-
cfg.Successf("Pulled branch %s", branch)
509497
}
510498

511499
// Build the stack

‎cmd/sync.go‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@ func SyncCmd(cfg *config.Config) *cobra.Command {
2626
Short: "Sync the current stack with the remote",
2727
Long: `Fetch, rebase, push, and sync PR state for the current stack.
2828
29-
This command performs a safe, non-interactive synchronization:
29+
This command performs a safe synchronization:
3030
3131
1. Fetches the latest changes from the remote
32-
2. Fast-forwards the trunk branch to match the remote
33-
3. Cascade-rebases stack branches onto their updated parents
34-
4. Pushes all branches atomically (using --force-with-lease --atomic)
35-
5. Syncs PR state from GitHub
36-
6. Links the stack's open PRs into a stack on GitHub (creating or updating
32+
2. Reconciles the stack on GitHub with your local stack: pulls down
33+
branches for any PRs added to the stack on GitHub, or prompts you to
34+
resolve a divergence in an interactive terminal
35+
3. Fast-forwards the trunk branch to match the remote
36+
4. Cascade-rebases stack branches onto their updated parents
37+
5. Pushes all branches atomically (using --force-with-lease --atomic)
38+
6. Syncs PR state from GitHub
39+
7. Links the stack's open PRs into a stack on GitHub (creating or updating
3740
the remote stack object) when two or more PRs exist
3841
42+
If PRs have been added to the stack on GitHub, their branches are pulled
43+
down and appended to your local stack so it mirrors the remote. A clean
44+
"remote is ahead" update happens automatically without prompting. If the
45+
local and remote stacks have diverged, sync prompts (in an interactive
46+
terminal) to use the remote as the source of truth, use your local stack
47+
as the source of truth, disassociate them, or cancel. In a non-interactive
48+
terminal a divergence is reported and left untouched.
49+
3950
If a rebase conflict is detected, all branches are restored to their
4051
original state and you are advised to run "gh stack rebase" to resolve
4152
conflicts interactively.
@@ -45,7 +56,7 @@ links PRs that already exist. The final message reflects what happened:
4556
"Stack synced" means the stack object on GitHub now matches your local
4657
stack, while "Branches synced" means the branches were rebased and pushed
4758
but no remote stack object was created or updated (for example, when fewer
48-
than two PRs exist yet).
59+
than two PRs exist yet, or a divergence was left unresolved).
4960
5061
Use --prune to delete local branches for merged PRs. Stack metadata is
5162
preserved so that rebase and display logic continue to work correctly.
@@ -99,6 +110,22 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
99110
_ = git.FetchBranches(remote, fetchTargets)
100111
cfg.Successf("Fetched latest changes from %s", remote)
101112

113+
// --- Step 1b: Reconcile remote-ahead stack changes ---
114+
// Pull in branches for PRs that were added to the stack on GitHub, or
115+
// resolve a divergence, before rebasing and pushing so pulled branches
116+
// participate in the normal flow. Best-effort for stacks tracked on the
117+
// remote; a no-op otherwise.
118+
reconcileRes, err := reconcileRemoteStack(cfg, sf, s, gitDir, remote)
119+
if err != nil {
120+
if errors.Is(err, errInterrupt) {
121+
return ErrSilent
122+
}
123+
return err
124+
}
125+
if reconcileRes.stack != nil {
126+
s = reconcileRes.stack
127+
}
128+
102129
// --- Step 2: Fast-forward trunk ---
103130
trunk := s.Trunk.Branch
104131
trunkUpdated := fastForwardTrunk(cfg, trunk, remote, currentBranch)
@@ -237,8 +264,10 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
237264
// stack object actually reflects the local stack, which determines the final
238265
// summary message below.
239266
stackSynced := false
240-
if client, err := cfg.GitHubClient(); err == nil {
241-
stackSynced = syncStack(cfg, client, s)
267+
if !reconcileRes.skipStackObjectSync {
268+
if client, err := cfg.GitHubClient(); err == nil {
269+
stackSynced = syncStack(cfg, client, s)
270+
}
242271
}
243272

244273
// --- Step 6: Prune merged branches (optional) ---

0 commit comments

Comments
 (0)