Skip to content

Commit c4ee0c8

Browse files
committed
Show confirmation menu when trying to amend changes while there are conflicts
1 parent 3722824 commit c4ee0c8

File tree

8 files changed

+269
-12
lines changed

8 files changed

+269
-12
lines changed

pkg/gui/controllers/files_controller.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -690,23 +690,68 @@ func (self *FilesController) refresh() error {
690690
}
691691

692692
func (self *FilesController) handleAmendCommitPress() error {
693-
self.c.Confirm(types.ConfirmOpts{
694-
Title: self.c.Tr.AmendLastCommitTitle,
695-
Prompt: self.c.Tr.SureToAmend,
696-
HandleConfirm: func() error {
697-
return self.c.Helpers().WorkingTree.WithEnsureCommittableFiles(func() error {
698-
if len(self.c.Model().Commits) == 0 {
699-
return errors.New(self.c.Tr.NoCommitToAmend)
700-
}
693+
doAmend := func() error {
694+
return self.c.Helpers().WorkingTree.WithEnsureCommittableFiles(func() error {
695+
if len(self.c.Model().Commits) == 0 {
696+
return errors.New(self.c.Tr.NoCommitToAmend)
697+
}
701698

702-
return self.c.Helpers().AmendHelper.AmendHead()
703-
})
704-
},
705-
})
699+
return self.c.Helpers().AmendHelper.AmendHead()
700+
})
701+
}
702+
703+
if self.isResolvingConflicts() {
704+
return self.c.Menu(types.CreateMenuOptions{
705+
Title: self.c.Tr.AmendCommitTitle,
706+
Prompt: self.c.Tr.AmendCommitWithConflictsMenuPrompt,
707+
HideCancel: true, // We want the cancel item first, so we add one manually
708+
Items: []*types.MenuItem{
709+
{
710+
Label: self.c.Tr.Cancel,
711+
OnPress: func() error {
712+
return nil
713+
},
714+
},
715+
{
716+
Label: self.c.Tr.AmendCommitWithConflictsContinue,
717+
OnPress: func() error {
718+
return self.c.Helpers().MergeAndRebase.ContinueRebase()
719+
},
720+
},
721+
{
722+
Label: self.c.Tr.AmendCommitWithConflictsAmend,
723+
OnPress: func() error {
724+
return doAmend()
725+
},
726+
},
727+
},
728+
})
729+
} else {
730+
self.c.Confirm(types.ConfirmOpts{
731+
Title: self.c.Tr.AmendLastCommitTitle,
732+
Prompt: self.c.Tr.SureToAmend,
733+
HandleConfirm: func() error {
734+
return doAmend()
735+
},
736+
})
737+
}
706738

707739
return nil
708740
}
709741

742+
func (self *FilesController) isResolvingConflicts() bool {
743+
commits := self.c.Model().Commits
744+
for _, c := range commits {
745+
if c.Status != models.StatusRebasing {
746+
break
747+
}
748+
if c.Action == models.ActionConflict {
749+
return true
750+
}
751+
}
752+
return false
753+
}
754+
710755
func (self *FilesController) handleStatusFilterPressed() error {
711756
return self.c.Menu(types.CreateMenuOptions{
712757
Title: self.c.Tr.FilteringMenuTitle,

pkg/gui/controllers/helpers/merge_and_rebase_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func (self *MergeAndRebaseHelper) CreateRebaseOptionsMenu() error {
7777
return self.c.Menu(types.CreateMenuOptions{Title: title, Items: menuItems})
7878
}
7979

80+
func (self *MergeAndRebaseHelper) ContinueRebase() error {
81+
return self.genericMergeCommand(REBASE_OPTION_CONTINUE)
82+
}
83+
8084
func (self *MergeAndRebaseHelper) genericMergeCommand(command string) error {
8185
status := self.c.Git().Status.WorkingTreeState()
8286

pkg/i18n/english.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ type TranslationSet struct {
353353
ScrollDownMainWindow string
354354
AmendCommitTitle string
355355
AmendCommitPrompt string
356+
AmendCommitWithConflictsMenuPrompt string
357+
AmendCommitWithConflictsContinue string
358+
AmendCommitWithConflictsAmend string
356359
DropCommitTitle string
357360
DropCommitPrompt string
358361
DropUpdateRefPrompt string
@@ -1368,6 +1371,9 @@ func EnglishTranslationSet() *TranslationSet {
13681371
ScrollDownMainWindow: "Scroll down main window",
13691372
AmendCommitTitle: "Amend commit",
13701373
AmendCommitPrompt: "Are you sure you want to amend this commit with your staged files?",
1374+
AmendCommitWithConflictsMenuPrompt: "WARNING: you are about to amend the last finished commit with your resolved conflicts. This is very unlikely to be what you want at this point. More likely, you simply want to continue the rebase instead.\n\nDo you still want to amend the previous commit?",
1375+
AmendCommitWithConflictsContinue: "No, continue rebase",
1376+
AmendCommitWithConflictsAmend: "Yes, amend previous commit",
13711377
DropCommitTitle: "Drop commit",
13721378
DropCommitPrompt: "Are you sure you want to drop the selected commit(s)?",
13731379
DropMergeCommitPrompt: "Are you sure you want to drop the selected merge commit? Note that it will also drop all the commits that were merged in by it.",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 AmendWhenThereAreConflictsAndAmend = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Amends the last commit from the files panel while a rebase is stopped due to conflicts, and amends the commit",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
setupForAmendTests(shell)
15+
},
16+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
17+
doTheRebaseForAmendTests(t, keys)
18+
19+
t.Views().Files().
20+
Press(keys.Commits.AmendToCommit)
21+
22+
t.ExpectPopup().Menu().
23+
Title(Equals("Amend commit")).
24+
Select(Equals("Yes, amend previous commit")).
25+
Confirm()
26+
27+
t.Views().Files().IsEmpty()
28+
29+
t.Views().Commits().
30+
Focus().
31+
Lines(
32+
Contains("pick").Contains("commit three"),
33+
Contains("conflict").Contains("<-- YOU ARE HERE --- file1 changed in branch"),
34+
Contains("commit two"),
35+
Contains("file1 changed in master"),
36+
Contains("base commit"),
37+
)
38+
39+
checkCommitContainsChange(t, "commit two", "+branch")
40+
},
41+
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 AmendWhenThereAreConflictsAndCancel = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Amends the last commit from the files panel while a rebase is stopped due to conflicts, and cancels the confirmation",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
setupForAmendTests(shell)
15+
},
16+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
17+
doTheRebaseForAmendTests(t, keys)
18+
19+
t.Views().Files().
20+
Press(keys.Commits.AmendToCommit)
21+
22+
t.ExpectPopup().Menu().
23+
Title(Equals("Amend commit")).
24+
Select(Equals("Cancel")).
25+
Confirm()
26+
27+
// Check that nothing happened:
28+
t.Views().Files().
29+
Lines(
30+
Contains("M file1"),
31+
)
32+
33+
t.Views().Commits().
34+
Focus().
35+
Lines(
36+
Contains("pick").Contains("commit three"),
37+
Contains("conflict").Contains("<-- YOU ARE HERE --- file1 changed in branch"),
38+
Contains("commit two"),
39+
Contains("file1 changed in master"),
40+
Contains("base commit"),
41+
)
42+
},
43+
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 AmendWhenThereAreConflictsAndContinue = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Amends the last commit from the files panel while a rebase is stopped due to conflicts, and continues the rebase",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
setupForAmendTests(shell)
15+
},
16+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
17+
doTheRebaseForAmendTests(t, keys)
18+
19+
t.Views().Files().
20+
Press(keys.Commits.AmendToCommit)
21+
22+
t.ExpectPopup().Menu().
23+
Title(Equals("Amend commit")).
24+
Select(Equals("No, continue rebase")).
25+
Confirm()
26+
27+
t.Views().Files().IsEmpty()
28+
29+
t.Views().Commits().
30+
Focus().
31+
Lines(
32+
Contains("commit three"),
33+
Contains("file1 changed in branch"),
34+
Contains("commit two"),
35+
Contains("file1 changed in master"),
36+
Contains("base commit"),
37+
)
38+
39+
checkCommitContainsChange(t, "file1 changed in branch", "+branch")
40+
},
41+
})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package commit
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
func setupForAmendTests(shell *Shell) {
9+
shell.EmptyCommit("base commit")
10+
shell.NewBranch("branch")
11+
shell.Checkout("master")
12+
shell.CreateFileAndAdd("file1", "master")
13+
shell.Commit("file1 changed in master")
14+
shell.Checkout("branch")
15+
shell.UpdateFileAndAdd("file2", "two")
16+
shell.Commit("commit two")
17+
shell.CreateFileAndAdd("file1", "branch")
18+
shell.Commit("file1 changed in branch")
19+
shell.UpdateFileAndAdd("file3", "three")
20+
shell.Commit("commit three")
21+
}
22+
23+
func doTheRebaseForAmendTests(t *TestDriver, keys config.KeybindingConfig) {
24+
t.Views().Commits().
25+
Focus().
26+
Lines(
27+
Contains("commit three").IsSelected(),
28+
Contains("file1 changed in branch"),
29+
Contains("commit two"),
30+
Contains("base commit"),
31+
)
32+
t.Views().Branches().
33+
Focus().
34+
NavigateToLine(Contains("master")).
35+
Press(keys.Branches.RebaseBranch).
36+
Tap(func() {
37+
t.ExpectPopup().Menu().
38+
Title(Equals("Rebase 'branch'")).
39+
Select(Contains("Simple rebase")).
40+
Confirm()
41+
t.Common().AcknowledgeConflicts()
42+
})
43+
44+
t.Views().Commits().
45+
Lines(
46+
Contains("pick").Contains("commit three"),
47+
Contains("conflict").Contains("<-- YOU ARE HERE --- file1 changed in branch"),
48+
Contains("commit two"),
49+
Contains("file1 changed in master"),
50+
Contains("base commit"),
51+
)
52+
53+
t.Views().Files().
54+
Focus().
55+
PressEnter()
56+
57+
t.Views().MergeConflicts().
58+
IsFocused().
59+
SelectNextItem(). // choose "incoming"
60+
PressPrimaryAction()
61+
62+
t.ExpectPopup().Confirmation().
63+
Title(Equals("Continue")).
64+
Content(Contains("All merge conflicts resolved. Continue?")).
65+
Cancel()
66+
}
67+
68+
func checkCommitContainsChange(t *TestDriver, commitSubject string, change string) {
69+
t.Views().Commits().
70+
Focus().
71+
NavigateToLine(Contains(commitSubject))
72+
t.Views().Main().
73+
Content(Contains(change))
74+
}

pkg/integration/tests/test_list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ var tests = []*components.IntegrationTest{
8383
commit.AddCoAuthorRange,
8484
commit.AddCoAuthorWhileCommitting,
8585
commit.Amend,
86+
commit.AmendWhenThereAreConflictsAndAmend,
87+
commit.AmendWhenThereAreConflictsAndCancel,
88+
commit.AmendWhenThereAreConflictsAndContinue,
8689
commit.AutoWrapMessage,
8790
commit.Checkout,
8891
commit.Commit,

0 commit comments

Comments
 (0)