55 "fmt"
66 "os"
77 "path/filepath"
8+ "strings"
89
910 "github.com/github/gh-stack/internal/config"
1011 "github.com/github/gh-stack/internal/git"
@@ -132,6 +133,10 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
132133 currentIdx = 0
133134 }
134135
136+ if opts .upstack && currentIdx >= 0 && s .Branches [currentIdx ].IsMerged () {
137+ cfg .Warningf ("Current branch %q has already been merged" , currentBranch )
138+ }
139+
135140 startIdx := 0
136141 endIdx := len (s .Branches )
137142
@@ -180,7 +185,7 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
180185
181186 // Skip branches whose PRs have already been merged (e.g. via squash).
182187 // Record state so subsequent branches can use --onto rebase.
183- if br .PullRequest != nil && br . PullRequest . Merged {
188+ if br .IsMerged () {
184189 ontoOldBase = originalRefs [br .Branch ]
185190 needsOnto = true
186191 cfg .Successf ("Skipping %s (PR #%d merged)" , br .Branch , br .PullRequest .Number )
@@ -192,7 +197,7 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
192197 newBase := s .Trunk .Branch
193198 for j := absIdx - 1 ; j >= 0 ; j -- {
194199 b := s .Branches [j ]
195- if b . PullRequest == nil || ! b .PullRequest . Merged {
200+ if ! b .IsMerged () {
196201 newBase = b .Branch
197202 break
198203 }
@@ -289,13 +294,13 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
289294
290295 for i := range s .Branches {
291296 // Skip merged branches when updating base SHAs.
292- if s .Branches [i ].PullRequest != nil && s . Branches [ i ]. PullRequest . Merged {
297+ if s .Branches [i ].IsMerged () {
293298 continue
294299 }
295300 // Find the first non-merged ancestor, or trunk.
296301 parent := s .Trunk .Branch
297302 for j := i - 1 ; j >= 0 ; j -- {
298- if s . Branches [ j ]. PullRequest == nil || ! s .Branches [j ].PullRequest . Merged {
303+ if ! s .Branches [j ].IsMerged () {
299304 parent = s .Branches [j ].Branch
300305 break
301306 }
@@ -311,6 +316,15 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
311316
312317 _ = stack .Save (gitDir , sf )
313318
319+ merged := s .MergedBranches ()
320+ if len (merged ) > 0 {
321+ names := make ([]string , len (merged ))
322+ for i , m := range merged {
323+ names [i ] = m .Branch
324+ }
325+ cfg .Printf ("Skipped %d merged %s: %s" , len (merged ), plural (len (merged ), "branch" , "branches" ), strings .Join (names , ", " ))
326+ }
327+
314328 rangeDesc := "All branches in stack"
315329 if opts .downstack {
316330 rangeDesc = fmt .Sprintf ("All downstack branches up to %s" , currentBranch )
@@ -380,7 +394,7 @@ func continueRebase(cfg *config.Config, gitDir string) error {
380394
381395 // Skip branches whose PRs have already been merged.
382396 br := s .Branches [idx ]
383- if br .PullRequest != nil && br . PullRequest . Merged {
397+ if br .IsMerged () {
384398 state .OntoOldBase = state .OriginalRefs [branchName ]
385399 state .UseOnto = true
386400 cfg .Successf ("Skipping %s (PR #%d merged)" , branchName , br .PullRequest .Number )
@@ -399,7 +413,7 @@ func continueRebase(cfg *config.Config, gitDir string) error {
399413 newBase := s .Trunk .Branch
400414 for j := idx - 1 ; j >= 0 ; j -- {
401415 b := s .Branches [j ]
402- if b . PullRequest == nil || ! b .PullRequest . Merged {
416+ if ! b .IsMerged () {
403417 newBase = b .Branch
404418 break
405419 }
@@ -484,13 +498,13 @@ func continueRebase(cfg *config.Config, gitDir string) error {
484498
485499 for i := range s .Branches {
486500 // Skip merged branches when updating base SHAs.
487- if s .Branches [i ].PullRequest != nil && s . Branches [ i ]. PullRequest . Merged {
501+ if s .Branches [i ].IsMerged () {
488502 continue
489503 }
490504 // Find the first non-merged ancestor, or trunk.
491505 parent := s .Trunk .Branch
492506 for j := i - 1 ; j >= 0 ; j -- {
493- if s . Branches [ j ]. PullRequest == nil || ! s .Branches [j ].PullRequest . Merged {
507+ if ! s .Branches [j ].IsMerged () {
494508 parent = s .Branches [j ].Branch
495509 break
496510 }
0 commit comments