mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
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>
63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"gitea.dev/models/db"
|
|
"gitea.dev/modules/indexer"
|
|
code_indexer "gitea.dev/modules/indexer/code"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/services/codesearch"
|
|
"gitea.dev/services/context"
|
|
)
|
|
|
|
func SearchCode(ctx *context.Context, opts *codesearch.Options) {
|
|
keyword := ctx.FormTrim("q")
|
|
ctx.Data["Keyword"] = keyword
|
|
ctx.Data["IsRepoIndexerEnabled"] = opts.Engine == codesearch.Indexer
|
|
if keyword == "" {
|
|
return
|
|
}
|
|
opts.Doer = ctx.Doer
|
|
opts.Paginator = &db.ListOptions{Page: max(1, ctx.FormInt("page")), PageSize: setting.UI.RepoSearchPagingNum}
|
|
result, err := codesearch.Search(ctx, keyword, opts)
|
|
if err != nil {
|
|
if queryError, ok := errors.AsType[*indexer.SearchQueryError](err); ok {
|
|
ctx.Data["CodeSearchError"] = queryError.Message
|
|
} else if errors.Is(err, codesearch.ErrIndexerUnavailable) {
|
|
ctx.Data["CodeIndexerUnavailable"] = true
|
|
} else {
|
|
ctx.ServerErrorAuto(err)
|
|
}
|
|
return
|
|
}
|
|
ctx.Data["Language"] = result.Query.Language
|
|
ctx.Data["KeywordWithoutLanguage"] = result.Query.WithoutLanguage()
|
|
results := make([]*codeSearchResult, len(result.Results))
|
|
for i, hit := range result.Results {
|
|
results[i] = &codeSearchResult{Result: hit}
|
|
for _, fragment := range code_indexer.BuildResultFragments(hit.Content, hit.ContentMatches) {
|
|
content := strings.TrimSuffix(fragment.Content, "\n")
|
|
lineNumbers := make([]int, 1+strings.Count(content, "\n"))
|
|
for i := range lineNumbers {
|
|
lineNumbers[i] = fragment.StartLine + i
|
|
}
|
|
results[i].Fragments = append(results[i].Fragments, code_indexer.HighlightSearchResultCode(hit.Filename, hit.Language, lineNumbers, content, fragment.Matches...))
|
|
}
|
|
}
|
|
ctx.Data["SearchResults"] = results
|
|
ctx.Data["SearchResultLanguages"] = result.Languages
|
|
ctx.Data["RepoMaps"] = result.Repos
|
|
ctx.Data["CodeSearchIncomplete"] = result.Incomplete
|
|
ctx.Data["Page"] = context.NewPagerBuilder(ctx).TotalCount(result.Total).PerPageLimit(opts.Paginator.PageSize).CurPage(opts.Paginator.Page).Build()
|
|
}
|
|
|
|
type codeSearchResult struct {
|
|
*code_indexer.Result
|
|
Fragments [][]*code_indexer.ResultLine
|
|
}
|