Skip to content

Commit 439acb1

Browse files
committed
Disregard master commits when finding base commit for fixup
If exactly one candidate from inside the current branch is found, we return that one even if there are also hunks belonging to master commits; we disregard those in this case.
1 parent 1463a1d commit 439acb1

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

pkg/gui/controllers/helpers/fixup_helper.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

pkg/integration/tests/commit/find_base_commit_for_fixup_disregard_main_branch.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var FindBaseCommitForFixupDisregardMainBranch = NewIntegrationTest(NewIntegratio
3535
Focus().
3636
Press(keys.Files.FindBaseCommitForFixup)
3737

38-
/* EXPECTED:
3938
t.Views().Commits().
4039
IsFocused().
4140
Lines(
@@ -44,13 +43,5 @@ var FindBaseCommitForFixupDisregardMainBranch = NewIntegrationTest(NewIntegratio
4443
Contains("2nd commit"),
4544
Contains("1st commit"),
4645
)
47-
*/
48-
49-
// ACTUAL:
50-
t.ExpectPopup().Alert().Title(Equals("Error")).Content(
51-
Contains("Multiple base commits found.").
52-
Contains("2nd commit").
53-
Contains("3rd commit"),
54-
).Confirm()
5546
},
5647
})

0 commit comments

Comments
 (0)