-
-
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
666155e
fix
wxiaoguang c1ef9e8
add "cancel" call
wxiaoguang 6c1044a
Merge branch 'main' into git-code-search
wxiaoguang d922c18
fix
wxiaoguang 9f1f0ce
Merge branch 'main' into git-code-search
wxiaoguang 3360e75
Update modules/git/grep.go
silverwind 12d488a
Update modules/git/grep.go
silverwind 6e33dbf
Merge branch 'main' into git-code-search
wxiaoguang edfd40d
doc
wxiaoguang 2a58473
better error handling
wxiaoguang 2358f16
Merge branch 'main' into git-code-search
wxiaoguang 3964358
add prompt if git grep
wxiaoguang 1ffd9fb
Merge branch 'main' into git-code-search
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package git | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"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 create os pipe to grep: %w", err) | ||
} | ||
defer func() { | ||
_ = stdoutReader.Close() | ||
_ = stdoutWriter.Close() | ||
}() | ||
|
||
/* | ||
The output is like this ( "^@" means \x00): | ||
|
||
HEAD:.air.toml | ||
6^@bin = "gitea" | ||
|
||
HEAD:.changelog.yml | ||
2^@repo: go-gitea/gitea | ||
*/ | ||
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", strings.TrimLeft(word, "-")) | ||
} | ||
} else { | ||
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-")) | ||
} | ||
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD")) | ||
stderr := bytes.Buffer{} | ||
err = cmd.Run(&RunOpts{ | ||
Dir: repo.Path, | ||
Stdout: stdoutWriter, | ||
Stderr: &stderr, | ||
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error { | ||
_ = stdoutWriter.Close() | ||
defer stdoutReader.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) | ||
} | ||
} | ||
return scanner.Err() | ||
}, | ||
}) | ||
// git grep exits with 1 if no results are found | ||
if IsErrorExitCode(err, 1) && stderr.Len() == 0 { | ||
return nil, nil | ||
} | ||
if err != nil && !errors.Is(err, context.Canceled) { | ||
return nil, fmt.Errorf("unable to run git grep: %w, stderr: %s", err, stderr.String()) | ||
} | ||
return results, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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) | ||
|
||
res, err = GrepSearch(context.Background(), &Repository{Path: "no-such-git-repo"}, "no-such-content", GrepOptions{}) | ||
assert.Error(t, err) | ||
assert.Len(t, res, 0) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I added a prompt like this, maybe it could make it clearer. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect that there is no new option to be introduced.