Skip to content

Commit 72a650c

Browse files
author
Guillermo Prandi
committed
Add wildcard support to REPO_INDEXER_EXTENSIONS
1 parent 021014a commit 72a650c

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
177177

178178
- `REPO_INDEXER_ENABLED`: **false**: Enables code search (uses a lot of disk space, about 6 times more than the repository size).
179179
- `REPO_INDEXER_PATH`: **indexers/repos.bleve**: Index file used for code search.
180-
- `REPO_INDEXER_EXTENSIONS`: **empty**: A comma separated list of file extensions to exclude from the index; a \`.' matches files with no extension. An empty list means do not exclude any files.
181-
- `REPO_EXTENSIONS_LIST_INCLUDE`: **false**: If true, `REPO_INDEXER_EXTENSIONS` are the file extensions to include rather than exclude from the index.
180+
- `REPO_INDEXER_PATTERNS`: **empty**: A comma separated list of file name patterns (see https://github.com/gobwas/glob) to **exclude** from the index. An empty list means do not exclude any files. Use `**.txt` to match any files with .txt extension.
181+
- `REPO_PATTERNS_INCLUDE`: **false**: If true, `REPO_INDEXER_PATTERNS` are the file extensions to **include** rather than exclude from the index.
182182
- `UPDATE_BUFFER_LEN`: **20**: Buffer length of index request.
183183
- `MAX_FILE_SIZE`: **1048576**: Maximum size in bytes of files to be indexed.
184184

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require (
5454
github.com/go-redis/redis v6.15.2+incompatible
5555
github.com/go-sql-driver/mysql v1.4.1
5656
github.com/go-xorm/xorm v0.7.4
57+
github.com/gobwas/glob v0.2.3
5758
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561
5859
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
5960
github.com/google/go-github/v24 v24.0.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:9wScpmSP5A3Bk
145145
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:56xuuqnHyryaerycW3BfssRdxQstACi0Epw/yC5E2xM=
146146
github.com/go-xorm/xorm v0.7.4 h1:g/NgC590SzqV5VKmdRDNe/K3Holw3YJUCXX28r+rFGw=
147147
github.com/go-xorm/xorm v0.7.4/go.mod h1:vpza5fydeRgt+stvo9qgMhSNohYqmNt0I1/D6hkCekA=
148+
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
149+
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
148150
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561 h1:deE7ritpK04PgtpyVOS2TYcQEld9qLCD5b5EbVNOuLA=
149151
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561/go.mod h1:YgYOrVn3Nj9Tq0EvjmFbphRytDj7JNRoWSStJZWDJTQ=
150152
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

models/repo_indexer.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,16 @@ func addDelete(filename string, repo *Repository, batch rupture.FlushingBatch) e
232232
}
233233

234234
func isIndexable(entry *git.TreeEntry) bool {
235-
if setting.Indexer.FileExtensions != nil {
236-
var ext string
237-
parts := strings.Split(entry.Name(), ".")
238-
cnt := len(parts)
239-
if cnt > 1 {
240-
ext = strings.ToLower(parts[cnt-1])
235+
if setting.Indexer.FilePatterns != nil {
236+
var found bool
237+
name := strings.ToLower(entry.Name())
238+
for _, g := range setting.Indexer.FilePatterns {
239+
if g.Match(name) {
240+
found = true
241+
break
242+
}
241243
}
242-
if setting.Indexer.FileExtensions[ext] != setting.Indexer.IncludeExtensions {
244+
if found != setting.Indexer.IncludePatterns {
243245
return false
244246
}
245247
}

modules/setting/indexer.go

+19-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"path"
99
"path/filepath"
1010
"strings"
11+
12+
"code.gitea.io/gitea/modules/log"
13+
14+
"github.com/gobwas/glob"
1115
)
1216

1317
// enumerates all the indexer queue types
@@ -30,8 +34,8 @@ var (
3034
IssueQueueDir string
3135
IssueQueueConnStr string
3236
IssueQueueBatchNumber int
33-
FileExtensions map[string]bool
34-
IncludeExtensions bool
37+
FilePatterns []glob.Glob
38+
IncludePatterns bool
3539
}{
3640
IssueType: "bleve",
3741
IssuePath: "indexers/issues.bleve",
@@ -54,8 +58,8 @@ func newIndexerService() {
5458
if !filepath.IsAbs(Indexer.RepoPath) {
5559
Indexer.RepoPath = path.Join(AppWorkPath, Indexer.RepoPath)
5660
}
57-
Indexer.FileExtensions = extensionsFromString(sec.Key("REPO_INDEXER_EXTENSIONS").MustString(""))
58-
Indexer.IncludeExtensions = sec.Key("REPO_EXTENSIONS_LIST_INCLUDE").MustBool(false)
61+
Indexer.FilePatterns = extensionsFromString(sec.Key("REPO_INDEXER_PATTERNS").MustString(""))
62+
Indexer.IncludePatterns = sec.Key("REPO_PATTERNS_INCLUDE").MustBool(false)
5963

6064
Indexer.UpdateQueueLength = sec.Key("UPDATE_BUFFER_LEN").MustInt(20)
6165
Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(1024 * 1024)
@@ -65,25 +69,20 @@ func newIndexerService() {
6569
Indexer.IssueQueueBatchNumber = sec.Key("ISSUE_INDEXER_QUEUE_BATCH_NUMBER").MustInt(20)
6670
}
6771

68-
func extensionsFromString(from string) map[string]bool {
69-
extmap := make(map[string]bool)
70-
for _, ext := range strings.Split(strings.ToLower(from), ",") {
71-
ext = strings.TrimSpace(ext)
72-
// Accept *.txt, .txt and txt. Also use . to mean no ext
73-
if strings.HasPrefix(ext, "*.") {
74-
ext = ext[1:]
75-
}
76-
if ext == "." {
77-
extmap[""] = true
78-
} else {
79-
ext = strings.TrimPrefix(ext, ".")
80-
if ext != "" {
81-
extmap[ext] = true
72+
func extensionsFromString(from string) []glob.Glob {
73+
extarr := make([]glob.Glob, 0, 10)
74+
for _, expr := range strings.Split(strings.ToLower(from), ",") {
75+
expr = strings.TrimSpace(expr)
76+
if expr != "" {
77+
if g, err := glob.Compile(expr, '.', '/'); err != nil {
78+
log.Trace("Index file extensions: '%s': bad pattern: %v", expr, err)
79+
} else {
80+
extarr = append(extarr, g)
8281
}
8382
}
8483
}
85-
if len(extmap) == 0 {
84+
if len(extarr) == 0 {
8685
return nil
8786
}
88-
return extmap
87+
return extarr
8988
}

vendor/modules.txt

+9
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ github.com/go-redis/redis/internal/util
158158
github.com/go-sql-driver/mysql
159159
# github.com/go-xorm/xorm v0.7.4
160160
github.com/go-xorm/xorm
161+
# github.com/gobwas/glob v0.2.3
162+
github.com/gobwas/glob
163+
github.com/gobwas/glob/compiler
164+
github.com/gobwas/glob/syntax
165+
github.com/gobwas/glob/match
166+
github.com/gobwas/glob/syntax/ast
167+
github.com/gobwas/glob/util/runes
168+
github.com/gobwas/glob/syntax/lexer
169+
github.com/gobwas/glob/util/strings
161170
# github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561
162171
github.com/gogits/chardet
163172
# github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14

0 commit comments

Comments
 (0)