Skip to content

Commit

Permalink
Deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Dec 7, 2023
1 parent 9d6d4b1 commit 113df08
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 97 deletions.
3 changes: 2 additions & 1 deletion cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func actionCI(c *cli.Context) error {
}

var entries []discovery.Entry
finder := discovery.NewGitBranchFinder(git.RunGit, includeRe, excludeRe, baseBranch, meta.cfg.CI.MaxCommits, meta.cfg.Parser.CompileRelaxed())
filter := discovery.NewPathFilter(includeRe, excludeRe, meta.cfg.Parser.CompileRelaxed())
finder := discovery.NewGitBranchFinder(git.RunGit, filter, baseBranch, meta.cfg.CI.MaxCommits)
entries, err = finder.Find()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func actionLint(c *cli.Context) error {
return fmt.Errorf("at least one file or directory required")
}

finder := discovery.NewGlobFinder(paths, meta.cfg.Parser.CompileRelaxed())
finder := discovery.NewGlobFinder(paths, discovery.NewPathFilter(nil, nil, meta.cfg.Parser.CompileRelaxed()))
entries, err := finder.Find()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func newProblemCollector(cfg config.Config, paths []string, minSeverity checks.S
}

func (c *problemCollector) scan(ctx context.Context, workers int, gen *config.PrometheusGenerator) error {
finder := discovery.NewGlobFinder(c.paths, c.cfg.Parser.CompileRelaxed())
finder := discovery.NewGlobFinder(c.paths, discovery.NewPathFilter(nil, nil, c.cfg.Parser.CompileRelaxed()))
// nolint: contextcheck
entries, err := finder.Find()
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions internal/discovery/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package discovery

import "regexp"

func NewPathFilter(include, exclude, relaxed []*regexp.Regexp) PathFilter {
return PathFilter{
include: include,
exclude: exclude,
relaxed: relaxed,
}
}

type PathFilter struct {
include []*regexp.Regexp
exclude []*regexp.Regexp
relaxed []*regexp.Regexp
}

func (pf PathFilter) IsPathAllowed(path string) bool {
if len(pf.include) == 0 && len(pf.exclude) == 0 {
return true
}

for _, pattern := range pf.exclude {
if pattern.MatchString(path) {
return false
}
}

for _, pattern := range pf.include {
if pattern.MatchString(path) {
return true
}
}

return false
}

func (pf PathFilter) IsRelaxed(path string) bool {
for _, r := range pf.relaxed {
if v := r.MatchString(path); v {
return true
}
}
return false
}
41 changes: 7 additions & 34 deletions internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"log/slog"
"regexp"
"strings"

"golang.org/x/exp/slices"
Expand All @@ -16,29 +15,23 @@ import (

func NewGitBranchFinder(
gitCmd git.CommandRunner,
include []*regexp.Regexp,
exclude []*regexp.Regexp,
filter PathFilter,
baseBranch string,
maxCommits int,
relaxed []*regexp.Regexp,
) GitBranchFinder {
return GitBranchFinder{
gitCmd: gitCmd,
include: include,
exclude: exclude,
filter: filter,
baseBranch: baseBranch,
maxCommits: maxCommits,
relaxed: relaxed,
}
}

type GitBranchFinder struct {
gitCmd git.CommandRunner
include []*regexp.Regexp
exclude []*regexp.Regexp
filter PathFilter
baseBranch string
maxCommits int
relaxed []*regexp.Regexp
}

func (f GitBranchFinder) Find() (entries []Entry, err error) {
Expand Down Expand Up @@ -68,7 +61,7 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
}

for _, change := range changes {
if !f.isPathAllowed(change.Path.After.Name) {
if !f.filter.IsPathAllowed(change.Path.After.Name) {
slog.Debug("Skipping file due to include/exclude rules", slog.String("path", change.Path.After.Name))
continue
}
Expand All @@ -78,13 +71,13 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
change.Path.Before.EffectivePath(),
change.Path.Before.Name,
bytes.NewReader(change.Body.Before),
!matchesAny(f.relaxed, change.Path.Before.Name),
!f.filter.IsRelaxed(change.Path.Before.Name),
)
entriesAfter, err = readRules(
change.Path.After.EffectivePath(),
change.Path.After.Name,
bytes.NewReader(change.Body.After),
!matchesAny(f.relaxed, change.Path.After.Name),
!f.filter.IsRelaxed(change.Path.After.Name),
)
if err != nil {
return nil, fmt.Errorf("invalid file syntax: %w", err)
Expand Down Expand Up @@ -164,34 +157,14 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
}

for _, entry := range symlinks {
if f.isPathAllowed(entry.SourcePath) {
if f.filter.IsPathAllowed(entry.SourcePath) {
entries = append(entries, entry)
}
}

return entries, nil
}

func (f GitBranchFinder) isPathAllowed(path string) bool {
if len(f.include) == 0 && len(f.exclude) == 0 {
return true
}

for _, pattern := range f.exclude {
if pattern.MatchString(path) {
return false
}
}

for _, pattern := range f.include {
if pattern.MatchString(path) {
return true
}
}

return false
}

func (f GitBranchFinder) shouldSkipAllChecks(changes []*git.FileChange) (bool, error) {
commits := map[string]struct{}{}
for _, change := range changes {
Expand Down
Loading

0 comments on commit 113df08

Please sign in to comment.