-
Notifications
You must be signed in to change notification settings - Fork 119
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c0582a1
add .vscode folder to gitignore
beeceej 9db446e
build excluder
beeceej 292d9ed
Query excluder by regexp fix build regexp
beeceej 7c25432
integrate the excluder
beeceej ee0d045
add excluder tests
beeceej 5f86c60
Don't swallow error on regex
beeceej e1cdfac
add the ./* case to the case where the user appends '/'
beeceej f5ded91
make regexp type *regexp.Regex
beeceej 8b91445
refactor regexp building
beeceej 68885b8
refactor regexp building
beeceej 74c955c
refactor tests
beeceej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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() == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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!