2024-03-24 21:35:00 +05:30
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2024-03-28 17:28:26 +05:30
|
|
|
"cmp"
|
2024-03-24 21:35:00 +05:30
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-04-11 17:04:53 +05:30
|
|
|
"io"
|
2024-03-24 21:35:00 +05:30
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-04-06 18:55:39 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-03-24 21:35:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type GrepResult struct {
|
|
|
|
Filename string
|
|
|
|
LineNumbers []int
|
|
|
|
LineCodes []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrepOptions struct {
|
|
|
|
RefName string
|
2024-03-25 18:55:22 +05:30
|
|
|
MaxResultLimit int
|
2024-03-24 21:35:00 +05:30
|
|
|
ContextLineNumber int
|
|
|
|
IsFuzzy bool
|
2024-05-14 21:11:03 +05:30
|
|
|
PathSpec []setting.Glob
|
2024-03-24 21:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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, "-"))
|
|
|
|
}
|
2024-05-14 21:11:03 +05:30
|
|
|
|
2024-04-06 18:55:39 +05:30
|
|
|
// pathspec
|
2024-05-14 21:11:03 +05:30
|
|
|
files := make([]string, 0,
|
|
|
|
len(setting.Indexer.IncludePatterns)+
|
|
|
|
len(setting.Indexer.ExcludePatterns)+
|
|
|
|
len(opts.PathSpec))
|
|
|
|
for _, expr := range append(setting.Indexer.IncludePatterns, opts.PathSpec...) {
|
|
|
|
files = append(files, ":"+expr.Pattern())
|
2024-04-06 18:55:39 +05:30
|
|
|
}
|
|
|
|
for _, expr := range setting.Indexer.ExcludePatterns {
|
|
|
|
files = append(files, ":^"+expr.Pattern())
|
|
|
|
}
|
|
|
|
cmd.AddDynamicArguments(cmp.Or(opts.RefName, "HEAD")).AddDashesAndList(files...)
|
2024-05-14 21:11:03 +05:30
|
|
|
|
2024-03-28 17:28:26 +05:30
|
|
|
opts.MaxResultLimit = cmp.Or(opts.MaxResultLimit, 50)
|
2024-03-24 21:35:00 +05:30
|
|
|
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
|
2024-04-11 17:04:53 +05:30
|
|
|
scanner := bufio.NewReader(stdoutReader)
|
2024-03-24 21:35:00 +05:30
|
|
|
var res *GrepResult
|
2024-04-11 17:04:53 +05:30
|
|
|
for {
|
|
|
|
line, err := scanner.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove delimiter.
|
|
|
|
if len(line) > 0 {
|
|
|
|
line = line[:len(line)-1]
|
|
|
|
}
|
|
|
|
|
2024-03-24 21:35:00 +05:30
|
|
|
if !isInBlock {
|
|
|
|
if _ /* ref */, filename, ok := strings.Cut(line, ":"); ok {
|
|
|
|
isInBlock = true
|
|
|
|
res = &GrepResult{Filename: filename}
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if line == "" {
|
2024-03-25 18:55:22 +05:30
|
|
|
if len(results) >= opts.MaxResultLimit {
|
2024-03-24 21:35:00 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 17:04:53 +05:30
|
|
|
return nil
|
2024-03-24 21:35:00 +05:30
|
|
|
},
|
|
|
|
})
|
2024-03-25 18:55:22 +05:30
|
|
|
// 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
|
|
|
|
}
|
2024-03-24 21:35:00 +05:30
|
|
|
// 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
|
|
|
|
}
|