Skip to content

Commit

Permalink
feat: add age on stash lines
Browse files Browse the repository at this point in the history
  • Loading branch information
AzraelSec committed Dec 21, 2023
1 parent 66b608b commit 34de01f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
26 changes: 22 additions & 4 deletions pkg/commands/git_commands/stash_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
Expand Down Expand Up @@ -32,14 +33,14 @@ func (self *StashLoader) GetStashEntries(filterPath string) []*models.StashEntry
return self.getUnfilteredStashEntries()
}

cmdArgs := NewGitCmd("stash").Arg("list", "--name-only").ToArgv()
cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--name-only", "--pretty=%gs date@{%ct}").ToArgv()
rawString, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
if err != nil {
return self.getUnfilteredStashEntries()
}
stashEntries := []*models.StashEntry{}
var currentStashEntry *models.StashEntry
lines := utils.SplitLines(rawString)
lines := utils.SplitNul(rawString)
isAStash := func(line string) bool { return strings.HasPrefix(line, "stash@{") }
re := regexp.MustCompile(`stash@\{(\d+)\}`)

Expand All @@ -66,7 +67,7 @@ outer:
}

func (self *StashLoader) getUnfilteredStashEntries() []*models.StashEntry {
cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--pretty=%gs").ToArgv()
cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--pretty=%gs date@{%ct}").ToArgv()

rawString, _ := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
return lo.Map(utils.SplitNul(rawString), func(line string, index int) *models.StashEntry {
Expand All @@ -75,8 +76,25 @@ func (self *StashLoader) getUnfilteredStashEntries() []*models.StashEntry {
}

func (c *StashLoader) stashEntryFromLine(line string, index int) *models.StashEntry {
return &models.StashEntry{
model := &models.StashEntry{
Name: line,
Index: index,
}

regular := regexp.MustCompile(`(?P<line>.*?) date@\{(?P<age>.*?)\}`)
match := regular.FindStringSubmatch(line)
if match == nil {
return model
}

msg, tstr := match[1], match[2]
t, err := strconv.ParseInt(tstr, 10, 64)
if err != nil {
return model
}

unixTimestamp := time.Unix(t, 0)
model.Time = &unixTimestamp
model.Name = msg
return model
}
10 changes: 7 additions & 3 deletions pkg/commands/git_commands/stash_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git_commands

import (
"testing"
"time"

"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
Expand All @@ -17,29 +18,32 @@ func TestGetStashEntries(t *testing.T) {
expectedStashEntries []*models.StashEntry
}

unixTimestamp := time.Unix(1703096639, 0)
scenarios := []scenario{
{
"No stash entries found",
"",
oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%gs"}, "", nil),
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%gs date@{%ct}"}, "", nil),
[]*models.StashEntry{},
},
{
"Several stash entries found",
"",
oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%gs"},
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build\x00WIP on master: bb86a3f update github template\x00",
ExpectGitArgs([]string{"stash", "list", "-z", "--pretty=%gs date@{%ct}"},
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build date@{1703096639}\x00WIP on master: bb86a3f update github template date@{1703096639}\x00",
nil,
),
[]*models.StashEntry{
{
Index: 0,
Time: &unixTimestamp,
Name: "WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
},
{
Index: 1,
Time: &unixTimestamp,
Name: "WIP on master: bb86a3f update github template",
},
},
Expand Down
34 changes: 33 additions & 1 deletion pkg/commands/models/stash_entry.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
package models

import "fmt"
import (
"errors"
"fmt"
"time"

"github.com/jesseduffield/lazygit/pkg/utils"
)

// StashEntry : A git stash entry
type StashEntry struct {
Index int
Time *time.Time
Name string
}

func (s *StashEntry) FullRefName() string {
return s.RefName()
}

func (s *StashEntry) hasTime() bool {
return s.Time != nil
}

func (s *StashEntry) Age() (string, error) {
if !s.hasTime() {
return "", errors.New("no time associated to this stash entry")
}
return utils.UnixToTimeAgo(s.Time.Unix()), nil
}

func (s *StashEntry) Line() string {
line := s.Name
if !s.hasTime() {
return line
}

age, err := s.Age()
if err != nil {
return line
}

return fmt.Sprintf("%s %s", age, line)
}

func (s *StashEntry) RefName() string {
return fmt.Sprintf("stash@{%d}", s.Index)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/gui/presentation/stash_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func getStashEntryDisplayStrings(s *models.StashEntry, diffed bool) []string {
if icons.IsIconEnabled() {
res = append(res, textStyle.Sprint(icons.IconForStash(s)))
}
res = append(res, textStyle.Sprint(s.Name))

res = append(res, textStyle.Sprint(s.Line()))
return res
}
6 changes: 3 additions & 3 deletions pkg/integration/tests/stash/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ var Rename = NewIntegrationTest(NewIntegrationTestArgs{
t.Views().Stash().
Focus().
Lines(
Equals("On master: bar"),
Equals("On master: foo"),
Contains("On master: bar"),
Contains("On master: foo"),
).
SelectNextItem().
Press(keys.Stash.RenameStash).
Tap(func() {
t.ExpectPopup().Prompt().Title(Equals("Rename stash: stash@{1}")).Type(" baz").Confirm()
}).
SelectedLine(Equals("On master: foo baz"))
SelectedLine(Contains("On master: foo baz"))
},
})
2 changes: 1 addition & 1 deletion pkg/integration/tests/stash/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Stash = NewIntegrationTest(NewIntegrationTestArgs{

t.Views().Stash().
Lines(
Contains("my stashed file"),
MatchesRegexp("\\ds .* my stashed file"),
)

t.Views().Files().
Expand Down

0 comments on commit 34de01f

Please sign in to comment.