Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use regex to detect exclusions instead of string.Contains #21

Merged
merged 11 commits into from May 24, 2017
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Folders
_obj
_test
.vscode

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down Expand Up @@ -34,4 +35,6 @@ vendor/

# Compiled executables
fsql
debug
main
build/
47 changes: 47 additions & 0 deletions query/excluder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package query

import (
"fmt"
"regexp"
"strings"
)

//Excluder allows us to support different methods of excluding in the future
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

I think we can use this same interface to detect paths we've seen and apply regex to that to check for leading/trailing slashes (this is also naively implemented with a simple map and no regex right now).

Let me know if you'd like to work on that as well (should be in a different PR).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make an issue for this, see if anyone picks it up, otherwise I'll gladly take care of it!

type Excluder interface {
ShouldExclude(path string) bool
}

//RegexpExclude uses regular expressions to tell if a file/path should be excluded
type RegexpExclude struct {
exclusions []string
regex *regexp.Regexp
}

//ShouldExclude will return a boolean denoting whether or not the path should be excluded based on the given slice of exclusions
func (r *RegexpExclude) ShouldExclude(path string) bool {
if r.regex == nil {
r.buildRegex()
}
if r.regex.String() == "" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

return false
}

return r.regex.MatchString(path)
}

func (r *RegexpExclude) buildRegex() {
numExclusion := len(r.exclusions)
tmpExclusions := make([]string, numExclusion, numExclusion)
for i, exclusion := range r.exclusions {
if strings.HasSuffix(exclusion, "/") {
tmpExclusions[i] = fmt.Sprintf("^%s(/.*)?$", escape(strings.TrimRight(exclusion, "/")))
} else {
tmpExclusions[i] = fmt.Sprintf("^%s(/.*)?$", escape(exclusion))
}
}
r.regex = regexp.MustCompile(strings.Join(tmpExclusions, "|"))
}

func escape(str string) string {
return strings.Replace(str, ".", "\\.", -1)
}
39 changes: 39 additions & 0 deletions query/excluder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package query

import "testing"

func TestShouldExclude_ExpectAllExcluded(t *testing.T) {
exclusions := []string{".git", ".gitignore"}
excluder := RegexpExclude{exclusions: exclusions}

b := excluder.ShouldExclude(".git")
if b == false {
t.Fail()
}

b = excluder.ShouldExclude(".git/")
if b == false {
t.Fail()
}

b = excluder.ShouldExclude(".git/some/other/file")
if b == false {
t.Fail()
}

b = excluder.ShouldExclude(".gitignore")
if b == false {
t.Fail()
}
}

func TestShouldExclude_ExpectNotExcluded(t *testing.T) {
exclusions := []string{".git"}
excluder := RegexpExclude{exclusions: exclusions}

b := excluder.ShouldExclude(".gitignore")

if b == true {
t.Fail()
}
}
15 changes: 2 additions & 13 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package query
import (
"os"
"path/filepath"
"strings"
)

const (
Expand Down Expand Up @@ -48,18 +47,8 @@ func (q *Query) HasAttribute(attributes ...string) bool {
// evaluating the condition tree for each file. This method calls workFunc on
// each "successful" file.
func (q *Query) Execute(workFunc interface{}) {
containsAny := func(path string) bool {
for _, exclusion := range q.Sources["exclude"] {
if strings.Contains(path, exclusion) {
return true
}
}

return false
}

seen := make(map[string]bool)

excluder := &RegexpExclude{exclusions: q.Sources["exclude"]}
for _, src := range q.Sources["include"] {
filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if path == "." || path == ".." || err != nil {
Expand All @@ -72,7 +61,7 @@ func (q *Query) Execute(workFunc interface{}) {
}
seen[path] = true

if containsAny(path) || !q.ConditionTree.evaluateTree(path, info) {
if excluder.ShouldExclude(path) || !q.ConditionTree.evaluateTree(path, info) {
return nil
}

Expand Down