Skip to content

Commit 1c1676a

Browse files
committed
Use git ls-files to list all file suggestions
1 parent 9d24963 commit 1c1676a

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

pkg/commands/git_commands/working_tree.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,19 @@ func (self *WorkingTreeCommands) MergeFileForObjectIDs(strategy string, oursID s
451451

452452
return self.cmd.New(cmdArgs).RunWithOutput()
453453
}
454+
455+
// Returns all tracked files in the repo (not in the working tree). The returned entries are
456+
// relative paths to the repo root, using '/' as the path separator on all platforms.
457+
// Does not really belong in WorkingTreeCommands, but it's close enough, and we don't seem to have a
458+
// better place for it right now.
459+
func (self *WorkingTreeCommands) AllRepoFiles() ([]string, error) {
460+
cmdArgs := NewGitCmd("ls-files").Arg("-z").ToArgv()
461+
output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
462+
if err != nil {
463+
return nil, err
464+
}
465+
if output == "" {
466+
return []string{}, nil
467+
}
468+
return strings.Split(strings.TrimRight(output, "\x00"), "\x00"), nil
469+
}

pkg/commands/git_commands/working_tree_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,32 @@ func TestWorkingTreeResetHard(t *testing.T) {
563563
})
564564
}
565565
}
566+
567+
func TestWorkingTreeCommands_AllRepoFiles(t *testing.T) {
568+
scenarios := []struct {
569+
name string
570+
runner *oscommands.FakeCmdObjRunner
571+
expected []string
572+
}{
573+
{
574+
name: "no files",
575+
runner: oscommands.NewFakeRunner(t).
576+
ExpectGitArgs([]string{"ls-files", "-z"}, "", nil),
577+
expected: []string{},
578+
},
579+
{
580+
name: "two files",
581+
runner: oscommands.NewFakeRunner(t).
582+
ExpectGitArgs([]string{"ls-files", "-z"}, "dir/file1.txt\x00dir2/file2.go\x00", nil),
583+
expected: []string{"dir/file1.txt", "dir2/file2.go"},
584+
},
585+
}
586+
for _, s := range scenarios {
587+
t.Run(s.name, func(t *testing.T) {
588+
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
589+
result, err := instance.AllRepoFiles()
590+
assert.NoError(t, err)
591+
assert.Equal(t, s.expected, result)
592+
})
593+
}
594+
}

pkg/gui/controllers/helpers/suggestions_helper.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package helpers
22

33
import (
44
"fmt"
5-
"os"
65
"strings"
76

7+
"github.com/jesseduffield/generics/set"
88
"github.com/jesseduffield/gocui"
99
"github.com/jesseduffield/lazygit/pkg/commands/models"
1010
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
1111
"github.com/jesseduffield/lazygit/pkg/gui/types"
1212
"github.com/jesseduffield/lazygit/pkg/utils"
13-
"github.com/jesseduffield/minimal/gitignore"
1413
"github.com/samber/lo"
1514
"golang.org/x/exp/slices"
1615
"gopkg.in/ozeidan/fuzzy-patricia.v3/patricia"
@@ -92,22 +91,28 @@ func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*ty
9291
func (self *SuggestionsHelper) GetFilePathSuggestionsFunc() func(string) []*types.Suggestion {
9392
_ = self.c.WithWaitingStatus(self.c.Tr.LoadingFileSuggestions, func(gocui.Task) error {
9493
trie := patricia.NewTrie()
95-
// load every non-gitignored file in the repo
96-
ignore, err := gitignore.FromGit()
94+
95+
// load every file in the repo
96+
files, err := self.c.Git().WorkingTree.AllRepoFiles()
9797
if err != nil {
9898
return err
9999
}
100100

101-
err = ignore.Walk(".",
102-
func(path string, info os.FileInfo, err error) error {
103-
if err != nil {
104-
return err
105-
}
106-
if path != "." {
107-
trie.Insert(patricia.Prefix(path), path)
101+
seen := set.New[string]()
102+
for _, file := range files {
103+
// For every file we also want to add its parent directories, but only once.
104+
for i := range len(file) {
105+
if file[i] == '/' {
106+
dir := file[:i]
107+
if !seen.Includes(dir) {
108+
trie.Insert(patricia.Prefix(dir), dir)
109+
seen.Add(dir)
110+
}
108111
}
109-
return nil
110-
})
112+
}
113+
114+
trie.Insert(patricia.Prefix(file), file)
115+
}
111116

112117
// cache the trie for future use
113118
self.c.Model().FilesTrie = trie

0 commit comments

Comments
 (0)