Merge pull request 'git-grep: refactor defaults' (#4964) from yoctozepto/git-grep-refactor-defaults into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4964
Reviewed-by: Shiny Nematoda <snematoda@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-08-19 13:12:37 +00:00
commit 0c70e11df8

View file

@ -36,6 +36,12 @@ type GrepOptions struct {
PathSpec []setting.Glob PathSpec []setting.Glob
} }
func (opts *GrepOptions) ensureDefaults() {
opts.RefName = cmp.Or(opts.RefName, "HEAD")
opts.MaxResultLimit = cmp.Or(opts.MaxResultLimit, 50)
opts.MatchesPerFile = cmp.Or(opts.MatchesPerFile, 20)
}
func hasPrefixFold(s, t string) bool { func hasPrefixFold(s, t string) bool {
if len(s) < len(t) { if len(s) < len(t) {
return false return false
@ -53,6 +59,8 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
_ = stdoutWriter.Close() _ = stdoutWriter.Close()
}() }()
opts.ensureDefaults()
/* /*
The output is like this ("^@" means \x00; the first number denotes the line, The output is like this ("^@" means \x00; the first number denotes the line,
the second number denotes the column of the first match in line): the second number denotes the column of the first match in line):
@ -69,7 +77,6 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
"-I", "--null", "--break", "--heading", "--column", "-I", "--null", "--break", "--heading", "--column",
"--fixed-strings", "--line-number", "--ignore-case", "--full-name") "--fixed-strings", "--line-number", "--ignore-case", "--full-name")
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber)) cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
opts.MatchesPerFile = cmp.Or(opts.MatchesPerFile, 20)
cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile)) cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile))
words := []string{search} words := []string{search}
if opts.IsFuzzy { if opts.IsFuzzy {
@ -90,9 +97,8 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
for _, expr := range setting.Indexer.ExcludePatterns { for _, expr := range setting.Indexer.ExcludePatterns {
files = append(files, ":^"+expr.Pattern()) files = append(files, ":^"+expr.Pattern())
} }
cmd.AddDynamicArguments(cmp.Or(opts.RefName, "HEAD")).AddDashesAndList(files...) cmd.AddDynamicArguments(opts.RefName).AddDashesAndList(files...)
opts.MaxResultLimit = cmp.Or(opts.MaxResultLimit, 50)
stderr := bytes.Buffer{} stderr := bytes.Buffer{}
err = cmd.Run(&RunOpts{ err = cmd.Run(&RunOpts{
Timeout: time.Duration(setting.Git.Timeout.Grep) * time.Second, Timeout: time.Duration(setting.Git.Timeout.Grep) * time.Second,