Skip to content

Commit

Permalink
index: add a search option to exclude files given a regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
vrischmann committed Oct 2, 2016
1 parent f95e9a9 commit 9b81292
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ type IndexOptions struct {
}

type SearchOptions struct {
IgnoreCase bool
LinesOfContext uint
FileRegexp string
Offset int
Limit int
IgnoreCase bool
LinesOfContext uint
FileRegexp string
ExcludeFileRegexp string
Offset int
Limit int
}

type Match struct {
Expand Down Expand Up @@ -167,6 +168,14 @@ func (n *Index) Search(pat string, opt *SearchOptions) (*SearchResponse, error)
}
}

var efre *regexp.Regexp
if opt.ExcludeFileRegexp != "" {
efre, err = regexp.Compile(opt.ExcludeFileRegexp)
if err != nil {
return nil, err
}
}

files := n.idx.PostingQuery(index.RegexpQuery(re.Syntax))
for _, file := range files {
var matches []*Match
Expand All @@ -178,6 +187,11 @@ func (n *Index) Search(pat string, opt *SearchOptions) (*SearchResponse, error)
continue
}

// reject files that match the exclude file pattern
if efre != nil && efre.MatchString(name, true, true) > 0 {
continue
}

filesOpened++
if err := g.grep2File(filepath.Join(n.Ref.dir, "raw", name), re, int(opt.LinesOfContext),
func(line []byte, lineno int, before [][]byte, after [][]byte) (bool, error) {
Expand Down

0 comments on commit 9b81292

Please sign in to comment.