Add a git grep search timeout (#36809)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
@@ -39,6 +40,10 @@ type GrepOptions struct {
|
|||||||
PathspecList []string
|
PathspecList []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// grepSearchTimeout is the timeout for git grep search, it should be long enough to get results
|
||||||
|
// but not too long to cause performance issues
|
||||||
|
const grepSearchTimeout = 30 * time.Second
|
||||||
|
|
||||||
func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepOptions) ([]*GrepResult, error) {
|
func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepOptions) ([]*GrepResult, error) {
|
||||||
/*
|
/*
|
||||||
The output is like this ( "^@" means \x00):
|
The output is like this ( "^@" means \x00):
|
||||||
@@ -76,6 +81,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
|||||||
stdoutReader, stdoutReaderClose := cmd.MakeStdoutPipe()
|
stdoutReader, stdoutReaderClose := cmd.MakeStdoutPipe()
|
||||||
defer stdoutReaderClose()
|
defer stdoutReaderClose()
|
||||||
err := cmd.WithDir(repo.Path).
|
err := cmd.WithDir(repo.Path).
|
||||||
|
WithTimeout(grepSearchTimeout).
|
||||||
WithPipelineFunc(func(ctx gitcmd.Context) error {
|
WithPipelineFunc(func(ctx gitcmd.Context) error {
|
||||||
isInBlock := false
|
isInBlock := false
|
||||||
rd := bufio.NewReaderSize(stdoutReader, util.IfZero(opts.MaxLineLength, 16*1024))
|
rd := bufio.NewReaderSize(stdoutReader, util.IfZero(opts.MaxLineLength, 16*1024))
|
||||||
|
|||||||
Reference in New Issue
Block a user