Files
gitea/services/codesearch/codesearch.go
T
Rafail Giavrimisandsilverwind 38389f00be feat: unify code search, add search API (#39403)
Adds GitHub-style code search endpoints and makes code search behave the
same in the web UI and the API, whether it runs on git grep, bleve or
Elasticsearch.

- `GET /repos/{owner}/{repo}/code/search` searches any branch, tag or
commit with git grep, so it works without the code indexer.
- `GET /search/code` searches the default branches of all accessible
repositories with the code indexer.
- One query syntax everywhere: every term must match a file's content or
path, case-insensitively. It supports `"phrases"`, `/regexp/` (git grep
only) and the `path:`, `language:`, `repo:`, `user:` and `org:`
qualifiers. Invalid queries return an error.
- All engines apply the same include, exclude, vendored and size rules,
and return every match. The web UI shows them as multiple snippets per
file with the matched text highlighted.

<img width="359" height="295" alt="image"
src="https://github.com/user-attachments/assets/59b7aa66-b1dc-4938-9ef9-66668ae08d7c"
/>

Fixes: https://github.com/go-gitea/gitea/issues/10706
Fixed: https://github.com/go-gitea/gitea/issues/15841
Fixes: https://github.com/go-gitea/gitea/issues/31375
Related: https://gitea.com/gitea/gitea-mcp/issues/280
Docs: https://gitea.com/gitea/docs/pulls/615

AI usage disclosure: Yes, I used AI to write code, review changes and
verify the factuality of the overview I provided. I take full
responsibility for the code in this PR.

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-13 07:24:37 +00:00

103 lines
3.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package codesearch
import (
"context"
"errors"
"slices"
"gitea.dev/models/db"
repo_model "gitea.dev/models/repo"
user_model "gitea.dev/models/user"
"gitea.dev/modules/container"
"gitea.dev/modules/git"
"gitea.dev/modules/indexer"
code_indexer "gitea.dev/modules/indexer/code"
"gitea.dev/modules/indexer/code/gitgrep"
)
var ErrIndexerUnavailable = errors.New("code indexer is unavailable")
type Engine int
const (
Indexer Engine = iota
GitGrep
)
type Options struct {
Engine Engine
Doer *user_model.User
RepoID int64
OwnerName string
GitRepo *git.Repository
Ref git.RefName
Paginator *db.ListOptions
}
type Result struct {
Query *indexer.SearchQuery
Total int64
Results []*code_indexer.Result
Languages []*code_indexer.SearchResultLanguages
Repos map[int64]*repo_model.Repository
Incomplete bool
}
func Search(ctx context.Context, keyword string, opts *Options) (*Result, error) {
query, err := indexer.ParseSearchQuery(keyword)
if err != nil {
return nil, err
}
var literals []string
for _, term := range query.Terms {
if term.Kind == indexer.SearchTermLiteral {
literals = append(literals, term.Text)
}
}
switch {
case opts.Engine == Indexer && len(literals) < len(query.Terms):
return nil, &indexer.SearchQueryError{Message: "Regular expressions are not supported by the code indexer"}
case opts.Engine == GitGrep && query.Language != "":
return nil, &indexer.SearchQueryError{Message: "Language qualifiers are not supported by git grep"}
case len(query.Repos)+len(query.Owners) > 0 && (opts.RepoID != 0 || opts.OwnerName != ""):
return nil, &indexer.SearchQueryError{Message: "Repository and owner qualifiers are only supported in global code search"}
}
result := &Result{Query: query}
if opts.Engine == GitGrep {
result.Results, result.Total, result.Incomplete, err = gitgrep.PerformSearch(ctx, opts.GitRepo, &gitgrep.SearchOptions{
RepoID: opts.RepoID, Ref: opts.Ref, Terms: query.Terms, Path: query.Path, Page: opts.Paginator.Page, PageSize: opts.Paginator.PageSize,
})
return result, err
}
if !code_indexer.IsAvailable(ctx) {
return nil, ErrIndexerUnavailable
}
repoIDs := []int64{opts.RepoID}
if opts.RepoID == 0 {
owners := query.Owners
if opts.OwnerName != "" {
owners = []string{opts.OwnerName}
}
repoIDs, err = repo_model.FindCodeSearchRepoIDs(ctx, opts.Doer, query.Repos, owners)
if err != nil || repoIDs != nil && len(repoIDs) == 0 {
return result, err
}
}
result.Total, result.Results, result.Languages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
RepoIDs: repoIDs, Terms: literals, Language: query.Language, Path: query.Path, Paginator: opts.Paginator,
})
if err != nil || opts.RepoID != 0 {
return result, err
}
result.Repos, err = repo_model.GetRepositoriesMapByIDs(ctx, container.FilterSlice(result.Results, func(item *code_indexer.Result) (int64, bool) {
return item.RepoID, true
}))
result.Results = slices.DeleteFunc(result.Results, func(item *code_indexer.Result) bool {
return result.Repos[item.RepoID] == nil // The index can still contain deleted repositories.
})
return result, err
}