From f742009c559a34959d28306ec13149efff398fff Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 9 Jul 2025 14:31:46 +0530 Subject: [PATCH] Ensure global gitignore file is not loaded in tests --- kittens/choose_files/scan.go | 18 ++++++++++++++---- kittens/choose_files/scan_test.go | 3 +++ tools/ignorefiles/api.go | 1 + tools/ignorefiles/gitignore.go | 2 ++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/kittens/choose_files/scan.go b/kittens/choose_files/scan.go index 52b5dba7d..fd39400cb 100644 --- a/kittens/choose_files/scan.go +++ b/kittens/choose_files/scan.go @@ -82,14 +82,14 @@ type FileSystemScanner struct { err error } -func NewFileSystemScanner(root_dir string, notify chan bool, filter_func func(string) bool) (fss *FileSystemScanner) { +func new_filesystem_scanner(root_dir string, notify chan bool, filter_func func(string) bool) (fss *FileSystemScanner) { ans := &FileSystemScanner{root_dir: root_dir, listeners: []chan bool{notify}, collection: NewResultCollection(4096)} ans.in_progress.Store(true) ans.keep_going.Store(true) ans.dir_reader = os.ReadDir ans.file_reader = os.ReadFile ans.filter_func = utils.IfElse(filter_func == nil, accept_all, filter_func) - ans.global_gitignore = ignorefiles.GlobalGitignore() + ans.global_gitignore = ignorefiles.NewGitignore() return ans } @@ -324,7 +324,7 @@ func (fss *FileSystemScanner) worker() { sortable = append(sortable, &arena[i]) } if has_dot_git { - if fss.global_gitignore != nil { + if fss.global_gitignore.Len() > 0 { add_ignore_file_from_impl(fss.global_gitignore) } add_ignore_file(filepath.Join(dot_git, "info", "exclude")) @@ -392,6 +392,8 @@ type FileSystemScorer struct { current_worker_wait *sync.WaitGroup scorer *fzf.FuzzyMatcher dir_reader func(path string) ([]fs.DirEntry, error) + file_reader func(path string) ([]byte, error) + global_gitignore ignorefiles.IgnoreFile } func NewFileSystemScorer(root_dir, query string, filter Filter, only_dirs bool, on_results func(error, bool)) (ans *FileSystemScorer) { @@ -409,10 +411,18 @@ func (fss *FileSystemScorer) Start() { fss.is_complete.Store(false) fss.keep_going.Store(true) if fss.scanner == nil { - sc := NewFileSystemScanner(fss.root_dir, on_results, fss.filter.Match) + sc := new_filesystem_scanner(fss.root_dir, on_results, fss.filter.Match) if fss.dir_reader != nil { sc.dir_reader = fss.dir_reader } + if fss.file_reader != nil { + sc.file_reader = fss.file_reader + } + if fss.global_gitignore != nil { + sc.global_gitignore = fss.global_gitignore + } else { + sc.global_gitignore = ignorefiles.GlobalGitignore() + } fss.scanner = sc fss.scanner.Start() } else { diff --git a/kittens/choose_files/scan_test.go b/kittens/choose_files/scan_test.go index 7b0c2c85b..57fd3db9e 100644 --- a/kittens/choose_files/scan_test.go +++ b/kittens/choose_files/scan_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/kovidgoyal/kitty/tools/ignorefiles" "github.com/kovidgoyal/kitty/tools/utils" ) @@ -163,6 +164,7 @@ func TestChooseFilesScoring(t *testing.T) { } }) s.dir_reader = root.ReadDir + s.global_gitignore = ignorefiles.NewGitignore() s.Start() wg.Wait() results := func() (ans []string) { @@ -309,6 +311,7 @@ func run_scoring(b *testing.B, depth, breadth int, query string) { } }) s.dir_reader = root.ReadDir + s.global_gitignore = ignorefiles.NewGitignore() b.StartTimer() s.scanner.Start() s.Start() diff --git a/tools/ignorefiles/api.go b/tools/ignorefiles/api.go index 4821fafbf..3422f6465 100644 --- a/tools/ignorefiles/api.go +++ b/tools/ignorefiles/api.go @@ -10,6 +10,7 @@ import ( var _ = fmt.Print type IgnoreFile interface { + Len() int // number of rules LoadString(string) error LoadBytes([]byte) error LoadLines(...string) error diff --git a/tools/ignorefiles/gitignore.go b/tools/ignorefiles/gitignore.go index 7c266b708..b6beb0d47 100644 --- a/tools/ignorefiles/gitignore.go +++ b/tools/ignorefiles/gitignore.go @@ -29,6 +29,8 @@ type Gitignore struct { line_number_offset int } +func (g Gitignore) Len() int { return len(g.patterns) } + func (g Gitignore) IsIgnored(relpath string, ftype os.FileMode) (is_ignored bool, linenum_of_matching_rule int, pattern string) { if os.PathSeparator != '/' { relpath = strings.ReplaceAll(relpath, string(os.PathSeparator), "/")