Skip to content

Commit 346a8ca

Browse files
committed
show message if stack fully merged
1 parent c99a0c4 commit 346a8ca

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎cmd/checkout.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,26 @@ func checkoutRemoteStack(cfg *config.Config, sf *stack.StackFile, gitDir string,
194194
// Determine trunk (base branch of the first PR) and the target branch
195195
trunk := prs[0].BaseRefName
196196
var targetBranch string
197+
allMerged := true
197198
for _, pr := range prs {
198199
if pr.Number == prNumber {
199200
targetBranch = pr.HeadRefName
200-
break
201+
}
202+
if !pr.Merged {
203+
allMerged = false
201204
}
202205
}
203206
if targetBranch == "" {
204207
cfg.Errorf("could not determine branch for PR #%d", prNumber)
205208
return nil, "", ErrAPIFailure
206209
}
207210

211+
if allMerged {
212+
cfg.Infof("All PRs in this stack have been merged")
213+
cfg.Printf("To start a new stack, use `%s`", cfg.ColorCyan("gh stack init"))
214+
return nil, "", ErrSilent
215+
}
216+
208217
remoteStackID := strconv.Itoa(remoteStack.ID)
209218

210219
// Step 3: Check if the target branch is already in a local stack

‎cmd/checkout_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,41 @@ func TestCheckout_NumericTarget_MergedBranchDeletedFromRemote(t *testing.T) {
684684
assert.Contains(t, output, "Imported stack with 2 branches")
685685
}
686686

687+
func TestCheckout_NumericTarget_AllPRsMerged(t *testing.T) {
688+
gitDir := t.TempDir()
689+
690+
restore := git.SetOps(&git.MockOps{
691+
GitDirFn: func() (string, error) { return gitDir, nil },
692+
CurrentBranchFn: func() (string, error) { return "main", nil },
693+
})
694+
defer restore()
695+
696+
require.NoError(t, stack.Save(gitDir, &stack.StackFile{SchemaVersion: 1, Stacks: []stack.Stack{}}))
697+
698+
cfg, outR, errR := config.NewTestConfig()
699+
cfg.GitHubClientOverride = &github.MockClient{
700+
ListStacksFn: func() ([]github.RemoteStack, error) {
701+
return []github.RemoteStack{
702+
{ID: 70, PullRequests: []int{10, 11}},
703+
}, nil
704+
},
705+
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
706+
prs := map[int]*github.PullRequest{
707+
10: {ID: "PR_10", Number: 10, HeadRefName: "feat-1", BaseRefName: "main", Merged: true, State: "MERGED", URL: "https://github.com/o/r/pull/10"},
708+
11: {ID: "PR_11", Number: 11, HeadRefName: "feat-2", BaseRefName: "feat-1", Merged: true, State: "MERGED", URL: "https://github.com/o/r/pull/11"},
709+
}
710+
return prs[number], nil
711+
},
712+
}
713+
714+
err := runCheckout(cfg, &checkoutOptions{target: "11"})
715+
output := collectOutput(cfg, outR, errR)
716+
717+
assert.ErrorIs(t, err, ErrSilent)
718+
assert.Contains(t, output, "All PRs in this stack have been merged")
719+
assert.Contains(t, output, "gh stack init")
720+
}
721+
687722
func TestCheckout_NumericTarget_APIError(t *testing.T) {
688723
gitDir := t.TempDir()
689724
restore := git.SetOps(&git.MockOps{

0 commit comments

Comments
 (0)