Skip to content

Commit

Permalink
lint-staged: optimize for ignore files loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSingee committed Oct 13, 2023
1 parent 02cb885 commit b8d841b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
4 changes: 2 additions & 2 deletions internal/ext/lint-staged/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func searchConfigs(cwd, gitDir, configPath string) ([]*Config, error) {
return []*Config{config}, nil
}

cachedFiles, err := execGitZ([]string{"ls-files", "-z", "--full-name"}, gitDir)
cachedFiles, err := getCachedFiles(gitDir)
if err != nil {
return nil, ee.Wrap(err, "cannot get list of known files")
}

//otherFiles, err := execGitZ([]string{"ls-files", "-z", "--full-name", "--others", "--exclude-standard"}, gitDir)
//otherFiles, err := getUncommittedFiles(gitDir)
//if err != nil {
// return nil, ee.Wrap(err, "cannot get list of uncommitted files")
//}
Expand Down
8 changes: 8 additions & 0 deletions internal/ext/lint-staged/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ func execGitZ(args []string, dir string) ([]string, error) {

return parseGitZOutput(lines), nil
}

func getCachedFiles(gitDir string) ([]string, error) {
return execGitZ([]string{"ls-files", "-z", "--full-name"}, gitDir)
}

func getUncommittedFiles(gitDir string) ([]string, error) {
return execGitZ([]string{"ls-files", "-z", "--full-name", "--others", "--exclude-standard"}, gitDir)
}
28 changes: 27 additions & 1 deletion internal/ext/lint-staged/ignore.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package lintstaged

import (
"log/slog"
"path/filepath"
"strings"

"github.com/ImSingee/go-ex/ee"
"github.com/ImSingee/go-ex/exstrings"
"github.com/ImSingee/go-ex/mr"

"github.com/ImSingee/kitty/internal/lib/ignore"
)
Expand All @@ -18,10 +22,11 @@ var ignoreFilenames = []string{
}

func NewIgnoreChecker(repoRoot string) (*IgnoreChecker, error) {
ps, err := ignore.ReadPatterns(repoRoot, nil, ignoreFilenames...)
ps, err := parseIgnoreRules(repoRoot)
if err != nil {
return nil, ee.Wrap(err, "cannot read ignore patterns")
}

return &IgnoreChecker{ignore.NewMatcher(ps)}, nil
}

Expand All @@ -30,3 +35,24 @@ func (c *IgnoreChecker) ShouldIgnore(gitRelativePath string) bool {

return c.matcher.Match(parts, false)
}

func parseIgnoreRules(gitDir string) ([]ignore.Pattern, error) {
cachedFiles, err := getCachedFiles(gitDir)
if err != nil {
return nil, ee.Wrap(err, "cannot get list of known files")
}

possibleIgnoreRuleFiles := cachedFiles

possibleIgnoreRuleFiles = mr.Filter(possibleIgnoreRuleFiles, func(file string, _index int) bool {
return exstrings.InStringList(ignoreFilenames, filepath.Base(file))
})

slog.Debug("Found possible ignore rule files", "possibleIgnoreRuleFiles", possibleIgnoreRuleFiles, "possibleIgnoreRuleFilesCount", len(possibleIgnoreRuleFiles))

ps, err := ignore.ParsePatternFiles(gitDir, possibleIgnoreRuleFiles...)

slog.Debug("Load ignore patterns", "count", len(ps))

return ps, err
}
19 changes: 19 additions & 0 deletions internal/lib/ignore/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewMatcher(ps []Pattern) Matcher {
// ReadPatterns read and parse ignoreFileNames recursively
//
// The result is in the ascending order of priority (last higher).
//
// WARNING: This is too slow in most casts.
// You can use git to find the ignore files and use ParseIgnoreFiles to parse them.
func ReadPatterns(root string, dirs []string, ignoreFileNames ...string) ([]Pattern, error) {
ps, err := readIgnoreFiles(root, dirs, ignoreFileNames...)
if err != nil {
Expand Down Expand Up @@ -91,3 +94,19 @@ func readIgnoreFile(root string, dirs []string, ignoreFile string) (ps []Pattern

return
}

func ParsePatternFiles(root string, gitRelativePaths ...string) (ps []Pattern, err error) {
for _, filename := range gitRelativePaths {
parts := strings.Split(filename, "/")
dirs := parts[:len(parts)-1]
ignoreFile := parts[len(parts)-1]

subps, err := readIgnoreFile(root, dirs, ignoreFile)
if err != nil {
return nil, err
}

ps = append(ps, subps...)
}
return
}

0 comments on commit b8d841b

Please sign in to comment.