Skip to content

Commit

Permalink
Show confirmation menu when trying to amend changes while there are c…
Browse files Browse the repository at this point in the history
…onflicts
  • Loading branch information
stefanhaller committed Jan 28, 2025
1 parent 3722824 commit c4ee0c8
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 12 deletions.
69 changes: 57 additions & 12 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,23 +690,68 @@ func (self *FilesController) refresh() error {
}

func (self *FilesController) handleAmendCommitPress() error {
self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.AmendLastCommitTitle,
Prompt: self.c.Tr.SureToAmend,
HandleConfirm: func() error {
return self.c.Helpers().WorkingTree.WithEnsureCommittableFiles(func() error {
if len(self.c.Model().Commits) == 0 {
return errors.New(self.c.Tr.NoCommitToAmend)
}
doAmend := func() error {
return self.c.Helpers().WorkingTree.WithEnsureCommittableFiles(func() error {
if len(self.c.Model().Commits) == 0 {
return errors.New(self.c.Tr.NoCommitToAmend)
}

return self.c.Helpers().AmendHelper.AmendHead()
})
},
})
return self.c.Helpers().AmendHelper.AmendHead()
})
}

if self.isResolvingConflicts() {
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.AmendCommitTitle,
Prompt: self.c.Tr.AmendCommitWithConflictsMenuPrompt,
HideCancel: true, // We want the cancel item first, so we add one manually
Items: []*types.MenuItem{
{
Label: self.c.Tr.Cancel,
OnPress: func() error {
return nil
},
},
{
Label: self.c.Tr.AmendCommitWithConflictsContinue,
OnPress: func() error {
return self.c.Helpers().MergeAndRebase.ContinueRebase()
},
},
{
Label: self.c.Tr.AmendCommitWithConflictsAmend,
OnPress: func() error {
return doAmend()
},
},
},
})
} else {
self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.AmendLastCommitTitle,
Prompt: self.c.Tr.SureToAmend,
HandleConfirm: func() error {
return doAmend()
},
})
}

return nil
}

func (self *FilesController) isResolvingConflicts() bool {
commits := self.c.Model().Commits
for _, c := range commits {
if c.Status != models.StatusRebasing {
break
}
if c.Action == models.ActionConflict {
return true
}
}
return false
}

func (self *FilesController) handleStatusFilterPressed() error {
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.FilteringMenuTitle,
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/controllers/helpers/merge_and_rebase_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (self *MergeAndRebaseHelper) CreateRebaseOptionsMenu() error {
return self.c.Menu(types.CreateMenuOptions{Title: title, Items: menuItems})
}

func (self *MergeAndRebaseHelper) ContinueRebase() error {
return self.genericMergeCommand(REBASE_OPTION_CONTINUE)
}

func (self *MergeAndRebaseHelper) genericMergeCommand(command string) error {
status := self.c.Git().Status.WorkingTreeState()

Expand Down
6 changes: 6 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ type TranslationSet struct {
ScrollDownMainWindow string
AmendCommitTitle string
AmendCommitPrompt string
AmendCommitWithConflictsMenuPrompt string
AmendCommitWithConflictsContinue string
AmendCommitWithConflictsAmend string
DropCommitTitle string
DropCommitPrompt string
DropUpdateRefPrompt string
Expand Down Expand Up @@ -1368,6 +1371,9 @@ func EnglishTranslationSet() *TranslationSet {
ScrollDownMainWindow: "Scroll down main window",
AmendCommitTitle: "Amend commit",
AmendCommitPrompt: "Are you sure you want to amend this commit with your staged files?",
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?",
AmendCommitWithConflictsContinue: "No, continue rebase",
AmendCommitWithConflictsAmend: "Yes, amend previous commit",
DropCommitTitle: "Drop commit",
DropCommitPrompt: "Are you sure you want to drop the selected commit(s)?",
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.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var AmendWhenThereAreConflictsAndAmend = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Amends the last commit from the files panel while a rebase is stopped due to conflicts, and amends the commit",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
setupForAmendTests(shell)
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
doTheRebaseForAmendTests(t, keys)

t.Views().Files().
Press(keys.Commits.AmendToCommit)

t.ExpectPopup().Menu().
Title(Equals("Amend commit")).
Select(Equals("Yes, amend previous commit")).
Confirm()

t.Views().Files().IsEmpty()

t.Views().Commits().
Focus().
Lines(
Contains("pick").Contains("commit three"),
Contains("conflict").Contains("<-- YOU ARE HERE --- file1 changed in branch"),
Contains("commit two"),
Contains("file1 changed in master"),
Contains("base commit"),
)

checkCommitContainsChange(t, "commit two", "+branch")
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var AmendWhenThereAreConflictsAndCancel = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Amends the last commit from the files panel while a rebase is stopped due to conflicts, and cancels the confirmation",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
setupForAmendTests(shell)
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
doTheRebaseForAmendTests(t, keys)

t.Views().Files().
Press(keys.Commits.AmendToCommit)

t.ExpectPopup().Menu().
Title(Equals("Amend commit")).
Select(Equals("Cancel")).
Confirm()

// Check that nothing happened:
t.Views().Files().
Lines(
Contains("M file1"),
)

t.Views().Commits().
Focus().
Lines(
Contains("pick").Contains("commit three"),
Contains("conflict").Contains("<-- YOU ARE HERE --- file1 changed in branch"),
Contains("commit two"),
Contains("file1 changed in master"),
Contains("base commit"),
)
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var AmendWhenThereAreConflictsAndContinue = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Amends the last commit from the files panel while a rebase is stopped due to conflicts, and continues the rebase",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
setupForAmendTests(shell)
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
doTheRebaseForAmendTests(t, keys)

t.Views().Files().
Press(keys.Commits.AmendToCommit)

t.ExpectPopup().Menu().
Title(Equals("Amend commit")).
Select(Equals("No, continue rebase")).
Confirm()

t.Views().Files().IsEmpty()

t.Views().Commits().
Focus().
Lines(
Contains("commit three"),
Contains("file1 changed in branch"),
Contains("commit two"),
Contains("file1 changed in master"),
Contains("base commit"),
)

checkCommitContainsChange(t, "file1 changed in branch", "+branch")
},
})
74 changes: 74 additions & 0 deletions pkg/integration/tests/commit/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

func setupForAmendTests(shell *Shell) {
shell.EmptyCommit("base commit")
shell.NewBranch("branch")
shell.Checkout("master")
shell.CreateFileAndAdd("file1", "master")
shell.Commit("file1 changed in master")
shell.Checkout("branch")
shell.UpdateFileAndAdd("file2", "two")
shell.Commit("commit two")
shell.CreateFileAndAdd("file1", "branch")
shell.Commit("file1 changed in branch")
shell.UpdateFileAndAdd("file3", "three")
shell.Commit("commit three")
}

func doTheRebaseForAmendTests(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("commit three").IsSelected(),
Contains("file1 changed in branch"),
Contains("commit two"),
Contains("base commit"),
)
t.Views().Branches().
Focus().
NavigateToLine(Contains("master")).
Press(keys.Branches.RebaseBranch).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Rebase 'branch'")).
Select(Contains("Simple rebase")).
Confirm()
t.Common().AcknowledgeConflicts()
})

t.Views().Commits().
Lines(
Contains("pick").Contains("commit three"),
Contains("conflict").Contains("<-- YOU ARE HERE --- file1 changed in branch"),
Contains("commit two"),
Contains("file1 changed in master"),
Contains("base commit"),
)

t.Views().Files().
Focus().
PressEnter()

t.Views().MergeConflicts().
IsFocused().
SelectNextItem(). // choose "incoming"
PressPrimaryAction()

t.ExpectPopup().Confirmation().
Title(Equals("Continue")).
Content(Contains("All merge conflicts resolved. Continue?")).
Cancel()
}

func checkCommitContainsChange(t *TestDriver, commitSubject string, change string) {
t.Views().Commits().
Focus().
NavigateToLine(Contains(commitSubject))
t.Views().Main().
Content(Contains(change))
}
3 changes: 3 additions & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ var tests = []*components.IntegrationTest{
commit.AddCoAuthorRange,
commit.AddCoAuthorWhileCommitting,
commit.Amend,
commit.AmendWhenThereAreConflictsAndAmend,
commit.AmendWhenThereAreConflictsAndCancel,
commit.AmendWhenThereAreConflictsAndContinue,
commit.AutoWrapMessage,
commit.Checkout,
commit.Commit,
Expand Down

0 comments on commit c4ee0c8

Please sign in to comment.