Skip to content

Commit

Permalink
plumbing: gitignore, Fix loading of deeper ignored files
Browse files Browse the repository at this point in the history
Fix loading of `.gitignore` files in nested ignored directories.
These were not matched in the current implementation,
as that only checks the ignores of the current directory.

As a side-effect this change also leads to significantly improved performance:
in a repository with 3M ignored files `Status` now takes 5 s instead of 27 s.
  • Loading branch information
silkeh committed Nov 21, 2024
1 parent 8cef206 commit 763e3be
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions plumbing/format/gitignore/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"io"
"log/slog"

Check failure on line 7 in plumbing/format/gitignore/dir.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/log/slog)

Check failure on line 7 in plumbing/format/gitignore/dir.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

package log/slog is not in GOROOT (/Users/runner/hostedtoolcache/go/1.20.14/arm64/src/log/slog)

Check failure on line 7 in plumbing/format/gitignore/dir.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

package log/slog is not in GOROOT (C:\hostedtoolcache\windows\go\1.20.14\x64\src\log\slog)
"os"
"strings"

Expand Down Expand Up @@ -51,10 +52,15 @@ func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps [
// recursively traversing through the directory structure. The result is in
// the ascending order of priority (last higher).
func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) {
ps, _ = readIgnoreFile(fs, path, infoExcludeFile)
return extendPatterns(fs, nil, path)
}

func extendPatterns(fs billy.Filesystem, start []Pattern, path []string) (ps []Pattern, err error) {
dirps, _ := readIgnoreFile(fs, path, infoExcludeFile)
subps, _ := readIgnoreFile(fs, path, gitignoreFile)
ps = append(ps, subps...)
dirps = append(dirps, subps...)
ignores := append(start, dirps...)
ps = dirps

var fis []os.FileInfo
fis, err = fs.ReadDir(fs.Join(path...))
Expand All @@ -64,12 +70,16 @@ func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error)

for _, fi := range fis {
if fi.IsDir() && fi.Name() != gitDir {
if NewMatcher(ps).Match(append(path, fi.Name()), true) {
if len(path) >= 2 && strings.HasPrefix(path[1], "output") {
slog.Debug("Bla")
}

if NewMatcher(ignores).Match(append(path, fi.Name()), true) {
continue
}

var subps []Pattern
subps, err = ReadPatterns(fs, append(path, fi.Name()))
subps, err = extendPatterns(fs, append(start, dirps...), append(path, fi.Name()))
if err != nil {
return
}
Expand Down

0 comments on commit 763e3be

Please sign in to comment.