Skip to content

Commit 7797931

Browse files
committed
simplified options to remote delete
1 parent 4cb3f22 commit 7797931

3 files changed

Lines changed: 53 additions & 103 deletions

File tree

‎cmd/sync.go‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ If PRs have been added to the stack on GitHub, their branches are pulled
4343
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
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. Cancelling — or a
46+
terminal) to use the remote as the source of truth, delete the stack on
47+
GitHub and recreate it later with sync/submit, or cancel. Cancelling — or a
4848
divergence in a non-interactive terminal — aborts the sync without pushing
4949
branches or updating PRs.
5050
@@ -126,11 +126,11 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
126126
if reconcileRes.stack != nil {
127127
s = reconcileRes.stack
128128
}
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")
129+
if reconcileRes.stop {
130+
// The reconcile step resolved the situation and there is nothing more to
131+
// do (the user cancelled or deleted the remote stack, or a divergence was
132+
// detected non-interactively). The resolving path already reported the
133+
// outcome, so just exit successfully.
134134
return nil
135135
}
136136
// Reconciling "use remote as source of truth" may have moved us off a
@@ -277,10 +277,8 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
277277
// stack object actually reflects the local stack, which determines the final
278278
// summary message below.
279279
stackSynced := false
280-
if !reconcileRes.skipStackObjectSync {
281-
if client, err := cfg.GitHubClient(); err == nil {
282-
stackSynced = syncStack(cfg, client, s)
283-
}
280+
if client, err := cfg.GitHubClient(); err == nil {
281+
stackSynced = syncStack(cfg, client, s)
284282
}
285283

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

‎cmd/sync_test.go‎

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,73 +2272,42 @@ func TestSync_Divergent_UseRemote_DirtyBlocked(t *testing.T) {
22722272
assert.Equal(t, "9", sf.Stacks[0].ID)
22732273
}
22742274

2275-
// TestSync_Divergent_UseLocal deletes the diverged remote stack and recreates it
2276-
// from the local stack.
2277-
func TestSync_Divergent_UseLocal(t *testing.T) {
2275+
// TestSync_Divergent_DeleteRemote deletes the diverged remote stack, clears the
2276+
// local association, and stops the sync (pointing the user at submit) without
2277+
// recreating the stack or pushing.
2278+
func TestSync_Divergent_DeleteRemote(t *testing.T) {
22782279
tmpDir := t.TempDir()
22792280
divergentStack(t, tmpDir)
22802281

22812282
deleted := false
22822283
var deletedID string
2283-
var createdWith []int
2284-
ghMock := &github.MockClient{
2285-
ListStacksFn: func() ([]github.RemoteStack, error) {
2286-
if deleted {
2287-
return nil, nil
2288-
}
2289-
return []github.RemoteStack{{ID: 9, PullRequests: []int{101, 102, 104}}}, nil
2290-
},
2291-
FindPRByNumberFn: prByNumberFinder(map[int]string{101: "b1", 102: "b2", 103: "b3", 104: "b4"}),
2292-
FindPRForBranchFn: openPRFinder(map[string]int{"b1": 101, "b2": 102, "b3": 103}),
2293-
DeleteStackFn: func(id string) error { deleted = true; deletedID = id; return nil },
2294-
CreateStackFn: func(prs []int) (int, error) { createdWith = prs; return 12, nil },
2295-
}
2296-
mock := newSyncMockNoRebase(tmpDir, "b1")
2297-
2298-
output, err := runSyncCfg(t, mock, func(cfg *config.Config) {
2299-
cfg.GitHubClientOverride = ghMock
2300-
cfg.ForceInteractive = true
2301-
cfg.SelectFn = func(_, _ string, _ []string) (int, error) { return 1, nil }
2302-
})
2303-
require.NoError(t, err)
2304-
2305-
assert.True(t, deleted, "remote stack should be deleted")
2306-
assert.Equal(t, "9", deletedID)
2307-
assert.Equal(t, []int{101, 102, 103}, createdWith, "remote recreated from local PRs")
2308-
assert.Contains(t, output, "Stack synced")
2309-
2310-
sf, err := stack.Load(tmpDir)
2311-
require.NoError(t, err)
2312-
assert.Equal(t, "12", sf.Stacks[0].ID, "adopts the recreated stack ID")
2313-
}
2314-
2315-
// TestSync_Divergent_Disassociate clears the remote stack ID without touching
2316-
// either the remote stack object or the local branches.
2317-
func TestSync_Divergent_Disassociate(t *testing.T) {
2318-
tmpDir := t.TempDir()
2319-
divergentStack(t, tmpDir)
2320-
2284+
var pushed bool
23212285
ghMock := divergentRemoteMock()
2322-
ghMock.DeleteStackFn = func(string) error { t.Fatal("DeleteStack must not be called"); return nil }
2286+
ghMock.DeleteStackFn = func(id string) error { deleted = true; deletedID = id; return nil }
23232287
ghMock.CreateStackFn = func([]int) (int, error) { t.Fatal("CreateStack must not be called"); return 0, nil }
23242288
ghMock.UpdateStackFn = func(string, []int) error { t.Fatal("UpdateStack must not be called"); return nil }
23252289
mock := newSyncMockNoRebase(tmpDir, "b1")
2290+
mock.PushFn = func(string, []string, bool, bool) error { pushed = true; return nil }
23262291

23272292
output, err := runSyncCfg(t, mock, func(cfg *config.Config) {
23282293
cfg.GitHubClientOverride = ghMock
23292294
cfg.ForceInteractive = true
2330-
cfg.SelectFn = func(_, _ string, _ []string) (int, error) { return 2, nil }
2295+
cfg.SelectFn = func(_, _ string, _ []string) (int, error) { return 1, nil }
23312296
})
23322297
require.NoError(t, err)
23332298

2334-
assert.Contains(t, output, "independent")
2335-
assert.Contains(t, output, "Branches synced")
2299+
assert.True(t, deleted, "remote stack should be deleted")
2300+
assert.Equal(t, "9", deletedID)
2301+
assert.False(t, pushed, "sync should stop after deleting the remote stack")
2302+
assert.Contains(t, output, "Deleted the stack on GitHub")
2303+
assert.Contains(t, output, "gh stack submit")
23362304
assert.NotContains(t, output, "Stack synced")
2305+
assert.NotContains(t, output, "Branches synced")
23372306

23382307
sf, err := stack.Load(tmpDir)
23392308
require.NoError(t, err)
2340-
assert.Equal(t, "", sf.Stacks[0].ID, "remote stack ID is cleared")
2341-
assert.Equal(t, []string{"b1", "b2", "b3"}, sf.Stacks[0].BranchNames())
2309+
assert.Equal(t, "", sf.Stacks[0].ID, "local association is cleared")
2310+
assert.Equal(t, []string{"b1", "b2", "b3"}, sf.Stacks[0].BranchNames(), "local branches untouched")
23422311
}
23432312

23442313
// TestSync_Divergent_Cancel makes no changes and preserves the association.
@@ -2357,7 +2326,7 @@ func TestSync_Divergent_Cancel(t *testing.T) {
23572326
output, err := runSyncCfg(t, mock, func(cfg *config.Config) {
23582327
cfg.GitHubClientOverride = ghMock
23592328
cfg.ForceInteractive = true
2360-
cfg.SelectFn = func(_, _ string, _ []string) (int, error) { return 3, nil }
2329+
cfg.SelectFn = func(_, _ string, _ []string) (int, error) { return 2, nil }
23612330
})
23622331
require.NoError(t, err)
23632332

‎cmd/utils.go‎

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,15 +1200,11 @@ type remoteReconcileResult struct {
12001200
// original pointer).
12011201
stack *stack.Stack
12021202

1203-
// skipStackObjectSync tells runSync to skip the later syncStack step so we
1204-
// never push the local stack over a divergent remote. Set when disassociating.
1205-
skipStackObjectSync bool
1206-
1207-
// abort tells runSync to stop the whole sync immediately (before any
1208-
// fast-forward, rebase, or push) and exit successfully without changes. Set
1209-
// when the user cancels an interactive divergence prompt, or when a
1210-
// divergence is detected in a non-interactive terminal.
1211-
abort bool
1203+
// stop tells runSync to end the sync immediately after reconcile (before any
1204+
// fast-forward, rebase, or push) and exit successfully. Set when the user
1205+
// cancels or deletes the remote stack, and when a divergence is detected in a
1206+
// non-interactive terminal. The resolving path prints its own outcome message.
1207+
stop bool
12121208
}
12131209

12141210
// remoteStackClass classifies the relationship between the local stack's active
@@ -1424,14 +1420,14 @@ func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *sta
14241420
if !cfg.IsInteractive() {
14251421
cfg.Printf(" Re-run in an interactive terminal to resolve, or import the remote stack with `%s`.",
14261422
cfg.ColorCyan("gh stack checkout <pr>"))
1427-
return remoteReconcileResult{abort: true}, nil
1423+
cfg.Infof("Sync aborted — no changes were made")
1424+
return remoteReconcileResult{stop: true}, nil
14281425
}
14291426

14301427
options := []string{
1431-
"Use the remote stack as the source of truth (update local to match)",
1432-
"Use your local stack as the source of truth (update remote to match)",
1433-
"Disassociate — unlink local stack with remote on GitHub",
1434-
"Cancel — abort the sync and do not make any changes",
1428+
"Update local to match remote — replace your local stack with the remote version",
1429+
"Delete the remote stack on GitHub — keep your local stack and recreate on remote later",
1430+
"Cancel — make no changes",
14351431
}
14361432
p := prompter.New(cfg.In, cfg.Out, cfg.Err)
14371433
selectFn := func(prompt, def string, opts []string) (int, error) {
@@ -1447,7 +1443,7 @@ func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *sta
14471443
clearSelectPrompt(cfg, len(options))
14481444
}
14491445
printInterrupt(cfg)
1450-
return remoteReconcileResult{abort: true}, errInterrupt
1446+
return remoteReconcileResult{stop: true}, errInterrupt
14511447
}
14521448
cfg.Errorf("selection failed: %v", err)
14531449
return remoteReconcileResult{}, ErrSilent
@@ -1457,12 +1453,11 @@ func resolveStackDivergence(cfg *config.Config, client github.ClientOps, sf *sta
14571453
case 0:
14581454
return resolveDivergenceUseRemote(cfg, sf, s, currentBranch, gitDir, remote, prs)
14591455
case 1:
1460-
return resolveDivergenceUseLocal(cfg, client, sf, s, gitDir)
1461-
case 2:
1462-
return resolveDivergenceDisassociate(cfg, sf, s, gitDir)
1456+
return resolveDivergenceDeleteRemote(cfg, client, sf, s, gitDir)
14631457
default:
1464-
// Cancel: abort the whole sync without touching branches or PRs.
1465-
return remoteReconcileResult{abort: true}, nil
1458+
// Cancel: stop the sync without touching branches or PRs.
1459+
cfg.Infof("Sync aborted — no changes were made")
1460+
return remoteReconcileResult{stop: true}, nil
14661461
}
14671462
}
14681463

@@ -1476,7 +1471,6 @@ func resolveDivergenceUseRemote(cfg *config.Config, sf *stack.StackFile, s *stac
14761471

14771472
if dirty, err := git.HasUncommittedChanges(); err == nil && dirty {
14781473
cfg.Errorf("You have uncommitted changes — commit or stash them before replacing your local stack with the remote")
1479-
res.skipStackObjectSync = true
14801474
return res, ErrSilent
14811475
}
14821476

@@ -1487,7 +1481,6 @@ func resolveDivergenceUseRemote(cfg *config.Config, sf *stack.StackFile, s *stac
14871481
removeLocalStack(sf, s)
14881482
newStack, err := importRemoteStack(cfg, sf, gitDir, remote, trunk, prs, remoteStackID)
14891483
if err != nil {
1490-
res.skipStackObjectSync = true
14911484
return res, err
14921485
}
14931486

@@ -1503,7 +1496,6 @@ func resolveDivergenceUseRemote(cfg *config.Config, sf *stack.StackFile, s *stac
15031496
}
15041497

15051498
if err := stack.Save(gitDir, sf); err != nil {
1506-
res.skipStackObjectSync = true
15071499
return res, handleSaveError(cfg, err)
15081500
}
15091501

@@ -1539,43 +1531,34 @@ func nearestBranchAfterReplace(oldBranches []string, currentBranch string, newSt
15391531
return newStack.Trunk.Branch
15401532
}
15411533

1542-
// resolveDivergenceUseLocal makes the remote match the local stack by deleting
1543-
// the diverged remote stack object and clearing the local ID. The normal
1544-
// syncStack step then recreates the stack from the local PR list (when two or
1545-
// more PRs exist). Remote PRs dropped from the stack remain open but unstacked.
1546-
func resolveDivergenceUseLocal(cfg *config.Config, client github.ClientOps, sf *stack.StackFile, s *stack.Stack, gitDir string) (remoteReconcileResult, error) {
1547-
var res remoteReconcileResult
1534+
// resolveDivergenceDeleteRemote deletes the diverged stack object on GitHub and
1535+
// removes the local association (clearing the stack ID). The PRs and local
1536+
// branches are left untouched — only the stack grouping on GitHub is removed. It
1537+
// stops the sync and points the user at `gh stack submit` to recreate the stack
1538+
// (which, unlike sync, also creates PRs for any un-submitted branches).
1539+
func resolveDivergenceDeleteRemote(cfg *config.Config, client github.ClientOps, sf *stack.StackFile, s *stack.Stack, gitDir string) (remoteReconcileResult, error) {
1540+
res := remoteReconcileResult{stop: true}
15481541

15491542
if err := client.DeleteStack(s.ID); err != nil {
15501543
var httpErr *api.HTTPError
15511544
if errors.As(err, &httpErr) && httpErr.StatusCode == 404 {
15521545
cfg.Warningf("Remote stack already deleted")
15531546
} else {
15541547
cfg.Errorf("failed to delete remote stack: %v", err)
1555-
res.skipStackObjectSync = true
15561548
return res, ErrAPIFailure
15571549
}
15581550
} else {
1559-
cfg.Successf("Deleted the diverged stack on GitHub — it will be recreated from your local stack")
1551+
cfg.Successf("Deleted the stack on GitHub")
15601552
}
15611553

15621554
s.ID = ""
15631555
if err := stack.Save(gitDir, sf); err != nil {
1564-
res.skipStackObjectSync = true
15651556
return res, handleSaveError(cfg, err)
15661557
}
1567-
return res, nil
1568-
}
15691558

1570-
// resolveDivergenceDisassociate removes the local stack's remote stack ID so
1571-
// the local and remote stacks become independent. The remote stack object is
1572-
// left untouched; syncStack is skipped so nothing is pushed over it.
1573-
func resolveDivergenceDisassociate(cfg *config.Config, sf *stack.StackFile, s *stack.Stack, gitDir string) (remoteReconcileResult, error) {
1574-
res := remoteReconcileResult{skipStackObjectSync: true}
1575-
s.ID = ""
1576-
if err := stack.Save(gitDir, sf); err != nil {
1577-
return res, handleSaveError(cfg, err)
1578-
}
1579-
cfg.Successf("Stopped tracking this stack on GitHub — your local and remote stacks are now independent")
1559+
cfg.Printf("")
1560+
cfg.Printf("Your PRs and local branches are unchanged — only the stack on GitHub was removed.")
1561+
cfg.Printf(" Run `%s` to recreate the stack on GitHub.", cfg.ColorCyan("gh stack submit"))
1562+
cfg.Printf(" Run `%s` first if you want to change the stack's structure.", cfg.ColorCyan("gh stack modify"))
15801563
return res, nil
15811564
}

0 commit comments

Comments
 (0)