Skip to content

Commit 6c6eee4

Browse files
committed
feat(search): support code search by zoekt
Signed-off-by: ZheNing Hu <[email protected]>
1 parent c27d87a commit 6c6eee4

File tree

13 files changed

+807
-5
lines changed

13 files changed

+807
-5
lines changed

assets/go-licenses.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

custom/conf/app.example.ini

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,10 +1483,10 @@ LEVEL = Info
14831483
;; If empty then it defaults to `sources` only, as if you'd like to disable fully please see REPO_INDEXER_ENABLED.
14841484
;REPO_INDEXER_REPO_TYPES = sources,forks,mirrors,templates
14851485
;;
1486-
;; Code search engine type, could be `bleve` or `elasticsearch`.
1486+
;; Code search engine type, could be `bleve`, `zoekt` or `elasticsearch`.
14871487
;REPO_INDEXER_TYPE = bleve
14881488
;;
1489-
;; Index file used for code search. available when `REPO_INDEXER_TYPE` is bleve
1489+
;; Index file used for code search. available when `REPO_INDEXER_TYPE` is bleve or zoekt
14901490
;REPO_INDEXER_PATH = indexers/repos.bleve
14911491
;;
14921492
;; Code indexer connection string, available when `REPO_INDEXER_TYPE` is elasticsearch. i.e. http://elastic:changeme@localhost:9200
@@ -1496,10 +1496,10 @@ LEVEL = Info
14961496
;REPO_INDEXER_NAME = gitea_codes
14971497
;;
14981498
;; A comma separated list of glob patterns (see https://github.com/gobwas/glob) to include
1499-
;; in the index; default is empty
1499+
;; in the index; it's not compatible with the `zoekt` indexer type; default is empty
15001500
;REPO_INDEXER_INCLUDE =
15011501
;;
1502-
;; A comma separated list of glob patterns to exclude from the index; ; default is empty
1502+
;; A comma separated list of glob patterns to exclude from the index; it's not compatible with the `zoekt` indexer type; default is empty
15031503
;REPO_INDEXER_EXCLUDE =
15041504
;;
15051505
;MAX_FILE_SIZE = 1048576

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ require (
105105
github.com/sassoftware/go-rpmutils v0.4.0
106106
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3
107107
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92
108+
github.com/sourcegraph/zoekt v0.0.0-20250407211326-2283cd5f430c
108109
github.com/stretchr/testify v1.10.0
109110
github.com/syndtr/goleveldb v1.0.0
110111
github.com/tstranex/u2f v1.0.0
@@ -175,6 +176,7 @@ require (
175176
github.com/blevesearch/zapx/v14 v14.3.10 // indirect
176177
github.com/blevesearch/zapx/v15 v15.3.13 // indirect
177178
github.com/blevesearch/zapx/v16 v16.1.5 // indirect
179+
github.com/bmatcuk/doublestar v1.3.4 // indirect
178180
github.com/bmatcuk/doublestar/v4 v4.8.1 // indirect
179181
github.com/boombuler/barcode v1.0.2 // indirect
180182
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 // indirect
@@ -231,6 +233,8 @@ require (
231233
github.com/gorilla/handlers v1.5.2 // indirect
232234
github.com/gorilla/mux v1.8.1 // indirect
233235
github.com/gorilla/securecookie v1.1.2 // indirect
236+
github.com/grafana/regexp v0.0.0-20240607082908-2cb410fa05da // indirect
237+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
234238
github.com/hashicorp/errwrap v1.1.0 // indirect
235239
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
236240
github.com/hashicorp/go-multierror v1.1.1 // indirect
@@ -264,6 +268,7 @@ require (
264268
github.com/oklog/ulid v1.3.1 // indirect
265269
github.com/olekukonko/tablewriter v0.0.5 // indirect
266270
github.com/onsi/ginkgo v1.16.5 // indirect
271+
github.com/opentracing/opentracing-go v1.2.0 // indirect
267272
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
268273
github.com/pierrec/lz4/v4 v4.1.22 // indirect
269274
github.com/pjbgf/sha1cd v0.3.2 // indirect
@@ -282,6 +287,7 @@ require (
282287
github.com/sirupsen/logrus v1.9.3 // indirect
283288
github.com/skeema/knownhosts v1.3.1 // indirect
284289
github.com/sourcegraph/conc v0.3.0 // indirect
290+
github.com/sourcegraph/go-ctags v0.0.0-20240424152308-4faeee4849da // indirect
285291
github.com/spf13/afero v1.14.0 // indirect
286292
github.com/spf13/cast v1.7.1 // indirect
287293
github.com/spf13/pflag v1.0.6 // indirect

go.sum

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

modules/indexer/code/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision s
3737
needGenesis = len(stdout) == 0
3838
}
3939

40+
// TODO: check if zoekt index file meta status is not sync with db index status, if not, get genesis changes
41+
//if setting.Indexer.RepoType == "zoekt" {
42+
//}
43+
4044
if needGenesis {
4145
return genesisChanges(ctx, repo, revision)
4246
}

modules/indexer/code/indexer.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/indexer/code/bleve"
1919
"code.gitea.io/gitea/modules/indexer/code/elasticsearch"
2020
"code.gitea.io/gitea/modules/indexer/code/internal"
21+
"code.gitea.io/gitea/modules/indexer/code/zoekt"
2122
"code.gitea.io/gitea/modules/log"
2223
"code.gitea.io/gitea/modules/process"
2324
"code.gitea.io/gitea/modules/queue"
@@ -116,7 +117,7 @@ func Init() {
116117

117118
// Create the Queue
118119
switch setting.Indexer.RepoType {
119-
case "bleve", "elasticsearch":
120+
case "bleve", "elasticsearch", "zoekt":
120121
handler := func(items ...*internal.IndexerData) (unhandled []*internal.IndexerData) {
121122
indexer := *globalIndexer.Load()
122123
for _, indexerData := range items {
@@ -183,6 +184,25 @@ func Init() {
183184
close(waitChannel)
184185
log.Fatal("PID: %d Unable to initialize the elasticsearch Repository Indexer connstr: %s Error: %v", os.Getpid(), setting.Indexer.RepoConnStr, err)
185186
}
187+
case "zoekt":
188+
log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoPath)
189+
defer func() {
190+
if err := recover(); err != nil {
191+
log.Error("PANIC whilst initializing repository indexer: %v\nStacktrace: %s", err, log.Stack(2))
192+
log.Error("The indexer files are likely corrupted and may need to be deleted")
193+
log.Error("You can completely remove the \"%s\" directory to make Gitea recreate the indexes", setting.Indexer.RepoPath)
194+
}
195+
}()
196+
197+
rIndexer = zoekt.NewIndexer(setting.Indexer.RepoPath)
198+
existed, err = rIndexer.Init(ctx)
199+
if err != nil {
200+
cancel()
201+
(*globalIndexer.Load()).Close()
202+
close(waitChannel)
203+
204+
log.Fatal("PID: %d Unable to initialize the zoekt Repository Indexer at path: %s Error: %v", os.Getpid(), setting.Indexer.RepoPath, err)
205+
}
186206

187207
default:
188208
log.Fatal("PID: %d Unknown Indexer type: %s", os.Getpid(), setting.Indexer.RepoType)

modules/indexer/code/zoekt/utils.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package zoekt
5+
6+
import "unicode/utf8"
7+
8+
// Bitmap used by func special to check whether a character needs to be escaped.
9+
var specialBytes [16]byte
10+
11+
// special reports whether byte b needs to be escaped by QuoteMeta.
12+
func special(b byte) bool {
13+
return b < utf8.RuneSelf && specialBytes[b%16]&(1<<(b/16)) != 0
14+
}
15+
16+
func init() {
17+
for _, b := range []byte(`-:\.+*?()|[]{}^$`) {
18+
specialBytes[b%16] |= 1 << (b / 16)
19+
}
20+
}
21+
22+
func QuoteMeta(s string) string {
23+
// A byte loop is correct because all metacharacters are ASCII.
24+
var i int
25+
for i = 0; i < len(s); i++ {
26+
if special(s[i]) {
27+
break
28+
}
29+
}
30+
// No meta characters found, so return original string.
31+
if i >= len(s) {
32+
return s
33+
}
34+
35+
b := make([]byte, 3*len(s)-2*i)
36+
copy(b, s[:i])
37+
j := i
38+
for ; i < len(s); i++ {
39+
if special(s[i]) {
40+
b[j] = '\\'
41+
j++
42+
b[j] = '\\'
43+
j++
44+
}
45+
b[j] = s[i]
46+
j++
47+
}
48+
return string(b[:j])
49+
}

0 commit comments

Comments
 (0)