Skip to content

Commit b33ec5a

Browse files
Merge pull request #1980 from ajhynes7/stash-untracked-changes
2 parents fbfec75 + 684d1e9 commit b33ec5a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+182
-22
lines changed

pkg/commands/git_commands/stash.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ func (self *StashCommands) SaveStagedChanges(message string) error {
123123
return nil
124124
}
125125

126+
func (self *StashCommands) StashIncludeUntrackedChanges(message string) error {
127+
return self.cmd.New(fmt.Sprintf("git stash save %s --include-untracked", self.cmd.Quote(message))).Run()
128+
}
129+
126130
func (self *StashCommands) Rename(index int, message string) error {
127131
sha, err := self.Sha(index)
128132
if err != nil {

pkg/gui/controllers/files_controller.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -758,37 +758,53 @@ func (self *FilesController) createStashMenu() error {
758758
{
759759
Label: self.c.Tr.LcStashAllChanges,
760760
OnPress: func() error {
761-
return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashAllChanges, self.c.Tr.NoFilesToStash)
761+
if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
762+
return self.c.ErrorMsg(self.c.Tr.NoFilesToStash)
763+
}
764+
return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashAllChanges)
762765
},
763766
Key: 'a',
764767
},
765768
{
766769
Label: self.c.Tr.LcStashAllChangesKeepIndex,
767770
OnPress: func() error {
771+
if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
772+
return self.c.ErrorMsg(self.c.Tr.NoFilesToStash)
773+
}
768774
// if there are no staged files it behaves the same as Stash.Save
769-
return self.handleStashSave(self.git.Stash.StashAndKeepIndex, self.c.Tr.Actions.StashAllChangesKeepIndex, self.c.Tr.NoFilesToStash)
775+
return self.handleStashSave(self.git.Stash.StashAndKeepIndex, self.c.Tr.Actions.StashAllChangesKeepIndex)
770776
},
771777
Key: 'i',
772778
},
779+
{
780+
Label: self.c.Tr.LcStashIncludeUntrackedChanges,
781+
OnPress: func() error {
782+
return self.handleStashSave(self.git.Stash.StashIncludeUntrackedChanges, self.c.Tr.Actions.StashIncludeUntrackedChanges)
783+
},
784+
Key: 'U',
785+
},
773786
{
774787
Label: self.c.Tr.LcStashStagedChanges,
775788
OnPress: func() error {
776789
// there must be something in staging otherwise the current implementation mucks the stash up
777790
if !self.helpers.WorkingTree.AnyStagedFiles() {
778791
return self.c.ErrorMsg(self.c.Tr.NoTrackedStagedFilesStash)
779792
}
780-
return self.handleStashSave(self.git.Stash.SaveStagedChanges, self.c.Tr.Actions.StashStagedChanges, self.c.Tr.NoTrackedStagedFilesStash)
793+
return self.handleStashSave(self.git.Stash.SaveStagedChanges, self.c.Tr.Actions.StashStagedChanges)
781794
},
782795
Key: 's',
783796
},
784797
{
785798
Label: self.c.Tr.LcStashUnstagedChanges,
786799
OnPress: func() error {
800+
if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
801+
return self.c.ErrorMsg(self.c.Tr.NoFilesToStash)
802+
}
787803
if self.helpers.WorkingTree.AnyStagedFiles() {
788-
return self.handleStashSave(self.git.Stash.StashUnstagedChanges, self.c.Tr.Actions.StashUnstagedChanges, self.c.Tr.NoFilesToStash)
804+
return self.handleStashSave(self.git.Stash.StashUnstagedChanges, self.c.Tr.Actions.StashUnstagedChanges)
789805
}
790806
// ordinary stash
791-
return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashUnstagedChanges, self.c.Tr.NoFilesToStash)
807+
return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashUnstagedChanges)
792808
},
793809
Key: 'u',
794810
},
@@ -797,7 +813,7 @@ func (self *FilesController) createStashMenu() error {
797813
}
798814

799815
func (self *FilesController) stash() error {
800-
return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashAllChanges, self.c.Tr.NoTrackedStagedFilesStash)
816+
return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashAllChanges)
801817
}
802818

803819
func (self *FilesController) createResetToUpstreamMenu() error {
@@ -825,11 +841,7 @@ func (self *FilesController) toggleTreeView() error {
825841
return self.c.PostRefreshUpdate(self.context())
826842
}
827843

828-
func (self *FilesController) handleStashSave(stashFunc func(message string) error, action string, errorMsg string) error {
829-
if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
830-
return self.c.ErrorMsg(errorMsg)
831-
}
832-
844+
func (self *FilesController) handleStashSave(stashFunc func(message string) error, action string) error {
833845
return self.c.Prompt(types.PromptOpts{
834846
Title: self.c.Tr.StashChanges,
835847
HandleConfirm: func(stashComment string) error {

pkg/i18n/english.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ type TranslationSet struct {
295295
LcStashStagedChanges string
296296
LcStashAllChangesKeepIndex string
297297
LcStashUnstagedChanges string
298+
LcStashIncludeUntrackedChanges string
298299
LcStashOptions string
299300
NotARepository string
300301
LcJump string
@@ -588,6 +589,7 @@ type Actions struct {
588589
StashAllChangesKeepIndex string
589590
StashStagedChanges string
590591
StashUnstagedChanges string
592+
StashIncludeUntrackedChanges string
591593
GitFlowFinish string
592594
GitFlowStart string
593595
CopyToClipboard string
@@ -940,6 +942,7 @@ func EnglishTranslationSet() TranslationSet {
940942
LcStashStagedChanges: "stash staged changes",
941943
LcStashAllChangesKeepIndex: "stash all changes and keep index",
942944
LcStashUnstagedChanges: "stash unstaged changes",
945+
LcStashIncludeUntrackedChanges: "stash all changes including untracked files",
943946
LcStashOptions: "Stash options",
944947
NotARepository: "Error: must be run inside a git repository",
945948
LcJump: "jump to panel",
@@ -1216,6 +1219,7 @@ func EnglishTranslationSet() TranslationSet {
12161219
StashAllChangesKeepIndex: "Stash all changes and keep index",
12171220
StashStagedChanges: "Stash staged changes",
12181221
StashUnstagedChanges: "Stash unstaged changes",
1222+
StashIncludeUntrackedChanges: "Stash all changes including untracked files",
12191223
GitFlowFinish: "Git flow finish",
12201224
GitFlowStart: "Git Flow start",
12211225
CopyToClipboard: "Copy to clipboard",

pkg/integration/components/assert.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ func (self *Assert) CommitCount(expectedCount int) {
8484
})
8585
}
8686

87+
func (self *Assert) StashCount(expectedCount int) {
88+
self.assertWithRetries(func() (bool, string) {
89+
actualCount := len(self.gui.Model().StashEntries)
90+
91+
return actualCount == expectedCount, fmt.Sprintf(
92+
"Expected %d stash entries, but got %d",
93+
expectedCount, actualCount,
94+
)
95+
})
96+
}
97+
8798
func (self *Assert) AtLeastOneCommit() {
8899
self.assertWithRetries(func() (bool, string) {
89100
actualCount := len(self.gui.Model().Commits)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package stash
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var Stash = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Stashing files",
10+
ExtraCmdArgs: "",
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.EmptyCommit("initial commit")
15+
shell.CreateFile("file", "content")
16+
shell.GitAddAll()
17+
},
18+
Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) {
19+
assert.StashCount(0)
20+
assert.WorkingTreeFileCount(1)
21+
22+
input.PressKeys(keys.Files.ViewStashOptions)
23+
assert.InMenu()
24+
25+
input.PressKeys("a")
26+
assert.InPrompt()
27+
assert.MatchCurrentViewTitle(Equals("Stash changes"))
28+
29+
input.Type("my stashed file")
30+
input.Confirm()
31+
assert.StashCount(1)
32+
assert.WorkingTreeFileCount(0)
33+
},
34+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package stash
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var StashIncludingUntrackedFiles = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Stashing all files including untracked ones",
10+
ExtraCmdArgs: "",
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.EmptyCommit("initial commit")
15+
shell.CreateFile("file_1", "content")
16+
shell.CreateFile("file_2", "content")
17+
shell.GitAdd("file_1")
18+
},
19+
Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) {
20+
assert.StashCount(0)
21+
assert.WorkingTreeFileCount(2)
22+
23+
input.PressKeys(keys.Files.ViewStashOptions)
24+
assert.InMenu()
25+
26+
input.PressKeys("U")
27+
assert.InPrompt()
28+
assert.MatchCurrentViewTitle(Equals("Stash changes"))
29+
30+
input.Type("my stashed file")
31+
input.Confirm()
32+
assert.StashCount(1)
33+
assert.WorkingTreeFileCount(0)
34+
},
35+
})

pkg/integration/tests/tests.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@ import (
2424
// be sure to add it to this list.
2525

2626
var tests = []*components.IntegrationTest{
27-
commit.Commit,
28-
commit.NewBranch,
29-
branch.Suggestions,
27+
bisect.Basic,
28+
bisect.FromOtherBranch,
3029
branch.Delete,
3130
branch.Rebase,
3231
branch.RebaseAndDrop,
33-
interactive_rebase.One,
34-
interactive_rebase.AmendMerge,
35-
custom_commands.Basic,
36-
custom_commands.MultiplePrompts,
37-
custom_commands.MenuFromCommand,
38-
bisect.Basic,
39-
bisect.FromOtherBranch,
32+
branch.Suggestions,
4033
cherry_pick.CherryPick,
4134
cherry_pick.CherryPickConflicts,
35+
commit.Commit,
36+
commit.NewBranch,
37+
custom_commands.Basic,
4238
custom_commands.FormPrompts,
43-
stash.Rename,
39+
custom_commands.MenuFromCommand,
40+
custom_commands.MultiplePrompts,
4441
file.DirWithUntrackedFile,
42+
interactive_rebase.AmendMerge,
43+
interactive_rebase.One,
44+
stash.Rename,
45+
stash.Stash,
46+
stash.StashIncludingUntrackedFiles,
4547
}
4648

4749
func GetTests() []*components.IntegrationTest {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
initial commit

test/integration_new/stash/stash/expected/repo/.git_keep/FETCH_HEAD

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

0 commit comments

Comments
 (0)