@@ -72,8 +72,49 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
7272 // This should never happen
7373 return errors .New (self .c .Tr .NoBaseCommitsFound )
7474 }
75- if len (hashes ) > 1 {
76- subjects , err := self .c .Git ().Commit .GetHashesAndCommitMessagesFirstLine (hashes )
75+
76+ // If a commit can't be found, and the last known commit is already merged,
77+ // we know that the commit we're looking for is also merged. Otherwise we
78+ // can't tell.
79+ notFoundMeansMerged := len (commits ) > 0 && commits [len (commits )- 1 ].Status == models .StatusMerged
80+
81+ const (
82+ MERGED int = iota
83+ NOT_MERGED
84+ CANNOT_TELL
85+ )
86+
87+ // Group the hashes into buckets by merged status
88+ hashGroups := lo .GroupBy (hashes , func (hash string ) int {
89+ commit , _ , ok := self .findCommit (commits , hash )
90+ if ok {
91+ return lo .Ternary (commit .Status == models .StatusMerged , MERGED , NOT_MERGED )
92+ }
93+ return lo .Ternary (notFoundMeansMerged , MERGED , CANNOT_TELL )
94+ })
95+
96+ if len (hashGroups [CANNOT_TELL ]) > 0 {
97+ // If we have any commits that we can't tell if they're merged, just
98+ // show the generic "not in current view" error. This can only happen if
99+ // a feature branch has more than 300 commits, or there is no main
100+ // branch. Both are so unlikely that we don't bother returning a more
101+ // detailed error message (e.g. we could say something about the commits
102+ // that *are* in the current branch, but it's not worth it).
103+ return errors .New (self .c .Tr .BaseCommitIsNotInCurrentView )
104+ }
105+
106+ if len (hashGroups [NOT_MERGED ]) == 0 {
107+ // If all the commits are merged, show the "already on main branch"
108+ // error. It isn't worth doing a detailed report of which commits we
109+ // found.
110+ return errors .New (self .c .Tr .BaseCommitIsAlreadyOnMainBranch )
111+ }
112+
113+ if len (hashGroups [NOT_MERGED ]) > 1 {
114+ // If there are multiple commits that could be the base commit, list
115+ // them in the error message. But only the candidates from the current
116+ // branch, not including any that are already merged.
117+ subjects , err := self .c .Git ().Commit .GetHashesAndCommitMessagesFirstLine (hashGroups [NOT_MERGED ])
77118 if err != nil {
78119 return err
79120 }
@@ -83,20 +124,9 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
83124 return fmt .Errorf ("%s\n \n %s" , message , subjects )
84125 }
85126
86- commit , index , ok := self .findCommit (commits , hashes [0 ])
87- if ! ok {
88- if commits [len (commits )- 1 ].Status == models .StatusMerged {
89- // If the commit is not found, it's most likely because it's already
90- // merged, and more than 300 commits away. Check if the last known
91- // commit is already merged; if so, show the "already merged" error.
92- return errors .New (self .c .Tr .BaseCommitIsAlreadyOnMainBranch )
93- }
94- // If we get here, the current branch must have more then 300 commits. Unlikely...
95- return errors .New (self .c .Tr .BaseCommitIsNotInCurrentView )
96- }
97- if commit .Status == models .StatusMerged {
98- return errors .New (self .c .Tr .BaseCommitIsAlreadyOnMainBranch )
99- }
127+ // At this point we know that the NOT_MERGED bucket has exactly one commit,
128+ // and that's the one we want to select.
129+ _ , index , _ := self .findCommit (commits , hashGroups [NOT_MERGED ][0 ])
100130
101131 doIt := func () error {
102132 if ! hasStagedChanges {
0 commit comments