Skip to content

Commit dff66dc

Browse files
committed
Allow the fixup helper to return commits on the main branch with a warning.
1 parent d3bcdb4 commit dff66dc

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

pkg/gui/controllers/helpers/fixup_helper.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,33 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
104104
}
105105

106106
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-
}
107+
// The commit is already merged, but we attempt to show it anyway.
108+
if len(hashGroups[MERGED]) > 1 {
109+
// We found multiple merged commits. List them in error message
110+
subjects := self.getHashesAndSubjects(commits, hashGroups[NOT_MERGED])
111+
message := self.c.Tr.MultipleBaseCommitsFoundMerged
112+
return fmt.Errorf("%s\n\n%s", message, subjects)
113+
} else if len(hashGroups[MERGED]) == 0 {
114+
// This can not happen, as the commit has to be either in
115+
// CANNOT_TELL, NOT_MERGED or MERGED state and we already established,
116+
// that it is not in CANNOT_TELL or NOT_MERGED
117+
return errors.New(self.c.Tr.BaseCommitIsNotInCurrentView)
118+
}
112119

120+
// At this point we know that the MERGED bucket has exactly one commit,
121+
// and that's the one we want to select.
122+
_, index, found := self.findCommit(commits, hashGroups[MERGED][0])
123+
if !found {
124+
// However the commit is not in view, so we cannot select it.
125+
return errors.New(self.c.Tr.BaseCommitIsNotInCurrentView)
126+
}
127+
// We found the commit, so we show it with a warning message, that it is already merged.
128+
return self.c.ConfirmIf(true, types.ConfirmOpts{
129+
Title: self.c.Tr.FindBaseCommitForFixup,
130+
Prompt: self.c.Tr.BaseCommitIsAlreadyOnMainBranch,
131+
HandleConfirm: self.getHandlerToStageAndSelectIndex(hasStagedChanges, index),
132+
})
133+
}
113134
if len(hashGroups[NOT_MERGED]) > 1 {
114135
// If there are multiple commits that could be the base commit, list
115136
// them in the error message. But only the candidates from the current

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type TranslationSet struct {
4545
NoBaseCommitsFound string
4646
MultipleBaseCommitsFoundStaged string
4747
MultipleBaseCommitsFoundUnstaged string
48+
MultipleBaseCommitsFoundMerged string
4849
BaseCommitIsAlreadyOnMainBranch string
4950
BaseCommitIsNotInCurrentView string
5051
HunksWithOnlyAddedLinesWarning string
@@ -1140,6 +1141,7 @@ func EnglishTranslationSet() *TranslationSet {
11401141
NoBaseCommitsFound: "No base commits found",
11411142
MultipleBaseCommitsFoundStaged: "Multiple base commits found. (Try staging fewer changes at once)",
11421143
MultipleBaseCommitsFoundUnstaged: "Multiple base commits found. (Try staging some of the changes)",
1144+
MultipleBaseCommitsFoundMerged: "Multiple base commits found, that are already on the main branch. (Try staging some of the changes)",
11431145
BaseCommitIsAlreadyOnMainBranch: "The base commit for this change is already on the main branch",
11441146
BaseCommitIsNotInCurrentView: "Base commit is not in current view",
11451147
HunksWithOnlyAddedLinesWarning: "There are ranges of only added lines in the diff; be careful to check that these belong in the found base commit.\n\nProceed?",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package commit
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var FindBaseCommitForFixupInMainBranch = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Finds the base commit to create a fixup for when the commit is already merged into master",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.
15+
EmptyCommit("1st commit").
16+
CreateFileAndAdd("file1", "file1 content\n").
17+
Commit("2nd commit").
18+
NewBranch("mybranch").
19+
CreateFileAndAdd("file2", "file2 content\n").
20+
Commit("3rd commit").
21+
EmptyCommit("4th commit").
22+
UpdateFile("file1", "file1 changed content")
23+
},
24+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
25+
t.Views().Commits().
26+
Lines(
27+
Contains("4th commit").IsSelected(),
28+
Contains("3rd commit"),
29+
Contains("2nd commit"),
30+
Contains("1st commit"),
31+
)
32+
33+
t.Views().Files().
34+
Focus().
35+
Press(keys.Files.FindBaseCommitForFixup)
36+
t.ExpectPopup().
37+
Confirmation().
38+
Title(Equals("Find base commit for fixup")).
39+
Content(Contains("already on the main branch")).
40+
Confirm()
41+
42+
t.Views().Commits().
43+
IsFocused().
44+
Lines(
45+
Contains("4th commit"),
46+
Contains("3rd commit"),
47+
Contains("2nd commit").IsSelected(),
48+
Contains("1st commit"),
49+
)
50+
},
51+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var tests = []*components.IntegrationTest{
131131
commit.FailHooksThenCommitNoHooks,
132132
commit.FindBaseCommitForFixup,
133133
commit.FindBaseCommitForFixupDisregardMainBranch,
134+
commit.FindBaseCommitForFixupInMainBranch,
134135
commit.FindBaseCommitForFixupOnlyAddedLines,
135136
commit.FindBaseCommitForFixupWarningForAddedLines,
136137
commit.Highlight,

0 commit comments

Comments
 (0)