Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stashing all staged #3568

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions files 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some text
1 change: 1 addition & 0 deletions files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some text
6 changes: 5 additions & 1 deletion pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,11 @@ func (self *FilesController) createStashMenu() error {
if !self.c.Helpers().WorkingTree.AnyStagedFiles() {
return errors.New(self.c.Tr.NoTrackedStagedFilesStash)
}
return self.handleStashSave(self.c.Git().Stash.SaveStagedChanges, self.c.Tr.Actions.StashStagedChanges)
if self.c.Helpers().WorkingTree.AnyUnstagedFiles() {
return self.handleStashSave(self.c.Git().Stash.SaveStagedChanges, self.c.Tr.Actions.StashStagedChanges)
}
// ordinary stash
return self.handleStashSave(self.c.Git().Stash.Push, self.c.Tr.Actions.StashUnstagedChanges)
},
Key: 's',
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/gui/controllers/helpers/working_tree_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (self *WorkingTreeHelper) AnyStagedFiles() bool {
return false
}

func (self *WorkingTreeHelper) AnyUnstagedFiles() bool {
for _, file := range self.c.Model().Files {
if file.HasUnstagedChanges {
return true
}
}
return false
}

func (self *WorkingTreeHelper) AnyTrackedFiles() bool {
for _, file := range self.c.Model().Files {
if file.Tracked {
Expand Down
50 changes: 50 additions & 0 deletions pkg/integration/tests/stash/stash_staged_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package stash

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

var StashStagedAll = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Stash staged (all files and content) changes",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.CreateFileAndAdd("file-staged", "content")
shell.EmptyCommit("initial commit")
shell.UpdateFileAndAdd("file-staged", "more content")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Stash().
IsEmpty()

t.Views().Files().
Lines(
Contains("file-staged"),
).
Press(keys.Files.ViewStashOptions)

t.ExpectPopup().Menu().Title(Equals("Stash options")).Select(MatchesRegexp("Stash staged changes$")).Confirm()

// in the previous implementation if you give a name to the stash entry it whould work
// that's why here I'm specifically not giving one
t.ExpectPopup().Prompt().Title(Equals("Stash changes")).Type("").Confirm()

t.Views().Stash().
LineCount(EqualsInt(1)). // I check that there's only one line here
Lines(MatchesRegexp("WIP")) // I didn't specify the file so cannot check the content.

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

t.Views().Stash().
Focus().
PressEnter()

t.Views().CommitFiles().
IsFocused().
Lines(
Contains("file-staged").IsSelected(),
)
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var StashStaged = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Stash staged changes",
var StashStagedPartial = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Stash some (but not all) staged changes",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
Expand Down
3 changes: 2 additions & 1 deletion pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ var tests = []*components.IntegrationTest{
stash.StashAll,
stash.StashAndKeepIndex,
stash.StashIncludingUntrackedFiles,
stash.StashStaged,
stash.StashStagedAll,
stash.StashStagedPartial,
stash.StashUnstaged,
status.ClickRepoNameToOpenReposMenu,
status.ClickToFocus,
Expand Down