Skip to content

Commit

Permalink
make ignorepattern public and add method
Browse files Browse the repository at this point in the history
  • Loading branch information
phanirithvij committed Mar 25, 2021
1 parent 54b8a0b commit 803f0f6
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions ignore.go
Expand Up @@ -63,6 +63,7 @@ import (
// IgnoreParser is an interface with `MatchesPaths`.
type IgnoreParser interface {
MatchesPath(f string) bool
MatchesPathHow(f string) (bool, *IgnorePattern)
}

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -147,26 +148,28 @@ func getPatternFromLine(line string) (*regexp.Regexp, bool) {

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

// ignorePattern encapsulates a pattern and if it is a negated pattern.
type ignorePattern struct {
pattern *regexp.Regexp
negate bool
// IgnorePattern encapsulates a pattern and if it is a negated pattern.
type IgnorePattern struct {
Pattern *regexp.Regexp
Negate bool
LineNo int
Line string
}

// GitIgnore wraps a list of ignore pattern.
type GitIgnore struct {
patterns []*ignorePattern
patterns []*IgnorePattern
}

// CompileIgnoreLines accepts a variadic set of strings, and returns a GitIgnore
// instance 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 {
gi := &GitIgnore{}
for _, line := range lines {
for i, line := range lines {
pattern, negatePattern := getPatternFromLine(line)
if pattern != nil {
ip := &ignorePattern{pattern, negatePattern}
ip := &IgnorePattern{pattern, negatePattern, i, line}
gi.patterns = append(gi.patterns, ip)
}
}
Expand Down Expand Up @@ -208,10 +211,10 @@ func (gi *GitIgnore) MatchesPath(f string) bool {

matchesPath := false
for _, ip := range gi.patterns {
if ip.pattern.MatchString(f) {
if ip.Pattern.MatchString(f) {
// If this is a regular target (not negated with a gitignore
// exclude "!" etc)
if !ip.negate {
if !ip.Negate {
matchesPath = true
} else if matchesPath {
// Negated pattern, and matchesPath is already set
Expand All @@ -222,4 +225,28 @@ func (gi *GitIgnore) MatchesPath(f string) bool {
return matchesPath
}

// MatchesPathHow returns true, pattern if the given GitIgnore structure would target
// a given path string `f`.
func (gi *GitIgnore) MatchesPathHow(f string) (bool, *IgnorePattern) {
// Replace OS-specific path separator.
f = strings.Replace(f, string(os.PathSeparator), "/", -1)

matchesPath := false
var mip *IgnorePattern
for _, ip := range gi.patterns {
if ip.Pattern.MatchString(f) {
// If this is a regular target (not negated with a gitignore
// exclude "!" etc)
if !ip.Negate {
matchesPath = true
mip = ip
} else if matchesPath {
// Negated pattern, and matchesPath is already set
matchesPath = false
}
}
}
return matchesPath, mip
}

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

0 comments on commit 803f0f6

Please sign in to comment.