-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
381 additions
and
206 deletions.
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
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,60 @@ | ||
package ignore | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// IgnoreFile is the name of the ignore file. | ||
const IgnoreFile = ".multignore" | ||
|
||
// Filter is a list of paths to ignore. | ||
type Filter []Rule | ||
|
||
// New returns a new ignore filter. | ||
func New(dir string, patterns ...string) Filter { | ||
var rules []Rule | ||
for _, p := range patterns { | ||
rule := ParseRule(filepath.Join(dir, p)) | ||
rules = append(rules, rule) | ||
} | ||
|
||
return Filter(rules) | ||
} | ||
|
||
// Load returns the ignore filter from the given directory. | ||
func Load(dir string) (Filter, error) { | ||
data, err := os.ReadFile(filepath.Join(dir, IgnoreFile)) | ||
if os.IsNotExist(err) { | ||
return nil, nil | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patterns := strings.Split(string(data), "\n") | ||
return New(dir, patterns...), nil | ||
} | ||
|
||
// Match returns true if the path matches any ignore rules. | ||
func (f Filter) Match(name string) bool { | ||
for _, r := range f { | ||
if match, _ := r.Match(name); match { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// Merge combines the ignore rules with the other ignore. | ||
func (f Filter) Merge(other Filter) Filter { | ||
var merge Filter | ||
for _, r := range other { | ||
merge = append(merge, r) | ||
} | ||
|
||
return append(f, merge...) | ||
} |
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,58 @@ | ||
package ignore | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMatch(t *testing.T) { | ||
ignore := New("test", "*.exe") | ||
|
||
if !ignore.Match("test/foo/bar.exe") { | ||
t.Error("expected ignore to match") | ||
} | ||
|
||
if !ignore.Match("foo.exe") { | ||
t.Error("expected ignore to match") | ||
} | ||
} | ||
|
||
func TestLoad(t *testing.T) { | ||
ignore, err := Load("testdata") | ||
if err != nil { | ||
t.Fatal("failed to load ignore file") | ||
} | ||
|
||
if !ignore.Match("foo/bar") { | ||
t.Error("expected ignore to match") | ||
} | ||
|
||
if !ignore.Match("bar/foo") { | ||
t.Error("expected ignore to match") | ||
} | ||
|
||
if ignore.Match("foo.exe") { | ||
t.Error("expected ignore not to match") | ||
} | ||
} | ||
|
||
func TestMerge(t *testing.T) { | ||
ignore, err := Load("testdata") | ||
if err != nil { | ||
t.Fatal("failed to load ignore file") | ||
} | ||
|
||
other := New("test", "*.exe") | ||
merge := ignore.Merge(other) | ||
|
||
if !merge.Match("foo/bar") { | ||
t.Error("expected ignore to match") | ||
} | ||
|
||
if !merge.Match("bar/foo") { | ||
t.Error("expected ignore to match") | ||
} | ||
|
||
if !merge.Match("foo.exe") { | ||
t.Error("expected ignore to match") | ||
} | ||
} |
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,79 @@ | ||
package ignore | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Rule is used to match file paths. | ||
type Rule interface { | ||
// Match returns true if the path matches. | ||
Match(string) (bool, error) | ||
} | ||
|
||
// compile time interface checks | ||
var _ Rule = (*NegateRule)(nil) | ||
var _ Rule = (BaseRule)("") | ||
var _ Rule = (PathRule)("") | ||
var _ Rule = (EmptyRule)("") | ||
|
||
// NegateRule negates a rule. | ||
type NegateRule struct { | ||
wrapped Rule | ||
} | ||
|
||
// BaseRule matches the base of the path. | ||
type BaseRule string | ||
|
||
// PathRule matches the entire path. | ||
type PathRule string | ||
|
||
// EmptyRule never matches. | ||
type EmptyRule string | ||
|
||
// Match returns true if the path matches. | ||
func (r NegateRule) Match(name string) (bool, error) { | ||
match, err := r.wrapped.Match(name) | ||
return !match, err | ||
} | ||
|
||
// Match returns true if the path matches. | ||
func (r BaseRule) Match(name string) (bool, error) { | ||
base := filepath.Base(name) | ||
return filepath.Match(string(r), base) | ||
} | ||
|
||
// Match returns true if the path matches. | ||
func (r PathRule) Match(name string) (bool, error) { | ||
return filepath.Match(string(r), name) | ||
} | ||
|
||
// Match returns true if the path matches. | ||
func (r EmptyRule) Match(name string) (bool, error) { | ||
return false, nil | ||
} | ||
|
||
// ParseRule returns a rule for the given pattern. | ||
func ParseRule(pattern string) Rule { | ||
pattern = strings.TrimSpace(pattern) | ||
if pattern == "" { | ||
return EmptyRule(pattern) | ||
} | ||
|
||
if strings.HasPrefix(pattern, "#") { | ||
return EmptyRule(pattern) | ||
} | ||
|
||
var rule Rule | ||
if strings.Contains(pattern, "/") { | ||
rule = PathRule(pattern) | ||
} else { | ||
rule = BaseRule(filepath.Base(pattern)) | ||
} | ||
|
||
if strings.HasPrefix(pattern, "!") { | ||
rule = NegateRule{rule} | ||
} | ||
|
||
return rule | ||
} |
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,7 @@ | ||
# *.exe | ||
|
||
foo | ||
|
||
!bar | ||
|
||
/foo/bar |
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
Oops, something went wrong.