Skip to content

Commit b8dfba8

Browse files
authored
Several small fixes to filtering mode (by path or author) (#4749)
- Refresh views properly when entering/exiting filtering mode - Fix filtering of stashes
2 parents 143d476 + ec82e7c commit b8dfba8

File tree

6 files changed

+88
-10
lines changed

6 files changed

+88
-10
lines changed

pkg/commands/git_commands/stash_loader.go

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

35-
cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--name-only", "--pretty=%ct|%gs").ToArgv()
35+
cmdArgs := NewGitCmd("stash").Arg("list", "--name-only", "--pretty=%gd:%ct|%gs").ToArgv()
3636
rawString, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
3737
if err != nil {
3838
return self.getUnfilteredStashEntries()
3939
}
4040
stashEntries := []*models.StashEntry{}
4141
var currentStashEntry *models.StashEntry
42-
lines := utils.SplitNul(rawString)
42+
lines := utils.SplitLines(rawString)
4343
isAStash := func(line string) bool { return strings.HasPrefix(line, "stash@{") }
44-
re := regexp.MustCompile(`stash@\{(\d+)\}`)
44+
re := regexp.MustCompile(`^stash@\{(\d+)\}:(.*)$`)
4545

4646
outer:
4747
for i := 0; i < len(lines); i++ {
48-
if !isAStash(lines[i]) {
48+
match := re.FindStringSubmatch(lines[i])
49+
if match == nil {
4950
continue
5051
}
51-
match := re.FindStringSubmatch(lines[i])
5252
idx, err := strconv.Atoi(match[1])
5353
if err != nil {
5454
return self.getUnfilteredStashEntries()
5555
}
56-
currentStashEntry = stashEntryFromLine(lines[i], idx)
56+
currentStashEntry = stashEntryFromLine(match[2], idx)
5757
for i+1 < len(lines) && !isAStash(lines[i+1]) {
5858
i++
59-
if lines[i] == filterPath {
59+
if strings.HasPrefix(lines[i], filterPath) {
6060
stashEntries = append(stashEntries, currentStashEntry)
6161
continue outer
6262
}

pkg/gui/controllers/filtering_menu_action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
78
"github.com/jesseduffield/lazygit/pkg/gui/types"
89
)
910

@@ -122,7 +123,7 @@ func (self *FilteringMenuAction) setFiltering() error {
122123

123124
self.c.Context().Push(self.c.Contexts().LocalCommits, types.OnFocusOpts{})
124125

125-
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}, Then: func() {
126+
self.c.Refresh(types.RefreshOptions{Scope: helpers.ScopesToRefreshWhenFilteringModeChanges(), Then: func() {
126127
self.c.Contexts().LocalCommits.SetSelection(0)
127128
self.c.Contexts().LocalCommits.HandleFocus(types.OnFocusOpts{})
128129
}})

pkg/gui/controllers/helpers/mode_helper.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (self *ModeHelper) ClearFiltering() error {
168168
}
169169

170170
self.c.Refresh(types.RefreshOptions{
171-
Scope: []types.RefreshableView{types.COMMITS},
171+
Scope: ScopesToRefreshWhenFilteringModeChanges(),
172172
Then: func() {
173173
// Find the commit that was last selected in filtering mode, and select it again after refreshing
174174
if !self.c.Contexts().LocalCommits.SelectCommitByHash(selectedCommitHash) {
@@ -178,12 +178,24 @@ func (self *ModeHelper) ClearFiltering() error {
178178
// before we entered filtering
179179
self.c.Contexts().LocalCommits.SelectCommitByHash(self.c.Modes().Filtering.GetSelectedCommitHash())
180180
}
181-
self.c.Contexts().LocalCommits.HandleFocus(types.OnFocusOpts{})
181+
182+
self.c.PostRefreshUpdate(self.c.Contexts().LocalCommits)
182183
},
183184
})
184185
return nil
185186
}
186187

188+
// Stashes really only need to be refreshed when filtering by path, not by author, but it's too much
189+
// work to distinguish this, and refreshing stashes is fast, so we don't bother
190+
func ScopesToRefreshWhenFilteringModeChanges() []types.RefreshableView {
191+
return []types.RefreshableView{
192+
types.COMMITS,
193+
types.SUB_COMMITS,
194+
types.REFLOG,
195+
types.STASH,
196+
}
197+
}
198+
187199
func (self *ModeHelper) SetSuppressRebasingMode(value bool) {
188200
self.suppressRebasingMode = value
189201
}

pkg/gui/controllers/helpers/refresh_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ func (self *RefreshHelper) refreshCommitsWithLimit() error {
345345
}
346346

347347
func (self *RefreshHelper) refreshSubCommitsWithLimit() error {
348+
if self.c.Contexts().SubCommits.GetRef() == nil {
349+
return nil
350+
}
351+
348352
self.c.Mutexes().SubCommitsMutex.Lock()
349353
defer self.c.Mutexes().SubCommitsMutex.Unlock()
350354

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 FilterByPath = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Filter the stash list by path",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.EmptyCommit("initial commit")
15+
shell.CreateFileAndAdd("file1", "content")
16+
shell.Stash("file1")
17+
shell.CreateDir("subdir")
18+
shell.CreateFileAndAdd("subdir/file2", "content")
19+
shell.Stash("subdir/file2")
20+
shell.CreateFileAndAdd("file1", "other content")
21+
shell.Stash("file1 again")
22+
},
23+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
24+
filterBy := func(path string) {
25+
t.GlobalPress(keys.Universal.FilteringMenu)
26+
t.ExpectPopup().Menu().
27+
Title(Equals("Filtering")).
28+
Select(Contains("Enter path to filter by")).
29+
Confirm()
30+
31+
t.ExpectPopup().Prompt().
32+
Title(Equals("Enter path:")).
33+
Type(path).
34+
Confirm()
35+
}
36+
37+
t.Views().Stash().
38+
Lines(
39+
Contains("file1 again"),
40+
Contains("subdir/file2"),
41+
Contains("file1"),
42+
)
43+
44+
filterBy("file1")
45+
46+
t.Views().Stash().
47+
Lines(
48+
Contains("file1 again"),
49+
Contains("file1"),
50+
)
51+
52+
t.GlobalPress(keys.Universal.Return)
53+
filterBy("subdir")
54+
55+
t.Views().Stash().
56+
Lines(
57+
Contains("subdir/file2"),
58+
)
59+
},
60+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ var tests = []*components.IntegrationTest{
352352
stash.CreateBranch,
353353
stash.Drop,
354354
stash.DropMultiple,
355+
stash.FilterByPath,
355356
stash.Pop,
356357
stash.PreventDiscardingFileChanges,
357358
stash.Rename,

0 commit comments

Comments
 (0)