Skip to content

Commit 0ac6b42

Browse files
stefanhallerkarolzwolak
authored andcommitted
Log the hash of dropped stashes (jesseduffield#4850)
### PR Description If you dropped/popped a stash accidentally, the logged hash can help recover it more easily. Supersedes jesseduffield#4847.
2 parents 2b60f83 + bf4f7e2 commit 0ac6b42

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

pkg/commands/git_commands/stash_loader.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (self *StashLoader) GetStashEntries(filterPath string) []*models.StashEntry
3232
return self.getUnfilteredStashEntries()
3333
}
3434

35-
cmdArgs := NewGitCmd("stash").Arg("list", "--name-only", "--pretty=%gd:%ct|%gs").ToArgv()
35+
cmdArgs := NewGitCmd("stash").Arg("list", "--name-only", "--pretty=%gd:%H|%ct|%gs").ToArgv()
3636
rawString, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
3737
if err != nil {
3838
return self.getUnfilteredStashEntries()
@@ -66,7 +66,7 @@ outer:
6666
}
6767

6868
func (self *StashLoader) getUnfilteredStashEntries() []*models.StashEntry {
69-
cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--pretty=%ct|%gs").ToArgv()
69+
cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--pretty=%H|%ct|%gs").ToArgv()
7070

7171
rawString, _ := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
7272
return lo.Map(utils.SplitNul(rawString), func(line string, index int) *models.StashEntry {
@@ -80,6 +80,12 @@ func stashEntryFromLine(line string, index int) *models.StashEntry {
8080
Index: index,
8181
}
8282

83+
hash, line, ok := strings.Cut(line, "|")
84+
if !ok {
85+
return model
86+
}
87+
model.Hash = hash
88+
8389
tstr, msg, ok := strings.Cut(line, "|")
8490
if !ok {
8591
return model

pkg/commands/git_commands/stash_loader_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package git_commands
22

33
import (
4+
"fmt"
45
"testing"
6+
"time"
57

68
"github.com/jesseduffield/lazygit/pkg/commands/models"
79
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
@@ -17,30 +19,38 @@ func TestGetStashEntries(t *testing.T) {
1719
expectedStashEntries []*models.StashEntry
1820
}
1921

22+
hoursAgo := time.Now().Unix() - 3*3600 - 1800
23+
daysAgo := time.Now().Unix() - 3*3600*24 - 3600*12
24+
2025
scenarios := []scenario{
2126
{
2227
"No stash entries found",
2328
"",
2429
oscommands.NewFakeRunner(t).
25-
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%ct|%gs"}, "", nil),
30+
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%H|%ct|%gs"}, "", nil),
2631
[]*models.StashEntry{},
2732
},
2833
{
2934
"Several stash entries found",
3035
"",
3136
oscommands.NewFakeRunner(t).
32-
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%ct|%gs"},
33-
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build\x00WIP on master: bb86a3f update github template\x00",
34-
nil,
35-
),
37+
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%H|%ct|%gs"},
38+
fmt.Sprintf("fa1afe1|%d|WIP on add-pkg-commands-test: 55c6af2 increase parallel build\x00deadbeef|%d|WIP on master: bb86a3f update github template\x00",
39+
hoursAgo,
40+
daysAgo,
41+
), nil),
3642
[]*models.StashEntry{
3743
{
38-
Index: 0,
39-
Name: "WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
44+
Index: 0,
45+
Name: "WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
46+
Recency: "3h",
47+
Hash: "fa1afe1",
4048
},
4149
{
42-
Index: 1,
43-
Name: "WIP on master: bb86a3f update github template",
50+
Index: 1,
51+
Name: "WIP on master: bb86a3f update github template",
52+
Recency: "3d",
53+
Hash: "deadbeef",
4454
},
4555
},
4656
},

pkg/commands/models/stash_entry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type StashEntry struct {
77
Index int
88
Recency string
99
Name string
10+
Hash string
1011
}
1112

1213
func (s *StashEntry) FullRefName() string {

pkg/gui/controllers/stash_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (self *StashController) handleStashApply(stashEntry *models.StashEntry) err
112112
Title: self.c.Tr.StashApply,
113113
Prompt: self.c.Tr.SureApplyStashEntry,
114114
HandleConfirm: func() error {
115-
self.c.LogAction(self.c.Tr.Actions.Stash)
115+
self.c.LogAction(self.c.Tr.Actions.ApplyStash)
116116
err := self.c.Git().Stash.Apply(stashEntry.Index)
117117
self.postStashRefresh()
118118
if err != nil {
@@ -128,7 +128,8 @@ func (self *StashController) handleStashApply(stashEntry *models.StashEntry) err
128128

129129
func (self *StashController) handleStashPop(stashEntry *models.StashEntry) error {
130130
pop := func() error {
131-
self.c.LogAction(self.c.Tr.Actions.Stash)
131+
self.c.LogAction(self.c.Tr.Actions.PopStash)
132+
self.c.LogCommand("Popping stash "+stashEntry.Hash, false)
132133
err := self.c.Git().Stash.Pop(stashEntry.Index)
133134
self.postStashRefresh()
134135
if err != nil {
@@ -160,8 +161,9 @@ func (self *StashController) handleStashDrop(stashEntries []*models.StashEntry)
160161
Title: self.c.Tr.StashDrop,
161162
Prompt: self.c.Tr.SureDropStashEntry,
162163
HandleConfirm: func() error {
163-
self.c.LogAction(self.c.Tr.Actions.Stash)
164+
self.c.LogAction(self.c.Tr.Actions.DropStash)
164165
for i := len(stashEntries) - 1; i >= 0; i-- {
166+
self.c.LogCommand("Dropping stash "+stashEntries[i].Hash, false)
165167
err := self.c.Git().Stash.Drop(stashEntries[i].Index)
166168
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
167169
if err != nil {

pkg/i18n/english.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,9 @@ type Actions struct {
10141014
UpdateRemote string
10151015
ApplyPatch string
10161016
Stash string
1017+
PopStash string
1018+
ApplyStash string
1019+
DropStash string
10171020
RenameStash string
10181021
RemoveSubmodule string
10191022
ResetSubmodule string
@@ -2056,6 +2059,9 @@ func EnglishTranslationSet() *TranslationSet {
20562059
UpdateRemote: "Update remote",
20572060
ApplyPatch: "Apply patch",
20582061
Stash: "Stash",
2062+
PopStash: "Pop stash",
2063+
ApplyStash: "Apply stash",
2064+
DropStash: "Drop stash",
20592065
RenameStash: "Rename stash",
20602066
RemoveSubmodule: "Remove submodule",
20612067
ResetSubmodule: "Reset submodule",

0 commit comments

Comments
 (0)