Improve GitIgnore struct
diff --git a/ignore.go b/ignore.go index 0751db3..846b52c 100644 --- a/ignore.go +++ b/ignore.go
@@ -60,8 +60,9 @@ //////////////////////////////////////////////////////////// -// An IgnoreParser is an interface which exposes two methods: -// MatchesPath() - Returns true if the path is targeted by the patterns compiled in the GitIgnore structure +// An IgnoreParser is an interface which exposes a single method: +// MatchesPath() - Returns true if the path is targeted by the patterns compiled +// in the GitIgnore structure type IgnoreParser interface { MatchesPath(f string) bool } @@ -148,26 +149,30 @@ //////////////////////////////////////////////////////////// -// GitIgnore is a struct which contains a slice of regexp.Regexp -// patterns +// ignorePattern encapsulates a pattern and if it is a negated pattern. +type ignorePattern struct { + pattern *regexp.Regexp + negate bool +} + +// GitIgnore wraps a list of ignore pattern. type GitIgnore struct { - patterns []*regexp.Regexp // List of regexp patterns which this ignore file applies - negate []bool // List of booleans which determine if the pattern is negated + patterns []*ignorePattern } // Accepts a variadic set of strings, and returns a GitIgnore object which // converts and appends the lines in the input to regexp.Regexp patterns // held within the GitIgnore objects "patterns" field func CompileIgnoreLines(lines ...string) (*GitIgnore, error) { - g := new(GitIgnore) + gi := &GitIgnore{} for _, line := range lines { pattern, negatePattern := getPatternFromLine(line) if pattern != nil { - g.patterns = append(g.patterns, pattern) - g.negate = append(g.negate, negatePattern) + ip := &ignorePattern{pattern, negatePattern} + gi.patterns = append(gi.patterns, ip) } } - return g, nil + return gi, nil } // Accepts a ignore file as the input, parses the lines out of the file @@ -194,21 +199,20 @@ //////////////////////////////////////////////////////////// -// MatchesPath is an interface function for the IgnoreParser interface. -// It returns true if the given GitIgnore structure would target a given -// path string "f" -func (g GitIgnore) MatchesPath(f string) bool { +// MatchesPath returns true if the given GitIgnore structure would target +// a given path string `f`. +func (gi *GitIgnore) MatchesPath(f string) bool { // Replace OS-specific path separator. f = strings.Replace(f, string(os.PathSeparator), "/", -1) matchesPath := false - for idx, pattern := range g.patterns { - if pattern.MatchString(f) { + for _, ip := range gi.patterns { + if ip.pattern.MatchString(f) { // If this is a regular target (not negated with a gitignore exclude "!" etc) - if !g.negate[idx] { + if !ip.negate { matchesPath = true - // Negated pattern, and matchesPath is already set } else if matchesPath { + // Negated pattern, and matchesPath is already set matchesPath = false } }
diff --git a/version_gen.go b/version_gen.go index d7c8231..720a057 100644 --- a/version_gen.go +++ b/version_gen.go
@@ -2,12 +2,11 @@ // WARNING: Auto generated version file. Do not edit this file by hand. // WARNING: go get github.com/sabhiram/gover to manage this file. -// Version: 1.0.1 - +// Version: 1.0.2 const ( - Major = 1 - Minor = 0 - Patch = 1 + Major = 1 + Minor = 0 + Patch = 2 - Version = "1.0.1" + Version = "1.0.2" )