-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Support repo code search without setting up an indexer #29998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
666155e
c1ef9e8
6c1044a
d922c18
9f1f0ce
3360e75
12d488a
6e33dbf
edfd40d
2a58473
2358f16
3964358
1ffd9fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package git | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
type GrepResult struct { | ||
Filename string | ||
LineNumbers []int | ||
LineCodes []string | ||
} | ||
|
||
type GrepOptions struct { | ||
RefName string | ||
ContextLineNumber int | ||
IsFuzzy bool | ||
} | ||
|
||
func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepOptions) ([]*GrepResult, error) { | ||
stdoutReader, stdoutWriter, err := os.Pipe() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to creata os pipe to grep: %w", err) | ||
} | ||
stderrReader, stderrWriter, err := os.Pipe() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to creata os pipe to grep: %w", err) | ||
silverwind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
defer func() { | ||
_ = stdoutReader.Close() | ||
_ = stdoutWriter.Close() | ||
_ = stderrReader.Close() | ||
_ = stderrWriter.Close() | ||
}() | ||
|
||
/* | ||
The output is like this ( "^@" means \x00): | ||
|
||
HEAD:.air.toml | ||
6^@bin = "gitea" | ||
|
||
HEAD:.changelog.yml | ||
2^@repo: go-gitea/gitea | ||
*/ | ||
var stderr []byte | ||
var results []*GrepResult | ||
cmd := NewCommand(ctx, "grep", "--null", "--break", "--heading", "--fixed-strings", "--line-number", "--ignore-case", "--full-name") | ||
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber)) | ||
if opts.IsFuzzy { | ||
words := strings.Fields(search) | ||
for _, word := range words { | ||
cmd.AddOptionValues("-e", word) | ||
} | ||
} else { | ||
cmd.AddOptionValues("-e", search) | ||
} | ||
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD")) | ||
err = cmd.Run(&RunOpts{ | ||
Dir: repo.Path, | ||
Stdout: stdoutWriter, | ||
Stderr: stderrWriter, | ||
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error { | ||
_ = stdoutWriter.Close() | ||
_ = stderrWriter.Close() | ||
defer stdoutReader.Close() | ||
defer stderrReader.Close() | ||
|
||
isInBlock := false | ||
scanner := bufio.NewScanner(stdoutReader) | ||
var res *GrepResult | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if !isInBlock { | ||
if _ /* ref */, filename, ok := strings.Cut(line, ":"); ok { | ||
isInBlock = true | ||
res = &GrepResult{Filename: filename} | ||
results = append(results, res) | ||
} | ||
continue | ||
} | ||
if line == "" { | ||
if len(results) >= 50 { | ||
cancel() | ||
break | ||
} | ||
isInBlock = false | ||
continue | ||
} | ||
if line == "--" { | ||
continue | ||
} | ||
if lineNum, lineCode, ok := strings.Cut(line, "\x00"); ok { | ||
lineNumInt, _ := strconv.Atoi(lineNum) | ||
res.LineNumbers = append(res.LineNumbers, lineNumInt) | ||
res.LineCodes = append(res.LineCodes, lineCode) | ||
} | ||
} | ||
stderr, _ = io.ReadAll(stderrReader) | ||
return scanner.Err() | ||
}, | ||
}) | ||
if err != nil && !errors.Is(err, context.Canceled) && len(stderr) != 0 { | ||
return nil, fmt.Errorf("unable to run git grep: %w, stderr: %s", err, string(stderr)) | ||
} | ||
return results, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package git | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGrepSearch(t *testing.T) { | ||
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "language_stats_repo")) | ||
assert.NoError(t, err) | ||
defer repo.Close() | ||
|
||
res, err := GrepSearch(context.Background(), repo, "void", GrepOptions{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []*GrepResult{ | ||
{ | ||
Filename: "java-hello/main.java", | ||
LineNumbers: []int{3}, | ||
LineCodes: []string{" public static void main(String[] args)"}, | ||
}, | ||
{ | ||
Filename: "main.vendor.java", | ||
LineNumbers: []int{3}, | ||
LineCodes: []string{" public static void main(String[] args)"}, | ||
}, | ||
}, res) | ||
|
||
res, err = GrepSearch(context.Background(), repo, "no-such-content", GrepOptions{}) | ||
assert.NoError(t, err) | ||
assert.Len(t, res, 0) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,11 @@ package repo | |
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/modules/base" | ||
"code.gitea.io/gitea/modules/git" | ||
code_indexer "code.gitea.io/gitea/modules/indexer/code" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/services/context" | ||
|
@@ -17,11 +19,6 @@ const tplSearch base.TplName = "repo/search" | |
|
||
// Search render repository search page | ||
func Search(ctx *context.Context) { | ||
if !setting.Indexer.RepoIndexerEnabled { | ||
ctx.Redirect(ctx.Repo.RepoLink) | ||
return | ||
} | ||
|
||
language := ctx.FormTrim("l") | ||
keyword := ctx.FormTrim("q") | ||
|
||
|
@@ -42,24 +39,51 @@ func Search(ctx *context.Context) { | |
page = 1 | ||
} | ||
|
||
total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{ | ||
RepoIDs: []int64{ctx.Repo.Repository.ID}, | ||
Keyword: keyword, | ||
IsKeywordFuzzy: isFuzzy, | ||
Language: language, | ||
Paginator: &db.ListOptions{ | ||
Page: page, | ||
PageSize: setting.UI.RepoSearchPagingNum, | ||
}, | ||
}) | ||
if err != nil { | ||
if code_indexer.IsAvailable(ctx) { | ||
ctx.ServerError("SearchResults", err) | ||
return | ||
var total int | ||
var searchResults []*code_indexer.Result | ||
var searchResultLanguages []*code_indexer.SearchResultLanguages | ||
if setting.Indexer.RepoIndexerEnabled { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to intigrate the git grep search as its own indexer and set it as default. This way it is transparent for webUI or API what to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea how to do that clearly, and I am not a fan of adding a lot of "options". If you have better ideas, free free to edit this PR directly or have some following PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that will help but is unrelated to the architecture idea of mine. I try to create a pull request to your branch that would move acording to my proposal, so it can be checked out and tested etc ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect that there is no new option to be introduced. |
||
var err error | ||
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{ | ||
RepoIDs: []int64{ctx.Repo.Repository.ID}, | ||
Keyword: keyword, | ||
IsKeywordFuzzy: isFuzzy, | ||
Language: language, | ||
Paginator: &db.ListOptions{ | ||
Page: page, | ||
PageSize: setting.UI.RepoSearchPagingNum, | ||
}, | ||
}) | ||
if err != nil { | ||
if code_indexer.IsAvailable(ctx) { | ||
ctx.ServerError("SearchResults", err) | ||
return | ||
} | ||
ctx.Data["CodeIndexerUnavailable"] = true | ||
} else { | ||
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx) | ||
} | ||
ctx.Data["CodeIndexerUnavailable"] = true | ||
} else { | ||
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx) | ||
res, err := git.GrepSearch(ctx, ctx.Repo.GitRepo, keyword, git.GrepOptions{ContextLineNumber: 3, IsFuzzy: isFuzzy}) | ||
if err != nil { | ||
ctx.ServerError("GrepSearch", err) | ||
return | ||
} | ||
total = len(res) | ||
pageStart := min((page-1)*setting.UI.RepoSearchPagingNum, len(res)) | ||
pageEnd := min(page*setting.UI.RepoSearchPagingNum, len(res)) | ||
res = res[pageStart:pageEnd] | ||
for _, r := range res { | ||
searchResults = append(searchResults, &code_indexer.Result{ | ||
RepoID: ctx.Repo.Repository.ID, | ||
Filename: r.Filename, | ||
CommitID: ctx.Repo.CommitID, | ||
// UpdatedUnix: not supported yet | ||
// Language: not supported yet | ||
// Color: not supported yet | ||
Lines: code_indexer.HighlightSearchResultCode(r.Filename, r.LineNumbers, strings.Join(r.LineCodes, "\n")), | ||
}) | ||
} | ||
} | ||
|
||
ctx.Data["Repo"] = ctx.Repo.Repository | ||
|
Uh oh!
There was an error while loading. Please reload this page.