diff --git a/pkg/gui/controllers/helpers/working_tree_helper.go b/pkg/gui/controllers/helpers/working_tree_helper.go index b7ce2f431..ad3dfc4d0 100644 --- a/pkg/gui/controllers/helpers/working_tree_helper.go +++ b/pkg/gui/controllers/helpers/working_tree_helper.go @@ -274,15 +274,7 @@ func (self *WorkingTreeHelper) WithEnsureCommittableFiles(handler func() error) } if self.c.UserConfig().Gui.SkipNoStagedFilesWarning { - self.c.LogAction(self.c.Tr.Actions.StageAllFiles) - if err := self.c.Git().WorkingTree.StageAll(false); err != nil { - return err - } - self.c.Refresh(types.RefreshOptions{ - Scope: []types.RefreshableView{types.FILES}, - Then: handler, - }) - return nil + return self.stageAllAndThen(handler) } return self.promptToStageAllAndRetry(handler) @@ -291,18 +283,24 @@ func (self *WorkingTreeHelper) WithEnsureCommittableFiles(handler func() error) return handler() } +func (self *WorkingTreeHelper) stageAllAndThen(then func() error) error { + self.c.LogAction(self.c.Tr.Actions.StageAllFiles) + if err := self.c.Git().WorkingTree.StageAll(false); err != nil { + return err + } + self.c.Refresh(types.RefreshOptions{ + Scope: []types.RefreshableView{types.FILES}, + Then: then, + }) + return nil +} + func (self *WorkingTreeHelper) promptToStageAllAndRetry(retry func() error) error { self.c.Confirm(types.ConfirmOpts{ Title: self.c.Tr.NoFilesStagedTitle, Prompt: self.c.Tr.NoFilesStagedPrompt, HandleConfirm: func() error { - self.c.LogAction(self.c.Tr.Actions.StageAllFiles) - if err := self.c.Git().WorkingTree.StageAll(false); err != nil { - return err - } - self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}}) - - return retry() + return self.stageAllAndThen(retry) }, }) diff --git a/pkg/integration/tests/commit/commit_in_editor_with_nothing_staged.go b/pkg/integration/tests/commit/commit_in_editor_with_nothing_staged.go new file mode 100644 index 000000000..f48ea3468 --- /dev/null +++ b/pkg/integration/tests/commit/commit_in_editor_with_nothing_staged.go @@ -0,0 +1,44 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CommitInEditorWithNothingStaged = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Commit using the git editor with nothing staged, confirming to stage all files", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().Gui.ShowFileTree = false + }, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("first commit") + shell.CreateFile("file1", "file1 content") + shell.CreateFile("file2", "file2 content") + + shell.SetConfig("core.editor", "sh -c 'echo my commit message >.git/COMMIT_EDITMSG'") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Files(). + IsFocused(). + Lines( + Equals("?? file1").IsSelected(), + Equals("?? file2"), + ). + Press(keys.Files.CommitChangesWithEditor). + Tap(func() { + t.ExpectPopup().Confirmation(). + Title(Equals("No files staged")). + Content(Equals("You have not staged any files. Commit all files?")). + Confirm() + }). + IsEmpty() + + t.Views().Commits(). + Lines( + Contains("my commit message"), + Contains("first commit"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index ee1ca9a91..6d66a1080 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -145,6 +145,7 @@ var tests = []*components.IntegrationTest{ commit.Commit, commit.CommitAllWithNestedRepository, commit.CommitDuringSquashMerge, + commit.CommitInEditorWithNothingStaged, commit.CommitMultiline, commit.CommitSkipHooks, commit.CommitSwitchToEditor,