mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-29 05:06:11 +01:00
parent
82851f429a
commit
eb21829800
|
@ -24,6 +24,7 @@ type GrepResult struct {
|
||||||
|
|
||||||
type GrepOptions struct {
|
type GrepOptions struct {
|
||||||
RefName string
|
RefName string
|
||||||
|
MaxResultLimit int
|
||||||
ContextLineNumber int
|
ContextLineNumber int
|
||||||
IsFuzzy bool
|
IsFuzzy bool
|
||||||
}
|
}
|
||||||
|
@ -59,6 +60,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
||||||
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
|
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
|
||||||
}
|
}
|
||||||
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD"))
|
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD"))
|
||||||
|
opts.MaxResultLimit = util.IfZero(opts.MaxResultLimit, 50)
|
||||||
stderr := bytes.Buffer{}
|
stderr := bytes.Buffer{}
|
||||||
err = cmd.Run(&RunOpts{
|
err = cmd.Run(&RunOpts{
|
||||||
Dir: repo.Path,
|
Dir: repo.Path,
|
||||||
|
@ -82,7 +84,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if line == "" {
|
if line == "" {
|
||||||
if len(results) >= 50 {
|
if len(results) >= opts.MaxResultLimit {
|
||||||
cancel()
|
cancel()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -101,6 +103,10 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
||||||
return scanner.Err()
|
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
|
// git grep exits with 1 if no results are found
|
||||||
if IsErrorExitCode(err, 1) && stderr.Len() == 0 {
|
if IsErrorExitCode(err, 1) && stderr.Len() == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
|
@ -31,6 +31,16 @@ func TestGrepSearch(t *testing.T) {
|
||||||
},
|
},
|
||||||
}, res)
|
}, 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{})
|
res, err = GrepSearch(context.Background(), repo, "no-such-content", GrepOptions{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, res, 0)
|
assert.Len(t, res, 0)
|
||||||
|
|
Loading…
Reference in a new issue