aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-03-25 21:25:22 +0800
committerGitHub <noreply@github.com>2024-03-25 21:25:22 +0800
commit8e79aed573a3597c028bfc3598bd78f12e8a3ac3 (patch)
treed79016ce20f94704d155a82bd85993a328239901 /modules
parentbbaf62589fe538be4afc86455d772360de80e7d8 (diff)
downloadgitea-8e79aed573a3597c028bfc3598bd78f12e8a3ac3.tar.gz
gitea-8e79aed573a3597c028bfc3598bd78f12e8a3ac3.zip
Fix git grep search limit, add test (#30071)
Fix #30069
Diffstat (limited to 'modules')
-rw-r--r--modules/git/grep.go8
-rw-r--r--modules/git/grep_test.go10
2 files changed, 17 insertions, 1 deletions
diff --git a/modules/git/grep.go b/modules/git/grep.go
index e533995984..a6c486112a 100644
--- a/modules/git/grep.go
+++ b/modules/git/grep.go
@@ -24,6 +24,7 @@ type GrepResult struct {
type GrepOptions struct {
RefName string
+ MaxResultLimit int
ContextLineNumber int
IsFuzzy bool
}
@@ -59,6 +60,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
}
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD"))
+ opts.MaxResultLimit = util.IfZero(opts.MaxResultLimit, 50)
stderr := bytes.Buffer{}
err = cmd.Run(&RunOpts{
Dir: repo.Path,
@@ -82,7 +84,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
continue
}
if line == "" {
- if len(results) >= 50 {
+ if len(results) >= opts.MaxResultLimit {
cancel()
break
}
@@ -101,6 +103,10 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
return scanner.Err()
},
})
+ // git grep exits by cancel (killed), usually it is caused by the limit of results
+ if IsErrorExitCode(err, -1) && stderr.Len() == 0 {
+ return results, nil
+ }
// git grep exits with 1 if no results are found
if IsErrorExitCode(err, 1) && stderr.Len() == 0 {
return nil, nil
diff --git a/modules/git/grep_test.go b/modules/git/grep_test.go
index 3993fa7ffc..b5fa437c53 100644
--- a/modules/git/grep_test.go
+++ b/modules/git/grep_test.go
@@ -31,6 +31,16 @@ func TestGrepSearch(t *testing.T) {
},
}, res)
+ res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1})
+ assert.NoError(t, err)
+ assert.Equal(t, []*GrepResult{
+ {
+ Filename: "java-hello/main.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)