Skip to content

Commit

Permalink
Improve GitIgnore struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhiram committed Jun 11, 2018
1 parent fc6676d commit d310757
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
40 changes: 22 additions & 18 deletions ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ import (

////////////////////////////////////////////////////////////

// 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
}
Expand Down Expand Up @@ -148,26 +149,30 @@ func getPatternFromLine(line string) (*regexp.Regexp, bool) {

////////////////////////////////////////////////////////////

// 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
Expand All @@ -194,21 +199,20 @@ func CompileIgnoreFileAndLines(fpath string, lines ...string) (*GitIgnore, error

////////////////////////////////////////////////////////////

// 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
}
}
Expand Down
11 changes: 5 additions & 6 deletions version_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package ignore

// 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"
)

0 comments on commit d310757

Please sign in to comment.