Skip to content

Commit 269d7ac

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

File tree

12 files changed

+803
-1
lines changed

12 files changed

+803
-1
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.

go.mod

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

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