Skip to content

Commit efa0a9e

Browse files
committed
cancel aborts operation
1 parent cbe1e94 commit efa0a9e

3 files changed

Lines changed: 44 additions & 24 deletions

File tree

‎cmd/sync.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ down and appended to your local stack so it mirrors the remote. A clean
4444
"remote is ahead" update happens automatically without prompting. If the
4545
local and remote stacks have diverged, sync prompts (in an interactive
4646
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.
47+
as the source of truth, disassociate them, or cancel. Cancelling — or a
48+
divergence in a non-interactive terminal — aborts the sync without pushing
49+
branches or updating PRs.
4950
5051
If a rebase conflict is detected, all branches are restored to their
5152
original state and you are advised to run "gh stack rebase" to resolve
@@ -56,7 +57,7 @@ links PRs that already exist. The final message reflects what happened:
5657
"Stack synced" means the stack object on GitHub now matches your local
5758
stack, while "Branches synced" means the branches were rebased and pushed
5859
but no remote stack object was created or updated (for example, when fewer
59-
than two PRs exist yet, or a divergence was left unresolved).
60+
than two PRs exist yet).
6061
6162
Use --prune to delete local branches for merged PRs. Stack metadata is
6263
preserved so that rebase and display logic continue to work correctly.
@@ -125,6 +126,13 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
125126
if reconcileRes.stack != nil {
126127
s = reconcileRes.stack
127128
}
129+
if reconcileRes.abort {
130+
// The user cancelled a divergence (or it can't be resolved
131+
// non-interactively). Stop before touching any branches or PRs.
132+
cfg.Printf("")
133+
cfg.Infof("Sync aborted — no changes were made")
134+
return nil
135+
}
128136

129137
// --- Step 2: Fast-forward trunk ---
130138
trunk := s.Trunk.Branch

‎cmd/sync_test.go‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,17 +2119,20 @@ func divergentRemoteMock() *github.MockClient {
21192119
}
21202120
}
21212121

2122-
// TestSync_Divergent_NonInteractive_SafeNoOp verifies that a divergence in a
2123-
// non-interactive terminal is a safe no-op: no stack API mutations, guidance is
2124-
// printed, the association is preserved, and it exits success.
2125-
func TestSync_Divergent_NonInteractive_SafeNoOp(t *testing.T) {
2122+
// TestSync_Divergent_NonInteractive_Aborts verifies that a divergence in a
2123+
// non-interactive terminal aborts the sync: no branches are pushed, no stack API
2124+
// mutations occur, guidance is printed, the association is preserved, and it
2125+
// exits successfully.
2126+
func TestSync_Divergent_NonInteractive_Aborts(t *testing.T) {
21262127
tmpDir := t.TempDir()
21272128
divergentStack(t, tmpDir)
21282129

21292130
ghMock := divergentRemoteMock()
21302131
var created []string
2132+
var pushed bool
21312133
mock := newSyncMockNoRebase(tmpDir, "b1")
21322134
mock.CreateBranchFn = func(name, base string) error { created = append(created, name); return nil }
2135+
mock.PushFn = func(string, []string, bool, bool) error { pushed = true; return nil }
21332136
ghMock.CreateStackFn = func([]int) (int, error) { t.Fatal("CreateStack must not be called"); return 0, nil }
21342137
ghMock.UpdateStackFn = func(string, []int) error { t.Fatal("UpdateStack must not be called"); return nil }
21352138
ghMock.DeleteStackFn = func(string) error { t.Fatal("DeleteStack must not be called"); return nil }
@@ -2138,8 +2141,10 @@ func TestSync_Divergent_NonInteractive_SafeNoOp(t *testing.T) {
21382141
require.NoError(t, err)
21392142

21402143
assert.Empty(t, created)
2144+
assert.False(t, pushed, "branches must not be pushed when sync aborts")
21412145
assert.Contains(t, output, "diverged")
2142-
assert.Contains(t, output, "Branches synced")
2146+
assert.Contains(t, output, "Sync aborted")
2147+
assert.NotContains(t, output, "Branches synced")
21432148
assert.NotContains(t, output, "Stack synced")
21442149

21452150
sf, err := stack.Load(tmpDir)
@@ -2283,7 +2288,9 @@ func TestSync_Divergent_Cancel(t *testing.T) {
22832288
ghMock.DeleteStackFn = func(string) error { t.Fatal("DeleteStack must not be called"); return nil }
22842289
ghMock.CreateStackFn = func([]int) (int, error) { t.Fatal("CreateStack must not be called"); return 0, nil }
22852290
ghMock.UpdateStackFn = func(string, []int) error { t.Fatal("UpdateStack must not be called"); return nil }
2291+
var pushed bool
22862292
mock := newSyncMockNoRebase(tmpDir, "b1")
2293+
mock.PushFn = func(string, []string, bool, bool) error { pushed = true; return nil }
22872294

22882295
output, err := runSyncCfg(t, mock, func(cfg *config.Config) {
22892296
cfg.GitHubClientOverride = ghMock
@@ -2292,8 +2299,10 @@ func TestSync_Divergent_Cancel(t *testing.T) {
22922299
})
22932300
require.NoError(t, err)
22942301

2295-
assert.Contains(t, output, "still differ")
2296-
assert.Contains(t, output, "Branches synced")
2302+
assert.False(t, pushed, "branches must not be pushed when the user cancels")
2303+
assert.Contains(t, output, "Sync aborted")
2304+
assert.NotContains(t, output, "Branches synced")
2305+
assert.NotContains(t, output, "Stack synced")
22972306

22982307
sf, err := stack.Load(tmpDir)
22992308
require.NoError(t, err)

‎cmd/utils.go‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,9 +1200,14 @@ type remoteReconcileResult struct {
12001200
stack *stack.Stack
12011201

12021202
// skipStackObjectSync tells runSync to skip the later syncStack step so we
1203-
// never push the local stack over a divergent remote. Set for cancel,
1204-
// disassociate, and unresolved (non-interactive) divergences.
1203+
// never push the local stack over a divergent remote. Set when disassociating.
12051204
skipStackObjectSync bool
1205+
1206+
// abort tells runSync to stop the whole sync immediately (before any
1207+
// fast-forward, rebase, or push) and exit successfully without changes. Set
1208+
// when the user cancels an interactive divergence prompt, or when a
1209+
// divergence is detected in a non-interactive terminal.
1210+
abort bool
12061211
}
12071212

12081213
// remoteStackClass classifies the relationship between the local stack's active
@@ -1407,11 +1412,9 @@ func pullRemoteAdditions(cfg *config.Config, sf *stack.StackFile, s *stack.Stack
14071412

14081413
// resolveStackDivergence handles a stack whose local composition has diverged
14091414
// from the remote (neither is a prefix of the other). In an interactive
1410-
// terminal it prompts the user; otherwise it is a safe no-op that reports the
1411-
// divergence and leaves everything untouched.
1415+
// terminal it prompts the user to resolve it; otherwise (or when the user
1416+
// cancels) it aborts the sync so nothing is pushed or updated.
14121417
func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *stack.StackFile, s *stack.Stack, gitDir, remote string, prs []*github.PullRequest, remoteActive []string) (remoteReconcileResult, error) {
1413-
res := remoteReconcileResult{skipStackObjectSync: true}
1414-
14151418
cfg.Printf("")
14161419
cfg.Warningf("Your local stack has diverged from the stack on GitHub")
14171420
cfg.Printf(" Local: %s", s.DisplayChain())
@@ -1420,14 +1423,14 @@ func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *sta
14201423
if !cfg.IsInteractive() {
14211424
cfg.Printf(" Re-run in an interactive terminal to resolve, or import the remote stack with `%s`.",
14221425
cfg.ColorCyan("gh stack checkout <pr>"))
1423-
return res, nil
1426+
return remoteReconcileResult{abort: true}, nil
14241427
}
14251428

14261429
options := []string{
14271430
"Use the remote stack as the source of truth (update local to match)",
14281431
"Use your local stack as the source of truth (update remote to match)",
1429-
"Disassociate — stop tracking this stack on GitHub",
1430-
"Cancel — make no changes for now",
1432+
"Disassociate — unlink local stack with remote on GitHub",
1433+
"Cancel — abort the sync and do not make any changes",
14311434
}
14321435
p := prompter.New(cfg.In, cfg.Out, cfg.Err)
14331436
selectFn := func(prompt, def string, opts []string) (int, error) {
@@ -1436,17 +1439,17 @@ func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *sta
14361439
}
14371440
return p.Select(prompt, def, opts)
14381441
}
1439-
selected, err := selectFn("How would you like to resolve this?", "", options)
1442+
selected, err := selectFn("How would you like to resolve?", "", options)
14401443
if err != nil {
14411444
if isInterruptError(err) {
14421445
if cfg.SelectFn == nil {
14431446
clearSelectPrompt(cfg, len(options))
14441447
}
14451448
printInterrupt(cfg)
1446-
return res, errInterrupt
1449+
return remoteReconcileResult{abort: true}, errInterrupt
14471450
}
14481451
cfg.Errorf("selection failed: %v", err)
1449-
return res, ErrSilent
1452+
return remoteReconcileResult{}, ErrSilent
14501453
}
14511454

14521455
switch selected {
@@ -1457,8 +1460,8 @@ func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *sta
14571460
case 2:
14581461
return resolveDivergenceDisassociate(cfg, sf, s, gitDir)
14591462
default:
1460-
cfg.Infof("No changes made — your local and remote stacks still differ")
1461-
return res, nil
1463+
// Cancel: abort the whole sync without touching branches or PRs.
1464+
return remoteReconcileResult{abort: true}, nil
14621465
}
14631466
}
14641467

0 commit comments

Comments
 (0)