Files
gitea/services/convert/code_search.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

47 lines
1.7 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package convert
import (
"context"
"net/url"
"path"
repo_model "gitea.dev/models/repo"
"gitea.dev/modules/git"
code_indexer "gitea.dev/modules/indexer/code"
api "gitea.dev/modules/structs"
"gitea.dev/modules/util"
)
func ToCodeSearchResultItem(ctx context.Context, repo *repo_model.Repository, apiRepo *api.Repository, result *code_indexer.Result) *api.CodeSearchResultItem {
fragments := code_indexer.BuildResultFragments(result.Content, result.ContentMatches)
item := &api.CodeSearchResultItem{
Name: path.Base(result.Filename),
Path: result.Filename,
URL: repo.APIURL(ctx) + "/contents/" + util.PathEscapeSegments(result.Filename) + "?ref=" + url.QueryEscape(result.CommitID),
HTMLURL: repo.HTMLURL(ctx) + "/src/" + git.RefNameFromCommit(result.CommitID).RefWebLinkPath() + "/" + util.PathEscapeSegments(result.Filename),
Language: result.Language,
Repository: apiRepo,
TextMatches: make([]*api.CodeSearchTextMatch, 0, len(fragments)),
}
for _, fragment := range fragments {
textMatch := &api.CodeSearchTextMatch{
ObjectURL: item.URL,
ObjectType: "FileContent",
Property: "content",
Fragment: fragment.Content,
Matches: make([]*api.CodeSearchTextMatchTerm, 0, len(fragment.Matches)),
}
for _, matchRange := range fragment.Matches {
textMatch.Matches = append(textMatch.Matches, &api.CodeSearchTextMatchTerm{
Text: fragment.Content[matchRange.Start:matchRange.End],
Indices: []int{matchRange.Start, matchRange.End},
})
}
item.TextMatches = append(item.TextMatches, textMatch)
}
return item
}