Files
gitea/modules/git/grep.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

50 lines
1.4 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"bufio"
"context"
"fmt"
"strings"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/util"
)
func GrepSearch(ctx context.Context, repo *Repository, patterns []string, refName string, visit func(string) (bool, error), paths ...string) (incomplete bool, err error) {
cmd := gitcmd.NewCommand("grep", "-l", "-z", "-I", "--perl-regexp", "--ignore-case", "--full-name", "--all-match")
for _, pattern := range patterns {
cmd.AddOptionGrepExpr(pattern)
}
cmd.AddDynamicArguments(refName)
cmd.AddDashesAndList(paths...)
stdoutReader, stdoutReaderClose := cmd.MakeStdoutPipe()
defer stdoutReaderClose()
runErr := cmd.WithRepo(repo).
WithPipelineFunc(func(pipelineCtx gitcmd.Context) error {
scanner := bufio.NewScanner(stdoutReader)
scanner.Split(util.BufioScannerSplit(0))
for scanner.Scan() {
_, filename, _ := strings.Cut(scanner.Text(), ":")
keepGoing, err := visit(filename)
if err != nil || !keepGoing {
incomplete = !keepGoing
return pipelineCtx.CancelPipeline(err)
}
}
return scanner.Err()
}).RunWithStderr(ctx)
if err := ctx.Err(); err != nil {
return incomplete, err
}
if gitcmd.IsErrorExitCode(runErr, 1) && runErr.Stderr() == "" {
return incomplete, nil
}
if runErr != nil {
return false, fmt.Errorf("GrepSearch: %w", runErr)
}
return incomplete, nil
}