mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-17 04:47:10 +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>
68 lines
2.6 KiB
Go
68 lines
2.6 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.dev/modules/git/gitrepo"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGrepSearch(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.RepoRootPath, t.TempDir())()
|
|
repo, err := OpenRepositoryLocal(t.Context(), filepath.Join(testReposDir, "language_stats_repo"))
|
|
require.NoError(t, err)
|
|
defer repo.Close()
|
|
for _, testCase := range []struct {
|
|
name string
|
|
patterns []string
|
|
paths []string
|
|
limit int
|
|
expected []string
|
|
incomplete bool
|
|
}{
|
|
{name: "paths in order", patterns: []string{"(*UTF)void"}, expected: []string{"java-hello/main.java", "main.vendor.java"}},
|
|
{name: "directory", patterns: []string{"(*UTF)void"}, paths: []string{":(literal)java-hello/"}, expected: []string{"java-hello/main.java"}},
|
|
{name: "literal directory", patterns: []string{"(*UTF)void"}, paths: []string{":(literal)java-*/"}},
|
|
{name: "cap", patterns: []string{"(*UTF)void"}, limit: 1, expected: []string{"java-hello/main.java"}, incomplete: true},
|
|
{name: "exact cap", patterns: []string{"(*UTF)void"}, limit: 2, expected: []string{"java-hello/main.java", "main.vendor.java"}},
|
|
{name: "no match", patterns: []string{"(*UTF)no-such-content"}},
|
|
{name: "dash", patterns: []string{"(*UTF)-v"}, expected: []string{".gitattributes"}},
|
|
} {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
var paths []string
|
|
incomplete, err := GrepSearch(t.Context(), repo, testCase.patterns, "HEAD", func(filename string) (bool, error) {
|
|
if testCase.limit > 0 && len(paths) == testCase.limit {
|
|
return false, nil
|
|
}
|
|
paths = append(paths, filename)
|
|
return true, nil
|
|
}, testCase.paths...)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, testCase.expected, paths)
|
|
assert.Equal(t, testCase.incomplete, incomplete)
|
|
})
|
|
}
|
|
visit := func(_filename string) (bool, error) { return true, nil }
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
cancel()
|
|
_, err = GrepSearch(ctx, repo, []string{"(*UTF)void"}, "HEAD", visit)
|
|
assert.ErrorIs(t, err, context.Canceled)
|
|
_, err = GrepSearch(t.Context(), repo, []string{"(*UTF)void"}, "HEAD", func(_filename string) (bool, error) {
|
|
return false, errors.New("read failed")
|
|
})
|
|
assert.ErrorContains(t, err, "read failed")
|
|
nonExistingRepo := &Repository{RepositoryBase: RepositoryBase{repoFacade: gitrepo.RepositoryUnmanaged("no-such-git-repo")}}
|
|
_, err = GrepSearch(t.Context(), nonExistingRepo, []string{"(*UTF)void"}, "HEAD", visit)
|
|
assert.Error(t, err)
|
|
}
|