99 "github.com/jesseduffield/lazygit/pkg/commands/models"
1010 "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
1111 "github.com/jesseduffield/lazygit/pkg/gui/context"
12+ "github.com/jesseduffield/lazygit/pkg/gui/context/traits"
1213 "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
1314 "github.com/jesseduffield/lazygit/pkg/gui/keybindings"
1415 "github.com/jesseduffield/lazygit/pkg/gui/style"
@@ -532,10 +533,7 @@ func (self *LocalCommitsController) startInteractiveRebaseWithEdit(
532533) error {
533534 return self .c .WithWaitingStatus (self .c .Tr .RebasingStatus , func (gocui.Task ) error {
534535 self .c .LogAction (self .c .Tr .Actions .EditCommit )
535- selectedIdx , rangeStartIdx , rangeSelectMode := self .context ().GetSelectionRangeAndMode ()
536- commits := self .c .Model ().Commits
537- selectedHash := commits [selectedIdx ].Hash
538- rangeStartHash := commits [rangeStartIdx ].Hash
536+ selectionRangeAndMode := self .getSelectionRangeAndMode ()
539537 err := self .c .Git ().Rebase .EditRebase (commitsToEdit [len (commitsToEdit )- 1 ].Hash )
540538 return self .c .Helpers ().MergeAndRebase .CheckMergeOrRebaseWithRefreshOptions (
541539 err ,
@@ -554,23 +552,41 @@ func (self *LocalCommitsController) startInteractiveRebaseWithEdit(
554552 }
555553 }
556554
557- // We need to select the same commit range again because after starting a rebase,
558- // new lines can be added for update-ref commands in the TODO file, due to
559- // stacked branches. So the selected commits may be in different positions in the list.
560- _ , newSelectedIdx , ok1 := lo .FindIndexOf (self .c .Model ().Commits , func (c * models.Commit ) bool {
561- return c .Hash == selectedHash
562- })
563- _ , newRangeStartIdx , ok2 := lo .FindIndexOf (self .c .Model ().Commits , func (c * models.Commit ) bool {
564- return c .Hash == rangeStartHash
565- })
566- if ok1 && ok2 {
567- self .context ().SetSelectionRangeAndMode (newSelectedIdx , newRangeStartIdx , rangeSelectMode )
568- }
555+ self .restoreSelectionRangeAndMode (selectionRangeAndMode )
569556 return nil
570557 }})
571558 })
572559}
573560
561+ type SelectionRangeAndMode struct {
562+ selectedHash string
563+ rangeStartHash string
564+ mode traits.RangeSelectMode
565+ }
566+
567+ func (self * LocalCommitsController ) getSelectionRangeAndMode () SelectionRangeAndMode {
568+ selectedIdx , rangeStartIdx , rangeSelectMode := self .context ().GetSelectionRangeAndMode ()
569+ commits := self .c .Model ().Commits
570+ selectedHash := commits [selectedIdx ].Hash
571+ rangeStartHash := commits [rangeStartIdx ].Hash
572+ return SelectionRangeAndMode {selectedHash , rangeStartHash , rangeSelectMode }
573+ }
574+
575+ func (self * LocalCommitsController ) restoreSelectionRangeAndMode (selectionRangeAndMode SelectionRangeAndMode ) {
576+ // We need to select the same commit range again because after starting a rebase,
577+ // new lines can be added for update-ref commands in the TODO file, due to
578+ // stacked branches. So the selected commits may be in different positions in the list.
579+ _ , newSelectedIdx , ok1 := lo .FindIndexOf (self .c .Model ().Commits , func (c * models.Commit ) bool {
580+ return c .Hash == selectionRangeAndMode .selectedHash
581+ })
582+ _ , newRangeStartIdx , ok2 := lo .FindIndexOf (self .c .Model ().Commits , func (c * models.Commit ) bool {
583+ return c .Hash == selectionRangeAndMode .rangeStartHash
584+ })
585+ if ok1 && ok2 {
586+ self .context ().SetSelectionRangeAndMode (newSelectedIdx , newRangeStartIdx , selectionRangeAndMode .mode )
587+ }
588+ }
589+
574590func (self * LocalCommitsController ) findCommitForQuickStartInteractiveRebase () (* models.Commit , error ) {
575591 commit , index , ok := lo .FindIndexOf (self .c .Model ().Commits , func (c * models.Commit ) bool {
576592 return c .IsMerge () || c .Status == models .StatusMerged
0 commit comments