2024-03-24 12:05:00 -04:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
2024-08-06 01:57:25 -04:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
2024-03-24 12:05:00 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2024-03-28 07:58:26 -04:00
|
|
|
"cmp"
|
2024-03-24 12:05:00 -04:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-04-11 07:34:53 -04:00
|
|
|
"io"
|
2024-03-24 12:05:00 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-08-11 15:09:57 -04:00
|
|
|
"time"
|
2024-04-06 09:25:39 -04:00
|
|
|
|
2024-10-30 10:45:18 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-04-06 09:25:39 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-03-24 12:05:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type GrepResult struct {
|
2024-08-06 01:57:25 -04:00
|
|
|
Filename string
|
|
|
|
LineNumbers []int
|
|
|
|
LineCodes []string
|
|
|
|
HighlightedRanges [][3]int
|
2024-03-24 12:05:00 -04:00
|
|
|
}
|
|
|
|
|
2024-08-12 14:57:42 -04:00
|
|
|
type grepMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
FixedGrepMode grepMode = iota
|
|
|
|
FixedAnyGrepMode
|
|
|
|
RegExpGrepMode
|
|
|
|
)
|
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
type GrepOptions struct {
|
|
|
|
RefName string
|
2024-03-25 09:25:22 -04:00
|
|
|
MaxResultLimit int
|
2024-10-30 10:45:18 -04:00
|
|
|
MatchesPerFile int // >= git 2.38
|
2024-03-24 12:05:00 -04:00
|
|
|
ContextLineNumber int
|
2024-08-12 14:57:42 -04:00
|
|
|
Mode grepMode
|
2024-05-14 11:41:03 -04:00
|
|
|
PathSpec []setting.Glob
|
2024-03-24 12:05:00 -04:00
|
|
|
}
|
|
|
|
|
2024-08-15 04:23:47 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-06 01:57:25 -04:00
|
|
|
func hasPrefixFold(s, t string) bool {
|
|
|
|
if len(s) < len(t) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.EqualFold(s[:len(t)], t)
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
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()
|
|
|
|
}()
|
|
|
|
|
2024-08-15 04:23:47 -04:00
|
|
|
opts.ensureDefaults()
|
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
/*
|
2024-08-10 10:41:12 -04:00
|
|
|
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):
|
2024-03-24 12:05:00 -04:00
|
|
|
|
|
|
|
HEAD:.air.toml
|
2024-08-10 10:41:12 -04:00
|
|
|
6^@8^@bin = "gitea"
|
2024-03-24 12:05:00 -04:00
|
|
|
|
|
|
|
HEAD:.changelog.yml
|
2024-08-10 10:41:12 -04:00
|
|
|
2^@10^@repo: go-gitea/gitea
|
2024-03-24 12:05:00 -04:00
|
|
|
*/
|
|
|
|
var results []*GrepResult
|
2024-08-11 08:24:40 -04:00
|
|
|
// -I skips binary files
|
2024-08-06 01:57:25 -04:00
|
|
|
cmd := NewCommand(ctx, "grep",
|
2024-08-16 09:23:25 -04:00
|
|
|
"-I", "--null", "--break", "--heading",
|
2024-08-12 14:57:42 -04:00
|
|
|
"--line-number", "--ignore-case", "--full-name")
|
|
|
|
if opts.Mode == RegExpGrepMode {
|
2024-09-16 08:23:15 -04:00
|
|
|
// No `--column` -- regexp mode does not support highlighting in the
|
|
|
|
// current implementation as the length of the match is unknown from
|
|
|
|
// `grep` but required for highlighting.
|
2024-08-12 14:57:42 -04:00
|
|
|
cmd.AddArguments("--perl-regexp")
|
|
|
|
} else {
|
2024-08-16 09:23:25 -04:00
|
|
|
cmd.AddArguments("--fixed-strings", "--column")
|
2024-08-12 14:57:42 -04:00
|
|
|
}
|
2024-10-30 10:45:18 -04:00
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
|
2024-10-30 10:45:18 -04:00
|
|
|
|
|
|
|
// --max-count requires at least git 2.38
|
|
|
|
if CheckGitVersionAtLeast("2.38.0") == nil {
|
|
|
|
cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile))
|
|
|
|
} else {
|
|
|
|
log.Warn("git-grep: --max-count requires at least git 2.38")
|
|
|
|
}
|
|
|
|
|
2024-08-06 01:57:25 -04:00
|
|
|
words := []string{search}
|
2024-08-12 14:57:42 -04:00
|
|
|
if opts.Mode == FixedAnyGrepMode {
|
2024-08-06 01:57:25 -04:00
|
|
|
words = strings.Fields(search)
|
|
|
|
}
|
|
|
|
for _, word := range words {
|
2024-08-12 08:05:01 -04:00
|
|
|
cmd.AddGitGrepExpression(word)
|
2024-03-24 12:05:00 -04:00
|
|
|
}
|
2024-05-14 11:41:03 -04:00
|
|
|
|
2024-04-06 09:25:39 -04:00
|
|
|
// pathspec
|
2024-05-14 11:41:03 -04:00
|
|
|
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 09:25:39 -04:00
|
|
|
}
|
|
|
|
for _, expr := range setting.Indexer.ExcludePatterns {
|
|
|
|
files = append(files, ":^"+expr.Pattern())
|
|
|
|
}
|
2024-08-15 04:23:47 -04:00
|
|
|
cmd.AddDynamicArguments(opts.RefName).AddDashesAndList(files...)
|
2024-05-14 11:41:03 -04:00
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
stderr := bytes.Buffer{}
|
|
|
|
err = cmd.Run(&RunOpts{
|
2024-08-11 15:09:57 -04:00
|
|
|
Timeout: time.Duration(setting.Git.Timeout.Grep) * time.Second,
|
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
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 07:34:53 -04:00
|
|
|
scanner := bufio.NewReader(stdoutReader)
|
2024-03-24 12:05:00 -04:00
|
|
|
var res *GrepResult
|
2024-04-11 07:34:53 -04:00
|
|
|
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 12:05:00 -04:00
|
|
|
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 09:25:22 -04:00
|
|
|
if len(results) >= opts.MaxResultLimit {
|
2024-03-24 12:05:00 -04:00
|
|
|
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)
|
2024-09-16 08:23:15 -04:00
|
|
|
// We support highlighting only when `--column` parameter is used.
|
2024-08-06 01:57:25 -04:00
|
|
|
if lineCol, lineCode2, ok := strings.Cut(lineCode, "\x00"); ok {
|
|
|
|
lineColInt, _ := strconv.Atoi(lineCol)
|
|
|
|
start := lineColInt - 1
|
|
|
|
matchLen := len(lineCode2)
|
|
|
|
for _, word := range words {
|
|
|
|
if hasPrefixFold(lineCode2[start:], word) {
|
|
|
|
matchLen = len(word)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.HighlightedRanges = append(res.HighlightedRanges, [3]int{
|
|
|
|
len(res.LineCodes),
|
|
|
|
start,
|
|
|
|
start + matchLen,
|
|
|
|
})
|
|
|
|
res.LineCodes = append(res.LineCodes, lineCode2)
|
|
|
|
continue
|
|
|
|
}
|
2024-03-24 12:05:00 -04:00
|
|
|
res.LineCodes = append(res.LineCodes, lineCode)
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 07:34:53 -04:00
|
|
|
return nil
|
2024-03-24 12:05:00 -04:00
|
|
|
},
|
|
|
|
})
|
2024-03-25 09:25:22 -04:00
|
|
|
// 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 12:05:00 -04:00
|
|
|
// 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
|
|
|
|
}
|