diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 41f9197fe75..064bdf4ede9 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -1477,6 +1477,9 @@ func (self *FilesController) hasPathStagedChanges(node *filetree.FileNode) bool } func (self *FilesController) stash() error { + if !self.c.Helpers().WorkingTree.IsWorkingTreeDirtyExceptSubmodules() { + return errors.New(self.c.Tr.NoFilesToStash) + } return self.handleStashSave(self.c.Git().Stash.Push, self.c.Tr.Actions.StashAllChanges) } diff --git a/pkg/integration/tests/stash/stash_no_tracked_changes.go b/pkg/integration/tests/stash/stash_no_tracked_changes.go new file mode 100644 index 00000000000..cf70e287f36 --- /dev/null +++ b/pkg/integration/tests/stash/stash_no_tracked_changes.go @@ -0,0 +1,33 @@ +package stash + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var StashNoTrackedChanges = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Pressing stash with only untracked files shows an error", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.CreateFile("untracked", "content") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Files(). + Lines( + Contains("untracked"), + ). + Press(keys.Files.StashAllChanges) + + t.ExpectPopup().Alert(). + Title(Equals("Error")). + Content(Contains("You have no files to stash")). + Confirm() + + t.Views().Files(). + Lines( + Contains("untracked"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 15ef6f8c75d..cd96e273c24 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -434,6 +434,7 @@ var tests = []*components.IntegrationTest{ stash.StashAll, stash.StashAndKeepIndex, stash.StashIncludingUntrackedFiles, + stash.StashNoTrackedChanges, stash.StashStaged, stash.StashStagedPartialFile, stash.StashUnstaged,