Skip to content

Commit 896a16b

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

File tree

13 files changed

+807
-5
lines changed

13 files changed

+807
-5
lines changed

assets/go-licenses.json

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

custom/conf/app.example.ini

+4-4
Original file line numberDiff line numberDiff line change
@@ -1494,10 +1494,10 @@ LEVEL = Info
14941494
;; If empty then it defaults to `sources` only, as if you'd like to disable fully please see REPO_INDEXER_ENABLED.
14951495
;REPO_INDEXER_REPO_TYPES = sources,forks,mirrors,templates
14961496
;;
1497-
;; Code search engine type, could be `bleve` or `elasticsearch`.
1497+
;; Code search engine type, could be `bleve`, `zoekt` or `elasticsearch`.
14981498
;REPO_INDEXER_TYPE = bleve
14991499
;;
1500-
;; Index file used for code search. available when `REPO_INDEXER_TYPE` is bleve
1500+
;; Index file used for code search. available when `REPO_INDEXER_TYPE` is bleve or zoekt
15011501
;REPO_INDEXER_PATH = indexers/repos.bleve
15021502
;;
15031503
;; Code indexer connection string, available when `REPO_INDEXER_TYPE` is elasticsearch. i.e. http://elastic:changeme@localhost:9200
@@ -1507,10 +1507,10 @@ LEVEL = Info
15071507
;REPO_INDEXER_NAME = gitea_codes
15081508
;;
15091509
;; A comma separated list of glob patterns (see https://github.com/gobwas/glob) to include
1510-
;; in the index; default is empty
1510+
;; in the index; it's not compatible with the `zoekt` indexer type; default is empty
15111511
;REPO_INDEXER_INCLUDE =
15121512
;;
1513-
;; A comma separated list of glob patterns to exclude from the index; ; default is empty
1513+
;; A comma separated list of glob patterns to exclude from the index; it's not compatible with the `zoekt` indexer type; default is empty
15141514
;REPO_INDEXER_EXCLUDE =
15151515
;;
15161516
;MAX_FILE_SIZE = 1048576

go.mod

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ require (
106106
github.com/sassoftware/go-rpmutils v0.4.0
107107
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3
108108
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92
109+
github.com/sourcegraph/zoekt v0.0.0-20250407211326-2283cd5f430c
109110
github.com/stretchr/testify v1.10.0
110111
github.com/syndtr/goleveldb v1.0.0
111112
github.com/tstranex/u2f v1.0.0
@@ -176,6 +177,7 @@ require (
176177
github.com/blevesearch/zapx/v14 v14.3.10 // indirect
177178
github.com/blevesearch/zapx/v15 v15.3.13 // indirect
178179
github.com/blevesearch/zapx/v16 v16.1.5 // indirect
180+
github.com/bmatcuk/doublestar v1.3.4 // indirect
179181
github.com/bmatcuk/doublestar/v4 v4.8.1 // indirect
180182
github.com/boombuler/barcode v1.0.2 // indirect
181183
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 // indirect
@@ -232,6 +234,8 @@ require (
232234
github.com/gorilla/handlers v1.5.2 // indirect
233235
github.com/gorilla/mux v1.8.1 // indirect
234236
github.com/gorilla/securecookie v1.1.2 // indirect
237+
github.com/grafana/regexp v0.0.0-20240607082908-2cb410fa05da // indirect
238+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
235239
github.com/hashicorp/errwrap v1.1.0 // indirect
236240
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
237241
github.com/hashicorp/go-multierror v1.1.1 // indirect
@@ -265,6 +269,7 @@ require (
265269
github.com/oklog/ulid v1.3.1 // indirect
266270
github.com/olekukonko/tablewriter v0.0.5 // indirect
267271
github.com/onsi/ginkgo v1.16.5 // indirect
272+
github.com/opentracing/opentracing-go v1.2.0 // indirect
268273
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
269274
github.com/pierrec/lz4/v4 v4.1.22 // indirect
270275
github.com/pjbgf/sha1cd v0.3.2 // indirect
@@ -283,6 +288,7 @@ require (
283288
github.com/sirupsen/logrus v1.9.3 // indirect
284289
github.com/skeema/knownhosts v1.3.1 // indirect
285290
github.com/sourcegraph/conc v0.3.0 // indirect
291+
github.com/sourcegraph/go-ctags v0.0.0-20240424152308-4faeee4849da // indirect
286292
github.com/spf13/afero v1.14.0 // indirect
287293
github.com/spf13/cast v1.7.1 // indirect
288294
github.com/spf13/pflag v1.0.6 // indirect

go.sum

+126
Large diffs are not rendered by default.

modules/indexer/code/git.go

+4
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

+21-1
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

+49
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)