[FEAT] Support Include/Exclude Filters for Grep (#3058)
fixes `TestRepoSearch` failing occasionally Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3058 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com> Co-committed-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
This commit is contained in:
parent
0a51ae26bc
commit
baac15f316
3 changed files with 67 additions and 16 deletions
|
@ -13,6 +13,8 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
type GrepResult struct {
|
||||
|
@ -58,7 +60,15 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
|||
} else {
|
||||
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
|
||||
}
|
||||
cmd.AddDynamicArguments(cmp.Or(opts.RefName, "HEAD"))
|
||||
// pathspec
|
||||
files := make([]string, 0, len(setting.Indexer.IncludePatterns)+len(setting.Indexer.ExcludePatterns))
|
||||
for _, expr := range setting.Indexer.IncludePatterns {
|
||||
files = append(files, expr.Pattern())
|
||||
}
|
||||
for _, expr := range setting.Indexer.ExcludePatterns {
|
||||
files = append(files, ":^"+expr.Pattern())
|
||||
}
|
||||
cmd.AddDynamicArguments(cmp.Or(opts.RefName, "HEAD")).AddDashesAndList(files...)
|
||||
opts.MaxResultLimit = cmp.Or(opts.MaxResultLimit, 50)
|
||||
stderr := bytes.Buffer{}
|
||||
err = cmd.Run(&RunOpts{
|
||||
|
|
|
@ -30,8 +30,8 @@ var Indexer = struct {
|
|||
RepoConnStr string
|
||||
RepoIndexerName string
|
||||
MaxIndexerFileSize int64
|
||||
IncludePatterns []glob.Glob
|
||||
ExcludePatterns []glob.Glob
|
||||
IncludePatterns []Glob
|
||||
ExcludePatterns []Glob
|
||||
ExcludeVendored bool
|
||||
}{
|
||||
IssueType: "bleve",
|
||||
|
@ -50,6 +50,19 @@ var Indexer = struct {
|
|||
ExcludeVendored: true,
|
||||
}
|
||||
|
||||
type Glob struct {
|
||||
glob glob.Glob
|
||||
pattern string
|
||||
}
|
||||
|
||||
func (g *Glob) Match(s string) bool {
|
||||
return g.glob.Match(s)
|
||||
}
|
||||
|
||||
func (g *Glob) Pattern() string {
|
||||
return g.pattern
|
||||
}
|
||||
|
||||
func loadIndexerFrom(rootCfg ConfigProvider) {
|
||||
sec := rootCfg.Section("indexer")
|
||||
Indexer.IssueType = sec.Key("ISSUE_INDEXER_TYPE").MustString("bleve")
|
||||
|
@ -90,15 +103,15 @@ func loadIndexerFrom(rootCfg ConfigProvider) {
|
|||
}
|
||||
|
||||
// IndexerGlobFromString parses a comma separated list of patterns and returns a glob.Glob slice suited for repo indexing
|
||||
func IndexerGlobFromString(globstr string) []glob.Glob {
|
||||
extarr := make([]glob.Glob, 0, 10)
|
||||
func IndexerGlobFromString(globstr string) []Glob {
|
||||
extarr := make([]Glob, 0, 10)
|
||||
for _, expr := range strings.Split(strings.ToLower(globstr), ",") {
|
||||
expr = strings.TrimSpace(expr)
|
||||
if expr != "" {
|
||||
if g, err := glob.Compile(expr, '.', '/'); err != nil {
|
||||
log.Info("Invalid glob expression '%s' (skipped): %v", expr, err)
|
||||
} else {
|
||||
extarr = append(extarr, g)
|
||||
extarr = append(extarr, Glob{glob: g, pattern: expr})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue