Skip to content

Commit 0be5cee

Browse files
adlternativehuzhening.hzn
authored andcommitted
feat(search): support code search by zoekt
Signed-off-by: ZheNing Hu <[email protected]>
1 parent de26c8a commit 0be5cee

File tree

13 files changed

+779
-5
lines changed

13 files changed

+779
-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
@@ -1522,10 +1522,10 @@ LEVEL = Info
15221522
;; If empty then it defaults to `sources` only, as if you'd like to disable fully please see REPO_INDEXER_ENABLED.
15231523
;REPO_INDEXER_REPO_TYPES = sources,forks,mirrors,templates
15241524
;;
1525-
;; Code search engine type, could be `bleve` or `elasticsearch`.
1525+
;; Code search engine type, could be `bleve`, `zoekt` or `elasticsearch`.
15261526
;REPO_INDEXER_TYPE = bleve
15271527
;;
1528-
;; Index file used for code search. available when `REPO_INDEXER_TYPE` is bleve
1528+
;; Index file used for code search. available when `REPO_INDEXER_TYPE` is bleve or zoekt
15291529
;REPO_INDEXER_PATH = indexers/repos.bleve
15301530
;;
15311531
;; Code indexer connection string, available when `REPO_INDEXER_TYPE` is elasticsearch. i.e. http://elastic:changeme@localhost:9200
@@ -1535,10 +1535,10 @@ LEVEL = Info
15351535
;REPO_INDEXER_NAME = gitea_codes
15361536
;;
15371537
;; A comma separated list of glob patterns (see https://github.com/gobwas/glob) to include
1538-
;; in the index; default is empty
1538+
;; in the index; it's not compatible with the `zoekt` indexer type; default is empty
15391539
;REPO_INDEXER_INCLUDE =
15401540
;;
1541-
;; A comma separated list of glob patterns to exclude from the index; ; default is empty
1541+
;; A comma separated list of glob patterns to exclude from the index; it's not compatible with the `zoekt` indexer type; default is empty
15421542
;REPO_INDEXER_EXCLUDE =
15431543
;;
15441544
;MAX_FILE_SIZE = 1048576

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ require (
103103
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
104104
github.com/sassoftware/go-rpmutils v0.4.0
105105
github.com/sergi/go-diff v1.4.0
106+
github.com/sourcegraph/zoekt v0.0.0-20250407211326-2283cd5f430c
106107
github.com/stretchr/testify v1.11.1
107108
github.com/syndtr/goleveldb v1.0.0
108109
github.com/tstranex/u2f v1.0.0
@@ -141,6 +142,7 @@ require (
141142
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect
142143
github.com/DataDog/zstd v1.5.7 // indirect
143144
github.com/Microsoft/go-winio v0.6.2 // indirect
145+
github.com/RoaringBitmap/roaring v1.9.4 // indirect
144146
github.com/RoaringBitmap/roaring/v2 v2.10.0 // indirect
145147
github.com/STARRY-S/zip v0.2.3 // indirect
146148
github.com/andybalholm/brotli v1.2.0 // indirect
@@ -170,6 +172,7 @@ require (
170172
github.com/blevesearch/zapx/v14 v14.4.2 // indirect
171173
github.com/blevesearch/zapx/v15 v15.4.2 // indirect
172174
github.com/blevesearch/zapx/v16 v16.2.4 // indirect
175+
github.com/bmatcuk/doublestar v1.3.4 // indirect
173176
github.com/bmatcuk/doublestar/v4 v4.9.1 // indirect
174177
github.com/bodgit/plumbing v1.3.0 // indirect
175178
github.com/bodgit/sevenzip v1.6.1 // indirect
@@ -213,6 +216,8 @@ require (
213216
github.com/gorilla/css v1.0.1 // indirect
214217
github.com/gorilla/mux v1.8.1 // indirect
215218
github.com/gorilla/securecookie v1.1.2 // indirect
219+
github.com/grafana/regexp v0.0.0-20240607082908-2cb410fa05da // indirect
220+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
216221
github.com/hashicorp/errwrap v1.1.0 // indirect
217222
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
218223
github.com/hashicorp/go-multierror v1.1.1 // indirect
@@ -246,6 +251,7 @@ require (
246251
github.com/olekukonko/ll v0.1.0 // indirect
247252
github.com/olekukonko/tablewriter v1.0.9 // indirect
248253
github.com/onsi/ginkgo v1.16.5 // indirect
254+
github.com/opentracing/opentracing-go v1.2.0 // indirect
249255
github.com/philhofer/fwd v1.2.0 // indirect
250256
github.com/pierrec/lz4/v4 v4.1.22 // indirect
251257
github.com/pjbgf/sha1cd v0.4.0 // indirect
@@ -260,6 +266,7 @@ require (
260266
github.com/sirupsen/logrus v1.9.3 // indirect
261267
github.com/skeema/knownhosts v1.3.1 // indirect
262268
github.com/sorairolake/lzip-go v0.3.8 // indirect
269+
github.com/sourcegraph/go-ctags v0.0.0-20240424152308-4faeee4849da // indirect
263270
github.com/spf13/afero v1.15.0 // indirect
264271
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
265272
github.com/tinylib/msgp v1.4.0 // indirect

go.sum

Lines changed: 95 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
@@ -39,6 +39,10 @@ func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision s
3939
needGenesis = len(stdout) == 0
4040
}
4141

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

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"
@@ -117,7 +118,7 @@ func Init() {
117118

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

188208
default:
189209
log.Fatal("PID: %d Unknown Indexer type: %s", os.Getpid(), setting.Indexer.RepoType)
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)